use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::Deserialize;
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct LoggingConfig {
#[serde(default, rename = "system_log")]
pub _system_log: Option<Ignored>,
#[serde(default)]
pub access_log: Option<String>,
#[serde(default)]
pub channels: BTreeMap<String, ChannelConfig>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Ignored;
impl<'de> Deserialize<'de> for Ignored {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D
) -> Result<Self, D::Error> {
serde::de::IgnoredAny::deserialize(deserializer).map(|_| Self)
}
}
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(tag = "adapter", rename_all = "snake_case", deny_unknown_fields)]
pub enum ChannelConfig {
File {
path: PathBuf,
format: FileFormat,
#[serde(default, alias = "truncate")]
truncate_on_startup: bool,
},
Otlp {
endpoint: String,
queue: usize,
},
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FileFormat {
Json,
Line,
}
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("{path}: more channels than a record can name ({count})")]
TooManyChannels { path: PathBuf, count: usize },
#[error(
"{path}: access_log names '{channel}', which is not a declared channel"
)]
UnknownAccessChannel { path: PathBuf, channel: String },
}
impl LoggingConfig {
pub fn validate(
&self,
path: &Path,
) -> Result<(), ConfigError> {
if let Some(channel) = &self.access_log
&& !self.channels.contains_key(channel)
{
return Err(ConfigError::UnknownAccessChannel {
path: path.to_owned(),
channel: channel.clone(),
});
}
Ok(())
}
}