Skip to content

Commit

Permalink
Merge #99: fix(conf): fix the ambigity when we get a list of confs
Browse files Browse the repository at this point in the history
00968b6 fix(conf): fix the ambigity when we get a list of confs (Vincenzo Palazzo)

Pull request description:

Top commit has no ACKs.

Tree-SHA512: a35fd09e66afa17f22012f817a6077c3422fc6159224b4eaf318b059340a8a1dbf2ca9d97cb0cd5dc9c38122db78fc5936d6074c1ca0fe3d6f6b747b5ecdd816
  • Loading branch information
vincenzopalazzo committed May 23, 2023
2 parents af940d0 + 00968b6 commit e266f63
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
6 changes: 6 additions & 0 deletions conf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.0.3

## Fixed
- fix the ambigity when we get a list of confs ([commit](https://github.com/laanwj/cln4rust/commit/4e32e0a4c7d8c9e97c552d632fc04696972fdee3)). @vincenzopalazzo 23-05-2023


# v0.0.2

## Fixed
Expand Down
2 changes: 1 addition & 1 deletion conf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clightningrpc-conf"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
authors = ["Vincenzo Palazzo <[email protected]>"]
license = "CC0-1.0"
Expand Down
4 changes: 2 additions & 2 deletions conf/changelog.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"package_name": "conf",
"version": "v0.0.2",
"version": "v0.0.3",
"api": {
"name": "github",
"repository": "laanwj/rust-clightning-rpc",
"branch": "master"
"branch": "macros/conf-fix"
},
"generation_method": {
"name": "semver-v2",
Expand Down
44 changes: 41 additions & 3 deletions conf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod parser;

use file::{File, SyncFile};

#[derive(Debug)]
pub struct ParsingError {
pub core: u64,
pub cause: String,
Expand All @@ -22,6 +23,12 @@ impl From<io::Error> for ParsingError {
}
}

impl std::fmt::Display for ParsingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.cause)
}
}

pub trait SyncCLNConf {
fn parse(&mut self) -> Result<(), ParsingError>;
}
Expand Down Expand Up @@ -78,17 +85,48 @@ impl CLNConf {
Ok(())
}

pub fn get_conf(&self, key: &str) -> Option<Vec<String>> {
/// Get a unique field with the specified key, if there are multiple definition
/// the function return an error.
///
/// In the case of multiple definition of the same key you would like to use `get_confs`.
pub fn get_conf(&self, key: &str) -> Result<Option<String>, ParsingError> {
let mut results = vec![];
if let Some(fields) = self.fields.get(key) {
results.append(&mut fields.clone());
}
for include in &self.includes {
let fields = include.get_confs(key);
if !fields.is_empty() {
results.append(&mut fields.clone());
}
}
if results.is_empty() {
return Ok(None);
}

if results.len() > 1 {
return Err(ParsingError {
core: 1,
cause: "mutiple field with the `{key}`".to_owned(),
});
}
Ok(Some(results.first().unwrap().clone()))
}

/// Return a list of values with the specified key, if no
/// item is found, return an empity vector.
pub fn get_confs(&self, key: &str) -> Vec<String> {
let mut results = vec![];
if let Some(fields) = self.fields.get(key) {
results.append(&mut fields.clone());
}
for include in &self.includes {
if let Some(fields) = include.get_conf(key) {
let fields = include.get_confs(key);
if !fields.is_empty() {
results.append(&mut fields.clone());
}
}
Some(results)
results
}

pub fn add_subconf(&mut self, conf: CLNConf) -> Result<(), ParsingError> {
Expand Down

0 comments on commit e266f63

Please sign in to comment.