use std::borrow::Cow;
use std::fmt;
#[derive(Debug, Clone)]
pub struct Location {
pub file: &'static str,
pub line: u32,
}
impl Location {
#[must_use]
pub const fn new(file: &'static str, line: u32) -> Self {
Self { file, line }
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.file, self.line)
}
}
#[derive(Debug, Clone)]
pub struct EnrichmentEntry {
pub message: Cow<'static, str>,
pub location: Location,
}
impl EnrichmentEntry {
pub fn new(message: impl Into<Cow<'static, str>>, file: &'static str, line: u32) -> Self {
Self {
message: message.into(),
location: Location::new(file, line),
}
}
}
impl fmt::Display for EnrichmentEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (at {})", self.message, self.location)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_enrichment_entry() {
let ctx = EnrichmentEntry::new("enrichment message", "main.rs", 42);
assert_eq!(ctx.to_string(), "enrichment message (at main.rs:42)");
assert_eq!(ctx.location.file, "main.rs");
assert_eq!(ctx.location.line, 42);
}
}