use serde_json::Value;
use crate::genome::migration::MigrationError;
use crate::genome::schema::GenomeSchemaVersion;
pub mod v3;
pub use v3::V3Normalizer;
#[derive(Debug, Clone)]
pub struct NormalizationDiagnostics {
pub schema_version: GenomeSchemaVersion,
pub transformations: Vec<String>,
}
impl NormalizationDiagnostics {
pub fn new(schema_version: GenomeSchemaVersion) -> Self {
Self {
schema_version,
transformations: Vec::new(),
}
}
pub fn record(&mut self, msg: impl Into<String>) {
self.transformations.push(msg.into());
}
pub fn is_clean(&self) -> bool {
self.transformations.is_empty()
}
}
pub trait Normalizer: Send + Sync {
fn schema_version(&self) -> GenomeSchemaVersion;
fn name(&self) -> &'static str;
fn normalize(&self, genome: &mut Value) -> Result<NormalizationDiagnostics, MigrationError>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn diagnostics_starts_clean() {
let d = NormalizationDiagnostics::new(GenomeSchemaVersion(3));
assert!(d.is_clean());
assert_eq!(d.transformations.len(), 0);
assert_eq!(d.schema_version, GenomeSchemaVersion(3));
}
#[test]
fn record_breaks_clean() {
let mut d = NormalizationDiagnostics::new(GenomeSchemaVersion(3));
d.record("set width 0 -> 1");
assert!(!d.is_clean());
assert_eq!(d.transformations, vec!["set width 0 -> 1".to_string()]);
}
}