1- use anyhow:: { anyhow , Context , Result } ;
1+ use anyhow:: { Context , Result , anyhow } ;
22use resast:: prelude:: * ;
33use ressa:: Parser ;
4- use serde_json:: { json , Value as JsonValue } ;
4+ use serde_json:: { Value as JsonValue , json } ;
55
66pub fn gron_to_json ( input : & str ) -> Result < JsonValue > {
77 let mut root = JsonValue :: Null ;
88
9- for ( line_num, line) in input. lines ( ) . enumerate ( ) . filter ( |( _, l) | !l. trim ( ) . is_empty ( ) ) {
9+ for ( line_num, line) in input
10+ . lines ( )
11+ . enumerate ( )
12+ . filter ( |( _, l) | !l. trim ( ) . is_empty ( ) )
13+ {
1014 let line = line. trim ( ) ;
1115 parse_and_apply_line ( line, & mut root)
1216 . with_context ( || format ! ( "Error on line {}: {}" , line_num + 1 , line) ) ?;
@@ -16,26 +20,29 @@ pub fn gron_to_json(input: &str) -> Result<JsonValue> {
1620
1721fn parse_and_apply_line ( line : & str , root : & mut JsonValue ) -> Result < ( ) > {
1822 let mut parser = Parser :: new ( line) . map_err ( |e| anyhow ! ( "Parser init failed: {:?}" , e) ) ?;
19-
23+
2024 let part = parser
2125 . next ( )
2226 . ok_or_else ( || anyhow ! ( "Empty line" ) ) ?
2327 . map_err ( |e| anyhow ! ( "Parse error: {:?}" , e) ) ?;
24-
28+
2529 if let ProgramPart :: Stmt ( Stmt :: Expr ( Expr :: Assign ( assign) ) ) = part {
2630 let path = extract_path ( & assign. left ) ?;
2731 let value = extract_value ( & assign. right ) ?;
2832 set_value_at_path ( root, & path, value) ?;
2933 Ok ( ( ) )
3034 } else {
31- // gron occasionally emits 'const' or 'var' depending on flags,
35+ // gron occasionally emits 'const' or 'var' depending on flags,
3236 // but standard gron is pure assignment.
3337 Err ( anyhow ! ( "Expected assignment (e.g., json.a = 1)" ) )
3438 }
3539}
3640
3741#[ derive( Debug ) ]
38- enum PathSegment { Property ( String ) , Index ( usize ) }
42+ enum PathSegment {
43+ Property ( String ) ,
44+ Index ( usize ) ,
45+ }
3946
4047fn extract_path ( left : & AssignLeft ) -> Result < Vec < PathSegment > > {
4148 let mut segments = Vec :: new ( ) ;
@@ -50,8 +57,8 @@ fn extract_path(left: &AssignLeft) -> Result<Vec<PathSegment>> {
5057 while let Expr :: Member ( mem) = current_expr {
5158 let seg = match & * mem. property {
5259 Expr :: Ident ( i) => PathSegment :: Property ( i. name . to_string ( ) ) ,
53- Expr :: Lit ( Lit :: String ( StringLit :: Double ( s) ) ) |
54- Expr :: Lit ( Lit :: String ( StringLit :: Single ( s) ) ) => PathSegment :: Property ( s. to_string ( ) ) ,
60+ Expr :: Lit ( Lit :: String ( StringLit :: Double ( s) ) )
61+ | Expr :: Lit ( Lit :: String ( StringLit :: Single ( s) ) ) => PathSegment :: Property ( s. to_string ( ) ) ,
5562 Expr :: Lit ( Lit :: Number ( n) ) => PathSegment :: Index ( n. parse ( ) ?) ,
5663 _ => return Err ( anyhow ! ( "Unsupported path segment type" ) ) ,
5764 } ;
@@ -66,7 +73,7 @@ fn extract_path(left: &AssignLeft) -> Result<Vec<PathSegment>> {
6673 }
6774 }
6875
69- segments. reverse ( ) ;
76+ segments. reverse ( ) ;
7077 Ok ( segments)
7178}
7279
@@ -82,9 +89,10 @@ fn extract_value(expr: &Expr) -> Result<JsonValue> {
8289 } else {
8390 Ok ( json ! ( n. parse:: <f64 >( ) ?) )
8491 }
85- } ,
86- Lit :: String ( StringLit :: Double ( s) ) |
87- Lit :: String ( StringLit :: Single ( s) ) => Ok ( json ! ( s. to_string( ) ) ) ,
92+ }
93+ Lit :: String ( StringLit :: Double ( s) ) | Lit :: String ( StringLit :: Single ( s) ) => {
94+ Ok ( json ! ( s. to_string( ) ) )
95+ }
8896 _ => Err ( anyhow ! ( "Unsupported literal" ) ) ,
8997 } ,
9098 Expr :: Unary ( u) if u. operator == UnaryOp :: Minus => {
@@ -96,9 +104,9 @@ fn extract_value(expr: &Expr) -> Result<JsonValue> {
96104 } else {
97105 Err ( anyhow ! ( "Cannot negate non-numeric value" ) )
98106 }
99- } ,
100- Expr :: Array ( _) => Ok ( json ! ( [ ] ) ) ,
101- Expr :: Obj ( _) => Ok ( json ! ( { } ) ) ,
107+ }
108+ Expr :: Array ( _) => Ok ( json ! ( [ ] ) ) ,
109+ Expr :: Obj ( _) => Ok ( json ! ( { } ) ) ,
102110 _ => Err ( anyhow ! ( "Value type not supported" ) ) ,
103111 }
104112}
@@ -109,11 +117,19 @@ fn set_value_at_path(root: &mut JsonValue, path: &[PathSegment], value: JsonValu
109117 for seg in path {
110118 match seg {
111119 PathSegment :: Property ( p) => {
112- if !cur. is_object ( ) { * cur = json ! ( { } ) ; }
113- cur = cur. as_object_mut ( ) . unwrap ( ) . entry ( p. clone ( ) ) . or_insert ( JsonValue :: Null ) ;
120+ if !cur. is_object ( ) {
121+ * cur = json ! ( { } ) ;
122+ }
123+ cur = cur
124+ . as_object_mut ( )
125+ . unwrap ( )
126+ . entry ( p. clone ( ) )
127+ . or_insert ( JsonValue :: Null ) ;
114128 }
115129 PathSegment :: Index ( i) => {
116- if !cur. is_array ( ) { * cur = json ! ( [ ] ) ; }
130+ if !cur. is_array ( ) {
131+ * cur = json ! ( [ ] ) ;
132+ }
117133 let arr = cur. as_array_mut ( ) . unwrap ( ) ;
118134 if * i >= arr. len ( ) {
119135 arr. resize ( * i + 1 , JsonValue :: Null ) ;
@@ -124,8 +140,9 @@ fn set_value_at_path(root: &mut JsonValue, path: &[PathSegment], value: JsonValu
124140 }
125141
126142 // Logic Fix: Don't overwrite an existing object/array with an empty one.
127- if ( value. is_object ( ) && value. as_object ( ) . unwrap ( ) . is_empty ( ) && cur. is_object ( ) ) ||
128- ( value. is_array ( ) && value. as_array ( ) . unwrap ( ) . is_empty ( ) && cur. is_array ( ) ) {
143+ if ( value. is_object ( ) && value. as_object ( ) . unwrap ( ) . is_empty ( ) && cur. is_object ( ) )
144+ || ( value. is_array ( ) && value. as_array ( ) . unwrap ( ) . is_empty ( ) && cur. is_array ( ) )
145+ {
129146 return Ok ( ( ) ) ;
130147 }
131148
0 commit comments