Skip to main content

braze_sync/diff/
mod.rs

1//! Pure structural diff layer. No I/O.
2//!
3//! See IMPLEMENTATION.md §6.6 and §11. The shape of [`DiffOp`] and
4//! [`ResourceDiff`] is the central design contract: every diff site in the
5//! crate goes through these types so that adding a resource forces all
6//! match arms to be updated by the compiler.
7
8use crate::resource::ResourceKind;
9use similar::{ChangeTag, TextDiff};
10
11pub mod catalog;
12pub mod content_block;
13pub mod custom_attribute;
14pub mod email_template;
15pub mod orphan;
16
17#[derive(Debug, Clone)]
18pub struct TextDiffSummary {
19    pub additions: usize,
20    pub deletions: usize,
21}
22
23pub(crate) fn compute_text_diff(from: &str, to: &str) -> TextDiffSummary {
24    let diff = TextDiff::from_lines(from, to);
25    let mut additions = 0;
26    let mut deletions = 0;
27    for change in diff.iter_all_changes() {
28        match change.tag() {
29            ChangeTag::Insert => additions += 1,
30            ChangeTag::Delete => deletions += 1,
31            ChangeTag::Equal => {}
32        }
33    }
34    TextDiffSummary {
35        additions,
36        deletions,
37    }
38}
39
40/// Treats `None` and `Some("")` as equal — Braze may omit a field or
41/// return an empty string interchangeably.
42pub(crate) fn opt_str_eq(a: &Option<String>, b: &Option<String>) -> bool {
43    a.as_deref().unwrap_or("") == b.as_deref().unwrap_or("")
44}
45
46/// Multiset equality: same elements after sort, ignoring order.
47pub(crate) fn tags_eq_unordered(a: &[String], b: &[String]) -> bool {
48    if a.len() != b.len() {
49        return false;
50    }
51    let mut a: Vec<&str> = a.iter().map(String::as_str).collect();
52    let mut b: Vec<&str> = b.iter().map(String::as_str).collect();
53    a.sort_unstable();
54    b.sort_unstable();
55    a == b
56}
57
58/// A diff operation on a single entity. Polymorphic over the entity type so
59/// the same vocabulary applies to whole resources, individual fields, etc.
60#[derive(Debug, Clone, PartialEq)]
61pub enum DiffOp<T> {
62    Added(T),
63    Removed(T),
64    Modified { from: T, to: T },
65    Unchanged,
66}
67
68impl<T> DiffOp<T> {
69    pub fn is_change(&self) -> bool {
70        !matches!(self, Self::Unchanged)
71    }
72
73    pub fn is_destructive(&self) -> bool {
74        matches!(self, Self::Removed(_))
75    }
76}
77
78/// Per-resource-kind diff result.
79#[derive(Debug, Clone)]
80pub enum ResourceDiff {
81    CatalogSchema(catalog::CatalogSchemaDiff),
82    CatalogItems(catalog::CatalogItemsDiff),
83    ContentBlock(content_block::ContentBlockDiff),
84    EmailTemplate(email_template::EmailTemplateDiff),
85    CustomAttribute(custom_attribute::CustomAttributeDiff),
86}
87
88impl ResourceDiff {
89    pub fn kind(&self) -> ResourceKind {
90        match self {
91            Self::CatalogSchema(_) => ResourceKind::CatalogSchema,
92            Self::CatalogItems(_) => ResourceKind::CatalogItems,
93            Self::ContentBlock(_) => ResourceKind::ContentBlock,
94            Self::EmailTemplate(_) => ResourceKind::EmailTemplate,
95            Self::CustomAttribute(_) => ResourceKind::CustomAttribute,
96        }
97    }
98
99    pub fn name(&self) -> &str {
100        match self {
101            Self::CatalogSchema(d) => &d.name,
102            Self::CatalogItems(d) => &d.catalog_name,
103            Self::ContentBlock(d) => &d.name,
104            Self::EmailTemplate(d) => &d.name,
105            Self::CustomAttribute(d) => &d.name,
106        }
107    }
108
109    pub fn has_changes(&self) -> bool {
110        match self {
111            Self::CatalogSchema(d) => d.has_changes(),
112            Self::CatalogItems(d) => d.has_changes(),
113            Self::ContentBlock(d) => d.has_changes(),
114            Self::EmailTemplate(d) => d.has_changes(),
115            Self::CustomAttribute(d) => d.has_changes(),
116        }
117    }
118
119    pub fn has_destructive(&self) -> bool {
120        match self {
121            Self::CatalogSchema(d) => d.has_destructive(),
122            Self::CatalogItems(d) => d.has_destructive(),
123            // Content Block / Email Template have no DELETE API. "Destructive"
124            // for these resources is reframed as orphan tracking (§11.6); the
125            // apply path performs no destructive call.
126            Self::ContentBlock(_) => false,
127            Self::EmailTemplate(_) => false,
128            // Custom Attribute "removal" is only a deprecation flag toggle.
129            Self::CustomAttribute(_) => false,
130        }
131    }
132
133    pub fn is_orphan(&self) -> bool {
134        match self {
135            Self::ContentBlock(d) => d.is_orphan(),
136            Self::EmailTemplate(d) => d.is_orphan(),
137            _ => false,
138        }
139    }
140}
141
142#[derive(Debug, Clone, Default)]
143pub struct DiffSummary {
144    pub diffs: Vec<ResourceDiff>,
145}
146
147impl DiffSummary {
148    pub fn changed_count(&self) -> usize {
149        self.diffs.iter().filter(|d| d.has_changes()).count()
150    }
151
152    pub fn destructive_count(&self) -> usize {
153        self.diffs.iter().filter(|d| d.has_destructive()).count()
154    }
155
156    pub fn orphan_count(&self) -> usize {
157        self.diffs.iter().filter(|d| d.is_orphan()).count()
158    }
159
160    pub fn in_sync_count(&self) -> usize {
161        self.diffs.iter().filter(|d| !d.has_changes()).count()
162    }
163}