chiark / gitweb /
config: Provide end in Aggregate and pass to contains()
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 18:08:41 +0000 (19:08 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 18:46:01 +0000 (19:46 +0100)
Nothing uses this yet.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/config.rs

index 1dab223469242be67c2a824ff4aa7cfcf9305179..b99d065671c7009474f479e8be5e91f52e3f4dae 100644 (file)
@@ -163,6 +163,7 @@ static SPECIAL_SERVER_SECTION: &str = "SERVER";
 
 #[derive(Debug)]
 struct Aggregate {
+  end: LinkEnd,
   keys_allowed: HashMap<&'static str, SectionKindList>,
   sections: HashMap<SectionName, SectionMap>,
 }
@@ -222,9 +223,10 @@ impl Display for SectionName {
 
 impl Aggregate {
   fn new(
+    end: LinkEnd,
     keys_allowed: HashMap<&'static str, SectionKindList>
   ) -> Self { Aggregate {
-    keys_allowed,
+    end, keys_allowed,
     sections: default(),
   } }
 
@@ -267,7 +269,7 @@ impl Aggregate {
             || anyhow!("unknown configuration key {:?}", key)
           )?
         };
-        if ! skl.contains(&sn) {
+        if ! skl.contains(&sn, self.end) {
           throw!(anyhow!("configuration key {:?} not applicable \
                           in this kind of section: {}", key, &sn))
         }
@@ -536,26 +538,26 @@ impl SectionName {
 }
 
 impl SectionKindList {
-  fn contains(self, s: &SectionName) -> bool {
-    match self {
-      SKL::PerClient      => matches!(s, SN::Link(_)
+  fn contains(self, s: &SectionName, end: LinkEnd) -> bool {
+    match (self, end) {
+      (SKL::PerClient,_)  => matches!(s, SN::Link(_)
                                        | SN::Client(_)
                                        | SN::Server(_)
                                        | SN::Common),
 
-      SKL::Limits         => matches!(s, SN::ServerLimit(_)
+      (SKL::Limits,_)     => matches!(s, SN::ServerLimit(_)
                                        | SN::GlobalLimit),
 
-      SKL::Global         => matches!(s, SN::Common
-                                       | SN::Server(_)),
+      (SKL::Global,_)         => matches!(s, SN::Common
+                                           | SN::Server(_)),
 
-      SKL::Limited        => SKL::PerClient.contains(s)
-                           | SKL::Limits   .contains(s),
+      (SKL::Limited,_)    => SKL::PerClient.contains(s, end)
+                           | SKL::Limits   .contains(s, end),
 
-      SKL::ServerName     => matches!(s, SN::Common)
+      (SKL::ServerName,_) => matches!(s, SN::Common)
                            | matches!(s, SN::Server(ServerName(name))
                                          if name == SPECIAL_SERVER_SECTION),
-      SKL::None           => false,
+      (SKL::None,_)       => false,
     }
   }
 }
@@ -600,7 +602,7 @@ impl<'c> ResolveContext<'c> {
     self.agg.lookup_raw(
       key,
       self.all_sections.iter()
-        .filter(|s| sections.contains(s))
+        .filter(|s| sections.contains(s, self.end))
     )
   }
 
@@ -822,6 +824,7 @@ impl InstanceConfig {
 pub fn read(opts: &Opts, end: LinkEnd) -> Vec<InstanceConfig> {
   let agg = (||{
     let mut agg = Aggregate::new(
+      end,
       InstanceConfig::FIELDS.iter().cloned().collect(),
     );