mod model;
mod yaml;
use std::fmt;
use std::path::Path;
pub use model::{
AttributeValue, Config, ExporterConfig, Limits, LoggerProviderConfig, MeterProviderConfig,
OtlpHttpConfig, ProcessorConfig, ReaderConfig, ResourceConfig, SamplerConfig,
TracerProviderConfig,
};
#[derive(Clone, Debug, PartialEq)]
pub struct Parsed {
pub config: Config,
pub warnings: Vec<String>,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Error {
Io(String),
Parse {
line: usize,
column: usize,
message: String,
},
FileFormat(Option<String>),
Invalid {
path: String,
expected: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "couldn't read the configuration file: {e}"),
Error::Parse {
line,
column,
message,
} => write!(
f,
"configuration file, line {line}, column {column}: {message}"
),
Error::FileFormat(None) => write!(f, "the configuration file has no `file_format`"),
Error::FileFormat(Some(v)) => {
write!(f, "`file_format: {v}` isn't supported; oxtel reads 1.x")
}
Error::Invalid { path, expected } => {
write!(f, "`{path}` in the configuration file should be {expected}")
}
}
}
}
impl std::error::Error for Error {}
pub fn parse(text: &str) -> Result<Parsed, Error> {
parse_with(text, &|name| std::env::var(name).ok())
}
pub fn parse_with(text: &str, lookup: &dyn Fn(&str) -> Option<String>) -> Result<Parsed, Error> {
let root = yaml::parse(text, lookup)?;
let mut warnings = Vec::new();
let config = model::config(&root, &mut warnings)?;
Ok(Parsed { config, warnings })
}
pub fn from_file(path: impl AsRef<Path>) -> Result<Parsed, Error> {
let text = std::fs::read_to_string(path.as_ref())
.map_err(|e| Error::Io(format!("{}: {e}", path.as_ref().display())))?;
parse(&text)
}