@@ -166,40 +166,43 @@ impl<'a> DisplayTree<'a> {
166
166
167
167
/// Display `self` and `other`, highlighting differences.
168
168
pub fn diff_display ( & self , other : & Self ) -> ( String , String ) {
169
+ let ( left, right, _) = self . diff_display_has_diff ( other) ;
170
+ ( left, right)
171
+ }
172
+
173
+ /// Display `self` and `other`, highlighting differences. Returns whether there was any diff.
174
+ pub fn diff_display_has_diff ( & self , other : & Self ) -> ( String , String , bool ) {
169
175
let mut left = String :: new ( ) ;
170
176
let mut right = String :: new ( ) ;
171
- let _ = self . diff_display_inner ( other, & mut left, & mut right) ;
172
- ( left, right)
177
+ let has_diff = self
178
+ . diff_display_inner ( other, & mut left, & mut right)
179
+ . unwrap ( ) ;
180
+ ( left, right, has_diff)
173
181
}
174
182
183
+ /// Returns whether there was any diff.
175
184
fn diff_display_inner (
176
185
& self ,
177
186
other : & Self ,
178
187
left : & mut String ,
179
188
right : & mut String ,
180
- ) -> std:: fmt:: Result {
189
+ ) -> Result < bool , std:: fmt:: Error > {
181
190
use std:: fmt:: Write ;
182
191
// The trivial cases: the trees are either fully identical or fully different.
183
192
let all_same = |left : & mut String , right : & mut String | {
184
193
write ! ( left, "{self}" ) ?;
185
194
write ! ( right, "{other}" ) ?;
186
- Ok ( ( ) )
195
+ Ok ( false )
187
196
} ;
188
197
let all_different = |left : & mut String , right : & mut String | {
189
198
write ! ( left, "{}" , self . to_string( ) . red( ) ) ?;
190
199
write ! ( right, "{}" , other. to_string( ) . green( ) ) ?;
191
- Ok ( ( ) )
200
+ Ok ( true )
192
201
} ;
193
202
match ( self . kind , other. kind ) {
194
- _ if self . tag != other. tag => {
195
- all_different ( left, right) ?;
196
- }
197
- _ if self . ignore_for_diff && other. ignore_for_diff => {
198
- all_same ( left, right) ?;
199
- }
200
- ( Leaf ( l) , Leaf ( r) ) if strip_markup ( l) == strip_markup ( r) => {
201
- all_same ( left, right) ?;
202
- }
203
+ _ if self . tag != other. tag => all_different ( left, right) ,
204
+ _ if self . ignore_for_diff && other. ignore_for_diff => all_same ( left, right) ,
205
+ ( Leaf ( l) , Leaf ( r) ) if strip_markup ( l) == strip_markup ( r) => all_same ( left, right) ,
203
206
// The non-trivial case: the trees differ partially.
204
207
(
205
208
Separated { sep, children : c1 } ,
@@ -209,20 +212,19 @@ impl<'a> DisplayTree<'a> {
209
212
} ,
210
213
) if strip_markup ( sep) == strip_markup ( sep2) && c1. len ( ) == c2. len ( ) => {
211
214
let mut is_first = true ;
215
+ let mut any_diff = false ;
212
216
for ( c1, c2) in c1. iter ( ) . zip ( c2) {
213
217
if !is_first {
214
218
write ! ( left, "{sep}" ) ?;
215
219
write ! ( right, "{sep}" ) ?;
216
220
}
217
- c1. diff_display_inner ( c2, left, right) ?;
221
+ any_diff |= c1. diff_display_inner ( c2, left, right) ?;
218
222
is_first = false ;
219
223
}
224
+ Ok ( any_diff)
220
225
}
221
- _ => {
222
- all_different ( left, right) ?;
223
- }
226
+ _ => all_different ( left, right) ,
224
227
}
225
- Ok ( ( ) )
226
228
}
227
229
}
228
230
0 commit comments