use plugx_input::Input;
use std::fmt::Debug;
use thiserror::Error;
#[cfg(feature = "env")]
pub mod env;
#[cfg(feature = "json")]
pub mod json;
#[cfg(feature = "toml")]
pub mod toml;
#[cfg(feature = "yaml")]
pub mod yaml;
pub mod closure;
#[derive(Debug, Error)]
pub enum ConfigurationParserError {
#[error("{parser} with supported formats {supported_format_list:?} could not parse `{data}`")]
Parse {
data: String,
parser: String,
supported_format_list: Vec<String>,
source: anyhow::Error,
},
#[error("Could not found parser to parse format `{format}`")]
ParserNotFound { format: String },
}
pub trait ConfigurationParser: Send + Sync + Debug {
fn name(&self) -> String;
fn supported_format_list(&self) -> Vec<String>;
fn try_parse(&self, bytes: &[u8]) -> anyhow::Result<Input>;
fn is_format_supported(&self, bytes: &[u8]) -> Option<bool>;
fn parse(&self, bytes: &[u8]) -> Result<Input, ConfigurationParserError> {
self.try_parse(bytes)
.map_err(|source| ConfigurationParserError::Parse {
data: String::from_utf8_lossy(bytes).to_string(),
parser: self.name(),
supported_format_list: self.supported_format_list(),
source,
})
}
}