@@ -166,40 +166,43 @@ impl<'a> DisplayTree<'a> {
166166
167167 /// Display `self` and `other`, highlighting differences.
168168 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 ) {
169175 let mut left = String :: new ( ) ;
170176 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)
173181 }
174182
183+ /// Returns whether there was any diff.
175184 fn diff_display_inner (
176185 & self ,
177186 other : & Self ,
178187 left : & mut String ,
179188 right : & mut String ,
180- ) -> std:: fmt:: Result {
189+ ) -> Result < bool , std:: fmt:: Error > {
181190 use std:: fmt:: Write ;
182191 // The trivial cases: the trees are either fully identical or fully different.
183192 let all_same = |left : & mut String , right : & mut String | {
184193 write ! ( left, "{self}" ) ?;
185194 write ! ( right, "{other}" ) ?;
186- Ok ( ( ) )
195+ Ok ( false )
187196 } ;
188197 let all_different = |left : & mut String , right : & mut String | {
189198 write ! ( left, "{}" , self . to_string( ) . red( ) ) ?;
190199 write ! ( right, "{}" , other. to_string( ) . green( ) ) ?;
191- Ok ( ( ) )
200+ Ok ( true )
192201 } ;
193202 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) ,
203206 // The non-trivial case: the trees differ partially.
204207 (
205208 Separated { sep, children : c1 } ,
@@ -209,20 +212,19 @@ impl<'a> DisplayTree<'a> {
209212 } ,
210213 ) if strip_markup ( sep) == strip_markup ( sep2) && c1. len ( ) == c2. len ( ) => {
211214 let mut is_first = true ;
215+ let mut any_diff = false ;
212216 for ( c1, c2) in c1. iter ( ) . zip ( c2) {
213217 if !is_first {
214218 write ! ( left, "{sep}" ) ?;
215219 write ! ( right, "{sep}" ) ?;
216220 }
217- c1. diff_display_inner ( c2, left, right) ?;
221+ any_diff |= c1. diff_display_inner ( c2, left, right) ?;
218222 is_first = false ;
219223 }
224+ Ok ( any_diff)
220225 }
221- _ => {
222- all_different ( left, right) ?;
223- }
226+ _ => all_different ( left, right) ,
224227 }
225- Ok ( ( ) )
226228 }
227229}
228230
0 commit comments