chiark / gitweb /
wip resolve
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 23 Jul 2021 23:57:11 +0000 (00:57 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 23 Jul 2021 23:57:11 +0000 (00:57 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs
src/types.rs

index 60c090b7ec8fc1f1200618ae0eda66c862225e86..9bcf41d0d7cc805be84ef68979419cdde981f1e6 100644 (file)
@@ -69,12 +69,6 @@ pub enum SectionName {
 }
 pub use SectionName as SN;
 
-#[derive(Debug,Clone,Hash,Eq,PartialEq)]
-struct LinkName {
-  server: ServerName,
-  client: ClientName,
-}
-
 #[derive(Debug,Clone)]
 struct RawVal { val: Option<String>, loc: Arc<PathBuf> }
 type SectionMap = HashMap<String, RawVal>;
@@ -266,8 +260,12 @@ struct ResolveContext<'c> {
   end: LinkEnd,
 }
 
+trait Parseable: Sized {
+  fn parse(s: &Option<String>) -> Result<Self, AE>;
+}
+
 impl<'c> ResolveContext<'c> {
-  fn first_of_raw(&self, key: &str,
+  fn first_of_raw(&self, key: &'static str,
                   sections: &[ &dyn Fn() -> SectionName ])
                   -> Option<&'c RawVal> {
     for section in sections {
@@ -282,66 +280,79 @@ impl<'c> ResolveContext<'c> {
   }
 
   #[throws(AE)]
-  fn first_of<T>(&self, key: &str,
+  fn first_of<T>(&self, key: &'static str,
                  sections: &[ &dyn Fn() -> SectionName ])
                  -> Option<T>
-  where T: FromStr
+  where T: Parseable
   {
     match self.first_of_raw(key, sections) {
       None => None,
       Some(raw) => Some({
-        raw.val.parse()
+        Parseable::parse(&raw.val)
           .context(key)
-          .context(r#"in section "{}""#, &raw.section)
+//          .with_context(|| format!(r#"in section "{}""#, &section))
           .dcontext(&raw.loc)?
       }),
     }
   }
 
-  pub fn ordinary<T>(&self, key: &str) -> T where T: FromStr + Default {
+  #[throws(AE)]
+  pub fn ordinary<T>(&self, key: &'static str) -> T
+  where T: Parseable + Default
+  {
     self.first_of(key, &[
-      || SN::Link(self.link.clone()),
-      || SN::Client(self.link.client.clone()),
-      || SN::Server(self.link.server.clone()),
-      || SN::Common,
+      &|| SN::Link(self.link.clone()),
+      &|| SN::Client(self.link.client.clone()),
+      &|| SN::Server(self.link.server.clone()),
+      &|| SN::Common,
     ])?
       .unwrap_or_default()
   }
 
-  pub fn limited<T>(&self, key: &str) -> T
-  where T: FromStr + Default + PartialOrd
+  #[throws(AE)]
+  pub fn limited<T>(&self, key: &'static str) -> T
+  where T: Parseable + Default + Ord
   {
-    let val = self.ordinary(key);
+    let val = self.ordinary(key)?;
     if let Some(limit) = self.first_of(key, &[
-      || SN::LimitServer(self.link.server.clone()),
-      || SN::LimitGlobal,
+      &|| SN::ServerLimit(self.link.server.clone()),
+      &|| SN::GlobalLimit,
     ])? {
-      val = min(val, limit)
+      min(val, limit)
+    } else {
+      val
     }
-    val
   }
 
-  pub fn client<T>(&self, key: &str) -> T where T: FromStr + Default {
+  #[throws(AE)]
+  pub fn client<T>(&self, key: &'static str) -> T
+  where T: Parseable + Default {
     match self.end {
       LinkEnd::Client => self.ordinary(key)?,
       LinkEnd::Server => default(),
     }
   }
-  pub fn server<T>(&self, key: &str) -> T where T: FromStr + Default {
+  #[throws(AE)]
+  pub fn server<T>(&self, key: &'static str) -> T
+  where T: Parseable + Default {
     match self.end {
       LinkEnd::Server => self.ordinary(key)?,
       LinkEnd::Client => default(),
     }
   }
 
-  pub fn special_ipif<T>(&self, key: &str) -> T where T: FromStr + Default {
+  #[throws(AE)]
+  pub fn special_ipif<T>(&self, key: &'static str) -> T
+  where T: Parseable + Default
+  {
     match self.end {
       LinkEnd::Client => self.ordinary(key)?,
       LinkEnd::Server => {
         self.first_of(key, &[
-          || SN::Common,
-          || SN::Server(self.link.server.clone()),
+          &|| SN::Common,
+          &|| SN::Server(self.link.server.clone()),
         ])?
+          .unwrap_or_default()
       },
     }
   }
@@ -350,7 +361,7 @@ impl<'c> ResolveContext<'c> {
 /*
 fn resolve_instance_config() {
   InstanceConfig {
-    max_batch_down: resolve::limited(&agg, "max_batch_down")
+    max_batch_down: resolve::limited(&agg, "max_batch_down")?.into()
   }
 }
 */
index 86652e46b84d558689d209f7b7a63f8560fbc619..a18ee8bed0d524f149acf6f57bf7c9d7a89dd0e6 100644 (file)
@@ -10,6 +10,12 @@ pub struct ServerName(pub String);
 #[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
 pub struct ClientName(pub Ipv4Addr);
 
+#[derive(Debug,Clone,Hash,Eq,PartialEq)]
+pub struct LinkName {
+  pub server: ServerName,
+  pub client: ClientName,
+}
+
 impl FromStr for ClientName {
   type Err = AE;
   #[throws(AE)]