chiark / gitweb /
wip resolve
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 00:04:56 +0000 (01:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 00:04:56 +0000 (01:04 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs

index 9bcf41d0d7cc805be84ef68979419cdde981f1e6..bbcb730f883486a8c448d10f948e5c76aaaddeae 100644 (file)
@@ -258,6 +258,9 @@ struct ResolveContext<'c> {
   agg: &'c Aggregate,
   link: &'c LinkName,
   end: LinkEnd,
+  lookup_ordinary:             Vec<SectionName>,
+  lookup_limit:                Vec<SectionName>,
+  lookup_server_sections_only: Vec<SectionName>,
 }
 
 trait Parseable: Sized {
@@ -265,12 +268,11 @@ trait Parseable: Sized {
 }
 
 impl<'c> ResolveContext<'c> {
-  fn first_of_raw(&self, key: &'static str,
-                  sections: &[ &dyn Fn() -> SectionName ])
+  fn first_of_raw(&self, key: &'static str, sections: &[SectionName])
                   -> Option<&'c RawVal> {
     for section in sections {
       if let Some(raw) = self.agg.sections
-        .get(&section())
+        .get(section)
         .and_then(|vars: &SectionMap| vars.get(key))
       {
         return Some(raw)
@@ -280,8 +282,7 @@ impl<'c> ResolveContext<'c> {
   }
 
   #[throws(AE)]
-  fn first_of<T>(&self, key: &'static str,
-                 sections: &[ &dyn Fn() -> SectionName ])
+  fn first_of<T>(&self, key: &'static str, sections: &[SectionName])
                  -> Option<T>
   where T: Parseable
   {
@@ -300,12 +301,7 @@ impl<'c> ResolveContext<'c> {
   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,
-    ])?
+    self.first_of(key, &self.lookup_ordinary)?
       .unwrap_or_default()
   }
 
@@ -314,10 +310,7 @@ impl<'c> ResolveContext<'c> {
   where T: Parseable + Default + Ord
   {
     let val = self.ordinary(key)?;
-    if let Some(limit) = self.first_of(key, &[
-      &|| SN::ServerLimit(self.link.server.clone()),
-      &|| SN::GlobalLimit,
-    ])? {
+    if let Some(limit) = self.first_of(key, &self.lookup_limit)? {
       min(val, limit)
     } else {
       val
@@ -348,10 +341,7 @@ impl<'c> ResolveContext<'c> {
     match self.end {
       LinkEnd::Client => self.ordinary(key)?,
       LinkEnd::Server => {
-        self.first_of(key, &[
-          &|| SN::Common,
-          &|| SN::Server(self.link.server.clone()),
-        ])?
+        self.first_of(key, &self.lookup_server_sections_only)?
           .unwrap_or_default()
       },
     }
@@ -370,7 +360,7 @@ fn resolve_instance_config() {
 pub fn read() {
   let opts = config::Opts::from_args();
 
-  (||{
+  let agg = (||{
     let mut agg = Aggregate::default();
 
     agg.read_toplevel(&opts.config)?;
@@ -380,6 +370,31 @@ pub fn read() {
 
     eprintln!("GOT {:#?}", agg);
 
-    Ok::<_,AE>(())
+    Ok::<_,AE>(agg)
   })().context("read configuration")?;
+
+  let link = LinkName {
+    server: "fooxxx".parse().unwrap(),
+    client: "127.0.0.1".parse().unwrap(),
+  };
+
+  let rctx = ResolveContext {
+    agg: &agg,
+    link: &link,
+    end: LinkEnd::Server,
+    lookup_ordinary: vec![
+      SN::Link(link.clone()),
+      SN::Client(link.client.clone()),
+      SN::Server(link.server.clone()),
+      SN::Common,
+    ],
+    lookup_limit: vec![
+      SN::ServerLimit(link.server.clone()),
+      SN::GlobalLimit,
+    ],
+    lookup_server_sections_only: vec![
+      SN::Common,
+      SN::Server(link.server.clone()),
+    ],
+  };
 }