use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Provenance {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub module: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thread: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pid: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub service: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub host: Option<String>,
}
impl Provenance {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
#[track_caller]
pub fn capture() -> Self {
let location = std::panic::Location::caller();
Self {
file: Some(location.file().to_string()),
line: Some(location.line()),
module: None,
thread: std::thread::current().name().map(ToString::to_string),
pid: Some(std::process::id()),
..Self::default()
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
#[must_use]
pub fn with_file<S: Into<String>>(mut self, file: S) -> Self {
self.file = Some(file.into());
self
}
#[must_use]
pub const fn with_line(mut self, line: u32) -> Self {
self.line = Some(line);
self
}
#[must_use]
pub fn with_module<S: Into<String>>(mut self, module: S) -> Self {
self.module = Some(module.into());
self
}
#[must_use]
pub fn with_service<S: Into<String>>(mut self, service: S) -> Self {
self.service = Some(service.into());
self
}
#[must_use]
pub fn with_version<S: Into<String>>(mut self, version: S) -> Self {
self.version = Some(version.into());
self
}
#[must_use]
pub fn with_host<S: Into<String>>(mut self, host: S) -> Self {
self.host = Some(host.into());
self
}
#[must_use]
pub fn or_fill_from(mut self, other: &Self) -> Self {
if self.file.is_none() {
self.file.clone_from(&other.file);
}
if self.line.is_none() {
self.line = other.line;
}
if self.module.is_none() {
self.module.clone_from(&other.module);
}
if self.thread.is_none() {
self.thread.clone_from(&other.thread);
}
if self.pid.is_none() {
self.pid = other.pid;
}
if self.service.is_none() {
self.service.clone_from(&other.service);
}
if self.version.is_none() {
self.version.clone_from(&other.version);
}
if self.host.is_none() {
self.host.clone_from(&other.host);
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_capture_records_the_call_site() {
let provenance = Provenance::capture();
assert_eq!(
provenance.file.as_deref(),
Some(file!()),
"capture must report the caller's file, not one inside this crate"
);
assert!(provenance.line.is_some());
assert_eq!(provenance.pid, Some(std::process::id()));
}
#[test]
fn test_empty_provenance_serializes_to_an_empty_object() {
let json = serde_json::to_string(&Provenance::new()).expect("serializes");
assert_eq!(json, "{}", "unset provenance must not bloat a log line");
assert!(Provenance::new().is_empty());
}
#[test]
fn test_or_fill_from_does_not_overwrite() {
let base = Provenance::new()
.with_service("auth-api")
.with_host("node-1")
.with_file("base.rs");
let merged = Provenance::new()
.with_file("call-site.rs")
.or_fill_from(&base);
assert_eq!(
merged.file.as_deref(),
Some("call-site.rs"),
"an established field wins over the fallback"
);
assert_eq!(merged.service.as_deref(), Some("auth-api"));
assert_eq!(merged.host.as_deref(), Some("node-1"));
}
#[test]
fn test_round_trips() {
let provenance = Provenance::capture()
.with_module("my_app::auth")
.with_service("auth-api")
.with_version("1.2.3");
let json = serde_json::to_string(&provenance).expect("serializes");
let parsed: Provenance = serde_json::from_str(&json).expect("deserializes");
assert_eq!(parsed, provenance);
}
#[test]
fn test_missing_provenance_deserializes() {
let parsed: Provenance = serde_json::from_str("{}").expect("deserializes");
assert!(parsed.is_empty());
}
}