Skip to main content

pgevolve_core/ir/
difference.rs

1//! Structured representation of one or more differences between two IR values.
2
3use serde::{Deserialize, Serialize};
4
5/// A single named difference between two IR values.
6///
7/// Fields capture the path to the differing position (e.g., a column name)
8/// and a JSON-encoded representation of the two values.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct Difference {
11    /// Dotted path to the differing field (e.g., `columns.email.ty`).
12    pub path: String,
13    /// Old value, as displayed (`Display` impl, not `Debug`).
14    pub from: String,
15    /// New value, as displayed.
16    pub to: String,
17}
18
19impl Difference {
20    /// Construct a `Difference` from displayable values.
21    pub fn new<F: std::fmt::Display, T: std::fmt::Display>(
22        path: impl Into<String>,
23        from: F,
24        to: T,
25    ) -> Self {
26        Self {
27            path: path.into(),
28            from: from.to_string(),
29            to: to.to_string(),
30        }
31    }
32
33    /// Prefix the path of this entry with the given prefix.
34    #[must_use]
35    pub fn prefix_path(self, prefix: &str) -> Self {
36        Self {
37            path: if self.path.is_empty() {
38                prefix.into()
39            } else {
40                format!("{prefix}.{}", self.path)
41            },
42            ..self
43        }
44    }
45}