use std::path::Path;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PathRecording {
#[default]
Off,
Relative,
Absolute,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct MetricsConfig {
pub record_paths: PathRecording,
}
#[must_use]
pub fn record_path(mode: PathRecording, rel: &str, absolute: &Path) -> Option<String> {
match mode {
PathRecording::Off => None,
PathRecording::Relative => Some(rel.to_string()),
PathRecording::Absolute => Some(absolute.display().to_string()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn off_is_the_default_and_records_nothing() {
assert_eq!(PathRecording::default(), PathRecording::Off);
assert_eq!(
record_path(PathRecording::Off, "src/a.rs", Path::new("/repo/src/a.rs")),
None,
"the default must reproduce the pre-#77 record exactly"
);
}
#[test]
fn relative_and_absolute_record_their_own_form() {
let abs = Path::new("/home/agent/repo/src/a.rs");
assert_eq!(
record_path(PathRecording::Relative, "src/a.rs", abs),
Some("src/a.rs".to_string())
);
assert_eq!(
record_path(PathRecording::Absolute, "src/a.rs", abs),
Some("/home/agent/repo/src/a.rs".to_string())
);
}
#[test]
fn record_paths_parses_from_config_toml() {
let cfg: MetricsConfig = toml::from_str("record_paths = \"relative\"").unwrap();
assert_eq!(cfg.record_paths, PathRecording::Relative);
let empty: MetricsConfig = toml::from_str("").unwrap();
assert_eq!(empty.record_paths, PathRecording::Off, "absent = off");
}
}