Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): config files are now kdl-based
Fixes: #153
- Loading branch information
Showing
6 changed files
with
94 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use miette::Diagnostic; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error, Diagnostic)] | ||
pub enum OroConfigError { | ||
#[error(transparent)] | ||
#[diagnostic(code(config::error))] | ||
ConfigError(#[from] config::ConfigError), | ||
|
||
#[error(transparent)] | ||
#[diagnostic(code(config::error))] | ||
ConfigParseError(#[from] Box<dyn std::error::Error + Send + Sync>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use config::{ConfigError, FileStoredFormat, Format, Map, Source, Value, ValueKind}; | ||
use kdl::{KdlDocument, KdlValue}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct KdlSource(KdlDocument); | ||
|
||
impl Source for KdlSource { | ||
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> { | ||
Box::new(self.clone()) | ||
} | ||
|
||
fn collect(&self) -> Result<Map<String, Value>, ConfigError> { | ||
let mut map = Map::new(); | ||
for node in self.0.nodes() { | ||
let key = node.name().to_string(); | ||
if let Some(value) = node.get(0) { | ||
let value = Value::new( | ||
Some(&if let Some(str) = value.as_string() { | ||
str.to_owned() | ||
} else { | ||
value.to_string() | ||
}), | ||
value_kind(value), | ||
); | ||
map.insert(key, value); | ||
} | ||
} | ||
Ok(map) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct KdlFormat; | ||
|
||
impl Format for KdlFormat { | ||
fn parse( | ||
&self, | ||
_uri: Option<&String>, | ||
text: &str, | ||
) -> Result<Map<String, Value>, Box<dyn std::error::Error + Send + Sync>> { | ||
Ok(KdlSource(text.parse()?).collect()?) | ||
} | ||
} | ||
|
||
impl FileStoredFormat for KdlFormat { | ||
fn file_extensions(&self) -> &'static [&'static str] { | ||
&["kdl"] | ||
} | ||
} | ||
|
||
fn value_kind(value: &KdlValue) -> ValueKind { | ||
if let Some(str) = value.as_string() { | ||
ValueKind::String(str.into()) | ||
} else if let Some(num) = value.as_i64() { | ||
ValueKind::I64(num) | ||
} else if let Some(float) = value.as_f64() { | ||
ValueKind::Float(float) | ||
} else if let Some(boolean) = value.as_bool() { | ||
ValueKind::Boolean(boolean) | ||
} else { | ||
ValueKind::Nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters