Skip to content

Commit

Permalink
Add config changes diff (#913)
Browse files Browse the repository at this point in the history
Add a binding function for diffing the configuration between two system
configurations.

The initial implementation here is for API and basic functionality to
support #909. There are improvements that will follow to eliminate false
positives.
  • Loading branch information
jw3 authored Sep 21, 2023
1 parent 4d08e25 commit 7014fa3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/pyo3/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ impl PySystem {
}
}

// todo;; this will become more advanced and based on the config object rather than text
#[pyfunction]
fn config_difference(lhs: &PySystem, rhs: &PySystem) -> String {
log::debug!("config_difference");

let ltxt = lhs.config_text();
let rtxt = rhs.config_text();
let diff = TextDiff::from_lines(&ltxt, &rtxt);

let mut diff_lines = vec![];
for line in diff.iter_all_changes() {
let sign = match line.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};
diff_lines.push(format!("{}{}", sign, line));
}
diff_lines.join("")
}

// todo;; this should become more advanced and be based on rule db rather than text
#[pyfunction]
fn rules_difference(lhs: &PySystem, rhs: &PySystem) -> String {
log::debug!("rules_difference");
Expand Down Expand Up @@ -228,6 +250,7 @@ fn checked_system(py: Python) -> PyResult<PySystem> {

pub fn init_module(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PySystem>()?;
m.add_function(wrap_pyfunction!(config_difference, m)?)?;
m.add_function(wrap_pyfunction!(rules_difference, m)?)?;
m.add_function(wrap_pyfunction!(checked_system, m)?)?;
Ok(())
Expand Down
2 changes: 2 additions & 0 deletions examples/change_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
s2 = s1.apply_config_changes(xs1)

print(s2.config_text())

print(config_difference(s1, s2))

0 comments on commit 7014fa3

Please sign in to comment.