Skip to content

avoid recursion #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,34 @@ fn diff_with<'a>(
rhs,
path,
acc,
queue: &mut Vec::new(),
config,
};

fold_json(lhs, &mut folder);
while let Some(e) = folder.queue.pop() {
folder.rhs = e.rhs;
folder.path = e.path.clone();
fold_json(e.lhs, &mut folder);
}
}

#[derive(Debug)]
struct DiffFolder<'a, 'b> {
rhs: &'a Value,
path: Path<'a>,
acc: &'b mut Vec<Difference<'a>>,
queue: &'b mut Vec<QueuedItem<'a>>,
config: Config,
}

#[derive(Debug)]
struct QueuedItem<'a> {
lhs: &'a Value,
rhs: &'a Value,
path: Path<'a>,
}

macro_rules! direct_compare {
($name:ident) => {
fn $name(&mut self, lhs: &'a Value) {
Expand All @@ -54,6 +68,10 @@ impl<'a, 'b> DiffFolder<'a, 'b> {
direct_compare!(on_bool);
direct_compare!(on_string);

fn queue(&mut self, lhs: &'a Value, rhs: &'a Value, path: Path<'a>) {
self.queue.push(QueuedItem { lhs, rhs, path });
}

fn on_number(&mut self, lhs: &'a Value) {
let is_equal = match self.config.numeric_mode {
NumericMode::Strict => self.rhs == lhs,
Expand All @@ -79,7 +97,7 @@ impl<'a, 'b> DiffFolder<'a, 'b> {
let path = self.path.append(Key::Idx(idx));

if let Some(lhs) = lhs.get(idx) {
diff_with(lhs, rhs, self.config.clone(), path, self.acc)
self.queue(lhs, rhs, path);
} else {
self.acc.push(Difference {
lhs: None,
Expand All @@ -101,7 +119,7 @@ impl<'a, 'b> DiffFolder<'a, 'b> {

match (lhs.get(key), rhs.get(key)) {
(Some(lhs), Some(rhs)) => {
diff_with(lhs, rhs, self.config.clone(), path, self.acc);
self.queue(lhs, rhs, path);
}
(None, Some(rhs)) => {
self.acc.push(Difference {
Expand Down Expand Up @@ -146,7 +164,7 @@ impl<'a, 'b> DiffFolder<'a, 'b> {
let path = self.path.append(Key::Field(key));

if let Some(lhs) = lhs.get(key) {
diff_with(lhs, rhs, self.config.clone(), path, self.acc)
self.queue(lhs, rhs, path);
} else {
self.acc.push(Difference {
lhs: None,
Expand All @@ -164,7 +182,7 @@ impl<'a, 'b> DiffFolder<'a, 'b> {

match (lhs.get(key), rhs.get(key)) {
(Some(lhs), Some(rhs)) => {
diff_with(lhs, rhs, self.config.clone(), path, self.acc);
self.queue(lhs, rhs, path);
}
(None, Some(rhs)) => {
self.acc.push(Difference {
Expand Down