use std::fmt::{self, Display, Formatter};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum SourceKind {
Default,
File,
Environment,
Arguments,
Normalization,
Custom,
}
impl Display for SourceKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Default => write!(f, "default"),
Self::File => write!(f, "file"),
Self::Environment => write!(f, "env"),
Self::Arguments => write!(f, "cli"),
Self::Normalization => write!(f, "normalize"),
Self::Custom => write!(f, "custom"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SourceTrace {
pub kind: SourceKind,
pub name: String,
pub location: Option<String>,
}
impl SourceTrace {
pub(super) fn new(kind: SourceKind, name: impl Into<String>) -> Self {
Self {
kind,
name: name.into(),
location: None,
}
}
}
impl Display for SourceTrace {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.location {
Some(location) if self.name.is_empty() => write!(f, "{}({location})", self.kind),
Some(location) => write!(f, "{}({}:{location})", self.kind, self.name),
None if self.name.is_empty() => write!(f, "{}", self.kind),
None => write!(f, "{}({})", self.kind, self.name),
}
}
}