@@ -2,72 +2,18 @@ pub mod constants;
22pub mod ds;
33mod process;
44
5- use colored:: * ;
6- use constants:: Message ;
7- use ds:: { key_node:: KeyNode , mismatch:: Mismatch } ;
8- use std:: io:: { self , Write } ;
9-
5+ use ds:: mismatch:: Mismatch ;
106pub fn compare_jsons ( a : & str , b : & str ) -> Result < Mismatch , serde_json:: Error > {
117 let value1 = serde_json:: from_str ( a) ?;
128 let value2 = serde_json:: from_str ( b) ?;
139 Ok ( process:: match_json ( & value1, & value2) )
1410}
1511
16- pub fn display_output ( result : Mismatch ) -> Result < ( ) , std:: io:: Error > {
17- let no_mismatch = Mismatch {
18- left_only_keys : KeyNode :: Nil ,
19- right_only_keys : KeyNode :: Nil ,
20- keys_in_both : KeyNode :: Nil ,
21- } ;
22-
23- let stdout = io:: stdout ( ) ;
24- let mut handle = io:: BufWriter :: new ( stdout. lock ( ) ) ;
25- Ok ( if no_mismatch == result {
26- writeln ! ( handle, "\n {}" , Message :: NoMismatch ) ?;
27- } else {
28- match result. keys_in_both {
29- KeyNode :: Node ( _) => {
30- let mut keys = Vec :: new ( ) ;
31- result. keys_in_both . absolute_keys ( & mut keys, None ) ;
32- writeln ! ( handle, "\n {}:" , Message :: Mismatch ) ?;
33- for key in keys {
34- writeln ! ( handle, "{}" , key) ?;
35- }
36- }
37- KeyNode :: Value ( _, _) => writeln ! ( handle, "{}" , Message :: RootMismatch ) ?,
38- KeyNode :: Nil => ( ) ,
39- }
40- match result. left_only_keys {
41- KeyNode :: Node ( _) => {
42- let mut keys = Vec :: new ( ) ;
43- result. left_only_keys . absolute_keys ( & mut keys, None ) ;
44- writeln ! ( handle, "\n {}:" , Message :: LeftExtra ) ?;
45- for key in keys {
46- writeln ! ( handle, "{}" , key. red( ) . bold( ) ) ?;
47- }
48- }
49- KeyNode :: Value ( _, _) => ( ) ,
50- KeyNode :: Nil => ( ) ,
51- }
52- match result. right_only_keys {
53- KeyNode :: Node ( _) => {
54- let mut keys = Vec :: new ( ) ;
55- result. right_only_keys . absolute_keys ( & mut keys, None ) ;
56- writeln ! ( handle, "\n {}:" , Message :: RightExtra ) ?;
57- for key in keys {
58- writeln ! ( handle, "{}" , key. green( ) . bold( ) ) ?;
59- }
60- }
61- KeyNode :: Value ( _, _) => ( ) ,
62- KeyNode :: Nil => ( ) ,
63- }
64- } )
65- }
6612
6713#[ cfg( test) ]
6814mod tests {
6915 use super :: * ;
70-
16+ use super :: ds :: { key_node :: KeyNode , mismatch :: Mismatch } ;
7117 use maplit:: hashmap;
7218 use serde_json:: json;
7319
0 commit comments