@@ -123,6 +123,64 @@ impl<'d> ToDisplayTree<'d> for Type<'_> {
123123 }
124124}
125125
126+ impl < ' d > ToDisplayTree < ' d > for BindingAssignments < ' _ > {
127+ fn to_display_tree ( & self , a : & ' d Arenas < ' d > ) -> DisplayTree < ' d > {
128+ DisplayTree :: sep_by (
129+ a,
130+ ", " ,
131+ self . assignments
132+ . iter ( )
133+ . map ( |( name, ty) | name. to_display_tree ( a) . sep_then ( a, ": " , ty) ) ,
134+ )
135+ }
136+ }
137+
138+ impl < ' d > ToDisplayTree < ' d > for TypingResult < ' _ > {
139+ fn to_display_tree ( & self , a : & ' d Arenas < ' d > ) -> DisplayTree < ' d > {
140+ match self {
141+ TypingResult :: Success ( bindings) => {
142+ bindings. to_display_tree ( a) . surrounded ( a, "Success(" , ")" )
143+ }
144+ TypingResult :: BorrowError ( bindings, s) => bindings
145+ . to_display_tree ( a)
146+ . sep_then ( a, ", " , format ! ( "\" {s:?}\" " ) )
147+ . surrounded ( a, "BorrowError(" , ")" ) ,
148+ TypingResult :: TypeError ( TypeError :: External ( e) ) => format ! ( "{e}" )
149+ . to_display_tree ( a)
150+ . surrounded ( a, "TypeError(\" " , "\" )" ) ,
151+ TypingResult :: TypeError ( e) => {
152+ format ! ( "{e:?}" )
153+ . to_display_tree ( a)
154+ . surrounded ( a, "TypeError(\" " , "\" )" )
155+ }
156+ }
157+ }
158+ }
159+
160+ impl TypingResult < ' _ > {
161+ pub fn display ( & self ) -> String {
162+ let a = & Arenas :: default ( ) ;
163+ let out = self . to_display_tree ( a) . to_string ( ) ;
164+ match self {
165+ TypingResult :: Success ( ..) => out. green ( ) ,
166+ TypingResult :: BorrowError ( ..) | TypingResult :: TypeError ( ..) => out. red ( ) ,
167+ }
168+ }
169+
170+ /// Display two typing results, adjusting colors to highlight differences.
171+ pub fn display_diffed ( & self , other : & Self ) -> ( String , String ) {
172+ let a = & Arenas :: default ( ) ;
173+ match ( self , other) {
174+ ( TypingResult :: Success ( ..) , TypingResult :: Success ( ..) ) => {
175+ let left = self . to_display_tree ( a) ;
176+ let right = other. to_display_tree ( a) ;
177+ left. diff_display ( & right)
178+ }
179+ _ => ( self . to_string ( ) , other. to_string ( ) ) ,
180+ }
181+ }
182+ }
183+
126184impl < ' a > TypingPredicate < ' a > {
127185 /// Display as `let ...`.
128186 pub fn display_as_let ( & self ) -> String {
@@ -359,30 +417,9 @@ impl Display for PredicateStyle {
359417 }
360418}
361419
362- impl std :: fmt :: Display for BindingAssignments < ' _ > {
420+ impl Display for TypingResult < ' _ > {
363421 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
364- write ! (
365- f,
366- "{}" ,
367- self . assignments
368- . iter( )
369- . map( |( name, ty) | format!( "{name}: {ty}" ) )
370- . format( ", " )
371- )
372- }
373- }
374-
375- impl std:: fmt:: Display for TypingResult < ' _ > {
376- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
377- let out = match self {
378- TypingResult :: Success ( bindings) => format ! ( "Success({bindings})" ) . green ( ) ,
379- TypingResult :: BorrowError ( bindings, s) => {
380- format ! ( "BorrowError({bindings}, \" {s:?}\" )" ) . red ( )
381- }
382- TypingResult :: TypeError ( TypeError :: External ( e) ) => format ! ( "TypeError(\" {e}\" )" ) . red ( ) ,
383- TypingResult :: TypeError ( e) => format ! ( "TypeError(\" {e:?}\" )" ) . red ( ) ,
384- } ;
385- write ! ( f, "{}" , out)
422+ write ! ( f, "{}" , self . display( ) )
386423 }
387424}
388425
0 commit comments