chiark / gitweb /
config, break out var section list
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 10:14:51 +0000 (11:14 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Jul 2021 10:14:51 +0000 (11:14 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
macros/macros.rs
src/config.rs
src/lib.rs

index 0bcfc33c82e053fcdbdeae1e8df2bcea5f01b1bc..be1dee1c75a86975111d698e9dfb79a7382eac25 100644 (file)
@@ -2,6 +2,8 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later
 // There is NO WARRANTY.
 
+#![allow(unused_imports)] // xxx
+
 use syn::{parse_macro_input, Data, DataStruct, DeriveInput};
 use quote::{quote, quote_spanned};
 use proc_macro2::Literal;
index 8ad04e87be9afbb9faf6775ca8c355cf7350b847..e72f9981c13226723d10a41894ea20098445f9e8 100644 (file)
@@ -264,9 +264,7 @@ 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>,
+  all_sections: Vec<SectionName>,
 }
 
 trait Parseable: Sized {
@@ -292,10 +290,46 @@ macro_rules! parseable_from_str { ($t:ty) => {
 } }
 parseable_from_str!{u32}
 
+#[derive(Debug,Copy,Clone)]
+enum SectionKindList {
+  Ordinary,
+  Limited,
+  Limits,
+  ClientAgnostic,
+  ServerName,
+}
+use SectionKindList as SKL;
+
+impl SectionKindList {
+  fn contains(self, s: &SectionName) -> bool {
+    match self {
+      SKL::Ordinary       => matches!(s, SN::Link(_)
+                                       | SN::Client(_)
+                                       | SN::Server(_)
+                                       | SN::Common),
+
+      SKL::Limits         => matches!(s, SN::ServerLimit(_)
+                                       | SN::GlobalLimit),
+
+      SKL::ClientAgnostic => matches!(s, SN::Common
+                                       | SN::Server(_)),
+
+      SKL::Limited        => SKL::Ordinary.contains(s)
+                           | SKL::Limits  .contains(s),
+
+      SKL::ServerName     => matches!(s, SN::Common)
+                           | matches!(s, SN::Server(ServerName(name))
+                                         if name == "SERVER"),
+    }
+  }
+}
+
 impl<'c> ResolveContext<'c> {
-  fn first_of_raw(&self, key: &'static str, sections: &[SectionName])
+  fn first_of_raw(&self, key: &'static str, sections: SectionKindList)
                   -> Option<&'c RawVal> {
-    for section in sections {
+    for section in self.all_sections.iter()
+      .filter(|s| sections.contains(s))
+    {
       if let Some(raw) = self.agg.sections
         .get(section)
         .and_then(|vars: &SectionMap| vars.get(key))
@@ -307,7 +341,7 @@ impl<'c> ResolveContext<'c> {
   }
 
   #[throws(AE)]
-  fn first_of<T>(&self, key: &'static str, sections: &[SectionName])
+  fn first_of<T>(&self, key: &'static str, sections: SectionKindList)
                  -> Option<T>
   where T: Parseable
   {
@@ -326,7 +360,7 @@ impl<'c> ResolveContext<'c> {
   pub fn ordinary<T>(&self, key: &'static str) -> T
   where T: Parseable + Default
   {
-    self.first_of(key, &self.lookup_ordinary)?
+    self.first_of(key, SKL::Ordinary)?
       .unwrap_or_default()
   }
 
@@ -335,7 +369,7 @@ impl<'c> ResolveContext<'c> {
   where T: Parseable + Default + Ord
   {
     let val = self.ordinary(key)?;
-    if let Some(limit) = self.first_of(key, &self.lookup_limit)? {
+    if let Some(limit) = self.first_of(key, SKL::Limits)? {
       min(val, limit)
     } else {
       val
@@ -366,7 +400,7 @@ impl<'c> ResolveContext<'c> {
     match self.end {
       LinkEnd::Client => self.ordinary(key)?,
       LinkEnd::Server => {
-        self.first_of(key, &self.lookup_server_sections_only)?
+        self.first_of(key, SKL::ClientAgnostic)?
           .unwrap_or_default()
       },
     }
@@ -408,19 +442,13 @@ pub fn read() {
     agg: &agg,
     link: &link,
     end: LinkEnd::Server,
-    lookup_ordinary: vec![
+    all_sections: 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()),
-    ],
   };
 }
index 1f6a3cdb959468d31bf6e5c75d155423739bdaa9..42196f51c9f86875e35fc1388e36d832d5e5cbe3 100644 (file)
@@ -2,6 +2,11 @@
 // SPDX-License-Identifier: AGPL-3.0-or-later
 // There is NO WARRANTY.
 
+#![allow(unused_imports)] // xxx
+#![allow(unreachable_code)] // xxx
+#![allow(dead_code)] // xxx
+#![allow(unused_variables)] // xxx
+
 #![feature(io_error_more)] // EK::IsADirectory
 
 pub mod prelude;