Skip to content

Commit

Permalink
fix(conf): do not allow duplicate fields
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Feb 24, 2023
1 parent 97605c2 commit 22e34cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 20 additions & 2 deletions conf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,35 @@ impl CLNConf {
parser::Parser::new(&self.path, self.create_if_missing)
}

pub fn add_conf(&mut self, key: &str, val: &str) {
pub fn add_conf(&mut self, key: &str, val: &str) -> Result<(), ParsingError> {
if self.fields.contains_key(key) {
let values = self.fields.get_mut(key).unwrap();
for value in values.iter() {
if val == value {
return Err(ParsingError {
core: 2,
cause: format!("field {key} with value {val} already present"),
});
}
}
values.push(val.to_owned());
} else {
self.fields.insert(key.to_owned(), vec![val.to_owned()]);
}
Ok(())
}

pub fn add_subconf(&mut self, conf: CLNConf) {
pub fn add_subconf(&mut self, conf: CLNConf) -> Result<(), ParsingError> {
for subconf in &self.includes {
if conf.path == subconf.path {
return Err(ParsingError {
core: 2,
cause: format!("duplicate include {}", conf.path),
});
}
}
self.includes.push(conf.into());
Ok(())
}

pub fn flush(&self) -> Result<(), std::io::Error> {
Expand Down
6 changes: 3 additions & 3 deletions conf/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Parser {
let key = stream.advance().to_owned();
if key.starts_with("comment") {
let value = key.strip_prefix("comment ").unwrap().trim();
conf.add_conf(&key, value);
conf.add_conf(&key, value)?;
return Ok(());
}
if key.starts_with("include") {
Expand All @@ -94,10 +94,10 @@ impl Parser {
if let Err(err) = subconf.parse() {
return Err(err);
}
conf.add_subconf(subconf);
conf.add_subconf(subconf)?;
} else {
let value = stream.advance().to_owned();
conf.add_conf(&key, &value);
conf.add_conf(&key, &value)?;
}
Ok(())
}
Expand Down

0 comments on commit 22e34cc

Please sign in to comment.