@@ -4,20 +4,59 @@ use serde_json::Map;
44use serde_json:: Value ;
55use std:: collections:: HashMap ;
66use std:: collections:: HashSet ;
7- use std:: env ;
7+ use std:: fmt ;
88use std:: fs;
9+ use std:: str:: FromStr ;
10+ use structopt:: StructOpt ;
11+
12+ #[ derive( Debug ) ]
13+ struct AppError {
14+ message : String ,
15+ }
16+ impl fmt:: Display for AppError {
17+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
18+ write ! ( f, "{}" , self . message)
19+ }
20+ }
21+
22+ enum CliOptions {
23+ D ,
24+ F ,
25+ }
26+ impl FromStr for CliOptions {
27+ type Err = AppError ;
28+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
29+ match s {
30+ "d" => Ok ( CliOptions :: D ) ,
31+ "f" => Ok ( CliOptions :: F ) ,
32+ _ => Err ( Self :: Err {
33+ message : "BAD option" . to_string ( ) ,
34+ } ) ,
35+ }
36+ }
37+ }
38+
39+ #[ derive( StructOpt ) ]
40+ struct Cli {
41+ option : CliOptions ,
42+ source1 : String ,
43+ source2 : String ,
44+ }
945
1046fn main ( ) {
11- let args = env:: args ( ) . collect :: < Vec < String > > ( ) ;
12- let file1 = & args[ 1 ] ;
13- let file2 = & args[ 2 ] ;
47+ let args = Cli :: from_args ( ) ;
1448
15- let data1 =
16- & fs:: read_to_string ( file1) . expect ( & format ! ( "Error occurred while reading {}" , file1) ) ;
17- let data2 =
18- & fs:: read_to_string ( file2) . expect ( & format ! ( "Error occurred while reading {}" , file2) ) ;
49+ let ( data1, data2) = match args. option {
50+ CliOptions :: D => ( args. source1 , args. source2 ) ,
51+ CliOptions :: F => {
52+ ( fs:: read_to_string ( args. source1 )
53+ . expect ( & format ! ( "Error occurred while reading source1" ) ) ,
54+ fs:: read_to_string ( args. source2 )
55+ . expect ( & format ! ( "Error occurred while reading source2" ) ) )
56+ }
57+ } ;
58+ display_output ( compare_jsons ( & data1, & data2) ) ;
1959
20- display_output ( compare_jsons ( data1, data2) ) ;
2160}
2261
2362fn display_output ( result : Mismatch ) {
@@ -87,9 +126,12 @@ impl KeyNode {
87126 let nil_key = |key : Option < String > | key. unwrap_or ( String :: new ( ) ) ;
88127 match self {
89128 KeyNode :: Nil => keys. push ( nil_key ( key_from_root) ) ,
90- KeyNode :: Value ( a, b) => {
91- keys. push ( format ! ( "{} [ {} :: {} ]" , val_key( key_from_root) , a. to_string( ) . blue( ) . bold( ) , b. to_string( ) . cyan( ) . bold( ) ) )
92- }
129+ KeyNode :: Value ( a, b) => keys. push ( format ! (
130+ "{} [ {} :: {} ]" ,
131+ val_key( key_from_root) ,
132+ a. to_string( ) . blue( ) . bold( ) ,
133+ b. to_string( ) . cyan( ) . bold( )
134+ ) ) ,
93135 KeyNode :: Node ( map) => {
94136 for ( key, value) in map {
95137 value. absolute_keys (
0 commit comments