Skip to content

Commit 19d0fd5

Browse files
authored
do not clone Schema, clone only when the inner struct is Bool (#34)
1 parent af68efe commit 19d0fd5

File tree

2 files changed

+37
-55
lines changed

2 files changed

+37
-55
lines changed

src/diff_walker.rs

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ impl<F: FnMut(Change)> DiffWalker<F> {
3535
if let (Some(lhs_any_of), Some(rhs_any_of)) =
3636
(&mut lhs.subschemas().any_of, &mut rhs.subschemas().any_of)
3737
{
38-
match (lhs_any_of.len(), rhs_any_of.len()) {
39-
(l, r) if l <= r => {
40-
lhs_any_of.append(&mut vec![Schema::Bool(false); r - l]);
41-
}
42-
(l, r) => {
43-
rhs_any_of.append(&mut vec![Schema::Bool(false); l - r]);
44-
}
45-
}
4638
let max_len = lhs_any_of.len().max(rhs_any_of.len());
4739
lhs_any_of.resize(max_len, Schema::Bool(false));
4840
rhs_any_of.resize(max_len, Schema::Bool(false));
@@ -57,11 +49,7 @@ impl<F: FnMut(Change)> DiffWalker<F> {
5749
self.lhs_root.clone(),
5850
self.rhs_root.clone(),
5951
)
60-
.diff(
61-
"",
62-
&mut l.clone().into_object(),
63-
&mut r.clone().into_object(),
64-
)?;
52+
.diff("", l, r)?;
6553
mat[(i, j)] = count;
6654
}
6755
}
@@ -178,15 +166,11 @@ impl<F: FnMut(Change)> DiffWalker<F> {
178166
}
179167

180168
for common in rhs_props.intersection(&lhs_props) {
181-
let lhs_child = lhs.object().properties.get(common.as_str()).unwrap();
182-
let rhs_child = rhs.object().properties.get(common.as_str()).unwrap();
169+
let lhs_child = lhs.object().properties.get_mut(common.as_str()).unwrap();
170+
let rhs_child = rhs.object().properties.get_mut(common.as_str()).unwrap();
183171

184172
let new_path = format!("{json_path}.{common}");
185-
self.diff(
186-
&new_path,
187-
&mut lhs_child.clone().into_object(),
188-
&mut rhs_child.clone().into_object(),
189-
)?;
173+
self.diff(&new_path, lhs_child, rhs_child)?;
190174
}
191175

192176
Ok(())
@@ -198,17 +182,17 @@ impl<F: FnMut(Change)> DiffWalker<F> {
198182
lhs: &mut SchemaObject,
199183
rhs: &mut SchemaObject,
200184
) -> Result<(), Error> {
201-
if let (Some(ref lhs_additional_properties), Some(ref rhs_additional_properties)) = (
202-
&lhs.object().additional_properties,
203-
&rhs.object().additional_properties,
185+
if let (Some(lhs_additional_properties), Some(rhs_additional_properties)) = (
186+
&mut lhs.object().additional_properties,
187+
&mut rhs.object().additional_properties,
204188
) {
205189
if rhs_additional_properties != lhs_additional_properties {
206190
let new_path = format!("{json_path}.<additionalProperties>");
207191

208192
self.diff(
209193
&new_path,
210-
&mut lhs_additional_properties.clone().into_object(),
211-
&mut rhs_additional_properties.clone().into_object(),
194+
lhs_additional_properties,
195+
rhs_additional_properties,
212196
)?;
213197
}
214198
}
@@ -270,7 +254,7 @@ impl<F: FnMut(Change)> DiffWalker<F> {
270254
lhs: &mut SchemaObject,
271255
rhs: &mut SchemaObject,
272256
) -> Result<(), Error> {
273-
match (&lhs.array().items, &rhs.array().items) {
257+
match (&mut lhs.array().items, &mut rhs.array().items) {
274258
(Some(SingleOrVec::Vec(lhs_items)), Some(SingleOrVec::Vec(rhs_items))) => {
275259
if lhs_items.len() != rhs_items.len() {
276260
(self.cb)(Change {
@@ -282,23 +266,15 @@ impl<F: FnMut(Change)> DiffWalker<F> {
282266
}
283267

284268
for (i, (lhs_inner, rhs_inner)) in
285-
lhs_items.iter().zip(rhs_items.iter()).enumerate()
269+
lhs_items.iter_mut().zip(rhs_items.iter_mut()).enumerate()
286270
{
287271
let new_path = format!("{json_path}.{i}");
288-
self.diff(
289-
&new_path,
290-
&mut lhs_inner.clone().into_object(),
291-
&mut rhs_inner.clone().into_object(),
292-
)?;
272+
self.diff(&new_path, lhs_inner, rhs_inner)?;
293273
}
294274
}
295275
(Some(SingleOrVec::Single(lhs_inner)), Some(SingleOrVec::Single(rhs_inner))) => {
296276
let new_path = format!("{json_path}.?");
297-
self.diff(
298-
&new_path,
299-
&mut lhs_inner.clone().into_object(),
300-
&mut rhs_inner.clone().into_object(),
301-
)?;
277+
self.diff(&new_path, lhs_inner, rhs_inner)?;
302278
}
303279
(Some(SingleOrVec::Single(lhs_inner)), Some(SingleOrVec::Vec(rhs_items))) => {
304280
(self.cb)(Change {
@@ -308,13 +284,9 @@ impl<F: FnMut(Change)> DiffWalker<F> {
308284
},
309285
});
310286

311-
for (i, rhs_inner) in rhs_items.iter().enumerate() {
287+
for (i, rhs_inner) in rhs_items.iter_mut().enumerate() {
312288
let new_path = format!("{json_path}.{i}");
313-
self.diff(
314-
&new_path,
315-
&mut lhs_inner.clone().into_object(),
316-
&mut rhs_inner.clone().into_object(),
317-
)?;
289+
self.diff(&new_path, lhs_inner, rhs_inner)?;
318290
}
319291
}
320292
(Some(SingleOrVec::Vec(lhs_items)), Some(SingleOrVec::Single(rhs_inner))) => {
@@ -325,13 +297,9 @@ impl<F: FnMut(Change)> DiffWalker<F> {
325297
},
326298
});
327299

328-
for (i, lhs_inner) in lhs_items.iter().enumerate() {
300+
for (i, lhs_inner) in lhs_items.iter_mut().enumerate() {
329301
let new_path = format!("{json_path}.{i}");
330-
self.diff(
331-
&new_path,
332-
&mut lhs_inner.clone().into_object(),
333-
&mut rhs_inner.clone().into_object(),
334-
)?;
302+
self.diff(&new_path, lhs_inner, rhs_inner)?;
335303
}
336304
}
337305
(None, None) => (),
@@ -507,10 +475,24 @@ impl<F: FnMut(Change)> DiffWalker<F> {
507475
pub fn diff(
508476
&mut self,
509477
json_path: &str,
510-
lhs: &mut SchemaObject,
511-
rhs: &mut SchemaObject,
478+
lhs: &mut Schema,
479+
rhs: &mut Schema,
512480
) -> Result<(), Error> {
513-
self.do_diff(json_path, false, lhs, rhs)
481+
match (lhs, rhs) {
482+
(Schema::Object(lhs), Schema::Object(rhs)) => self.do_diff(json_path, false, lhs, rhs),
483+
(bool_lhs, Schema::Object(rhs)) => {
484+
self.do_diff(json_path, false, &mut bool_lhs.clone().into_object(), rhs)
485+
}
486+
(Schema::Object(lhs), bool_rhs) => {
487+
self.do_diff(json_path, false, lhs, &mut bool_rhs.clone().into_object())
488+
}
489+
(bool_lhs, bool_rhs) => self.do_diff(
490+
json_path,
491+
false,
492+
&mut bool_lhs.clone().into_object(),
493+
&mut bool_rhs.clone().into_object(),
494+
),
495+
}
514496
}
515497
}
516498

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![doc = include_str!("../README.md")]
22
#![warn(missing_docs)]
33

4-
use schemars::schema::RootSchema;
4+
use schemars::schema::{RootSchema, Schema};
55
use serde_json::Value;
66
use thiserror::Error;
77

@@ -27,8 +27,8 @@ pub fn diff(lhs: Value, rhs: Value) -> Result<Vec<Change>, Error> {
2727
);
2828
walker.diff(
2929
"",
30-
&mut walker.lhs_root.schema.clone(),
31-
&mut walker.rhs_root.schema.clone(),
30+
&mut Schema::Object(walker.lhs_root.schema.clone()),
31+
&mut Schema::Object(walker.rhs_root.schema.clone()),
3232
)?;
3333
Ok(changes)
3434
}

0 commit comments

Comments
 (0)