use serde_json::Value;
pub struct DiffTree {
pub roots: Vec<DiffNode>,
}
impl DiffTree {
pub fn is_empty(&self) -> bool {
self.roots.is_empty()
}
}
pub enum ChildKind {
Fields,
Items,
}
impl DiffNode {
pub fn leaf(segment: PathSegment, kind: DiffKind) -> Self {
Self::Leaf { segment, kind }
}
pub fn container(
segment: PathSegment,
child_kind: ChildKind,
omitted_count: u16,
children: Vec<DiffNode>,
) -> Self {
Self::Container {
segment,
child_kind,
omitted_count,
children,
}
}
}
pub enum DiffNode {
Container {
segment: PathSegment,
child_kind: ChildKind,
omitted_count: u16,
children: Vec<DiffNode>,
},
Leaf {
segment: PathSegment,
kind: DiffKind,
},
}
impl DiffKind {
pub fn changed(actual: Value, expected: Value) -> Self {
Self::Changed { actual, expected }
}
pub fn missing(expected: Value) -> Self {
Self::Missing { expected }
}
pub fn type_mismatch(
actual: Value,
actual_type: &'static str,
expected: Value,
expected_type: &'static str,
) -> Self {
Self::TypeMismatch {
actual,
actual_type,
expected,
expected_type,
}
}
}
pub enum DiffKind {
Changed {
actual: Value,
expected: Value,
},
Missing {
expected: Value,
},
TypeMismatch {
actual: Value,
actual_type: &'static str,
expected: Value,
expected_type: &'static str,
},
}
impl PathSegment {
pub fn is_array(&self) -> bool {
matches!(
self,
PathSegment::NamedElement { .. } | PathSegment::Index(_) | PathSegment::Unmatched
)
}
}
pub enum PathSegment {
Key(String),
NamedElement {
match_key: String,
match_value: String,
},
Index(u16),
Unmatched,
}