22pub (all ) type! EdnCommonError String derive (Eq , Hash , Default )
33
44///|
5- pub fn to_string ( self : EdnCommonError ) -> String {
5+ pub impl Show for EdnCommonError with output ( self , logger ) {
66 match self {
7- EdnCommonError (s ) => s
7+ EdnCommonError (s ) => logger . write_string ( s )
88 }
99}
1010
1111///|
12- pub impl Show for EdnCommonError with output ( self , logger ) {
13- Show :: output ( self . to_string (), logger )
12+ impl @strconv . FromStr for Edn with from_string ( s ) {
13+ parse! ( s )
1414}
1515
1616///| parse Cirru code into data
17- pub fn parse (s : String ) -> Edn !EdnCommonError {
17+ pub fn parse (s : String ) -> Edn !@strconv . StrConvError {
1818 let xs = @cirru_parser .parse? (s ).unwrap ()
1919 if xs .length () == 1 {
2020 match xs [0 ] {
21- Leaf (s ) => raise EdnCommonError ("expected expr for data, got leaf: " + s )
21+ Leaf (s ) =>
22+ raise @strconv .StrConvError ("expected expr for data, got leaf: " + s )
2223 List (_ ) => extract_cirru_edn! (xs [0 ])
2324 }
2425 } else {
25- raise EdnCommonError ("Expected 1 expr for edn, got length \{ xs .length ()} " )
26+ raise @strconv .StrConvError (
27+ "Expected 1 expr for edn, got length \{ xs .length ()} " ,
28+ )
2629 }
2730}
2831
2932///|
30- fn extract_cirru_edn (node : @cirru_parser .Cirru ) -> Edn !EdnCommonError {
33+ fn extract_cirru_edn (node : @cirru_parser .Cirru ) -> Edn !@strconv . StrConvError {
3134 match node {
3235 Leaf (s ) =>
3336 match s {
3437 "nil" => Nil
3538 "true" => Bool (true )
3639 "false" => Bool (false )
37- "" => raise EdnCommonError ("empty string is invalid for edn" )
40+ "" => raise @strconv . StrConvError ("empty string is invalid for edn" )
3841 s1 =>
3942 match s1 [0 ] {
4043 '\' ' => Symbol (s1 .substring (start = 1 ))
41- ':' => tag (s1 .substring (start = 1 ))
44+ ':' => Edn :: tag (s1 .substring (start = 1 ))
4245 '"' | '|' => Str (s1 .substring (start = 1 ))
4346 _ =>
4447 match @strconv .parse_double? (s1 .trim (" " )) {
4548 Ok (f ) => Number (f )
4649 Err (e ) =>
4750 match e {
4851 @strconv .StrConvError (s ) =>
49- raise EdnCommonError ("failed to parse number: " + s )
52+ raise @strconv .StrConvError (
53+ "failed to parse number: " + s ,
54+ )
5055 }
5156 }
5257 }
5358 }
5459 List (xs ) =>
5560 if xs .is_empty () {
56- raise EdnCommonError ("empty expr is invalid for edn" )
61+ raise @strconv . StrConvError ("empty expr is invalid for edn" )
5762 } else {
5863 // println("extracting list: " + xs.to_string())
5964 match xs [0 ] {
@@ -63,7 +68,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
6368 if xs .length () == 2 {
6469 Quote (xs [1 ])
6570 } else {
66- raise EdnCommonError ("missing edn quote value" )
71+ raise @strconv . StrConvError ("missing edn quote value" )
6772 }
6873 "do" => {
6974 let mut ret : Edn? = None
@@ -72,21 +77,22 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
7277 continue
7378 }
7479 match ret {
75- Some (_ ) => raise EdnCommonError ("multiple values in do" )
80+ Some (_ ) =>
81+ raise @strconv .StrConvError ("multiple values in do" )
7682 None => ret = Some (extract_cirru_edn! (x ))
7783 }
7884 }
7985 match ret {
80- None => raise EdnCommonError ("missing edn do value" )
86+ None => raise @strconv . StrConvError ("missing edn do value" )
8187 Some (_v ) => ()
8288 }
83- ret .or_error! (EdnCommonError ("missing edn do value" ))
89+ ret .or_error! (@strconv . StrConvError ("missing edn do value" ))
8490 }
8591 "atom" =>
8692 if xs .length () == 2 {
8793 Atom (extract_cirru_edn! (xs [1 ]))
8894 } else {
89- raise EdnCommonError ("missing edn atom value" )
95+ raise @strconv . StrConvError ("missing edn atom value" )
9096 }
9197 "::" => {
9298 let mut tag : Edn? = None
@@ -105,7 +111,8 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
105111 }
106112 match tag {
107113 Some (x0 ) => Tuple ({ tag : x0 , extra })
108- None => raise EdnCommonError ("missing edn :: fst value" )
114+ None =>
115+ raise @strconv .StrConvError ("missing edn :: fst value" )
109116 }
110117 }
111118 "[]" => {
@@ -116,7 +123,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
116123 }
117124 match extract_cirru_edn? (x ) {
118125 Ok (v ) => ys .push (v )
119- Err (v ) => raise EdnCommonError (v .to_string ())
126+ Err (v ) => raise @strconv . StrConvError (v .to_string ())
120127 }
121128 }
122129 List (EdnListView (ys ))
@@ -129,7 +136,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
129136 }
130137 match extract_cirru_edn? (x ) {
131138 Ok (v ) => ys .add (v )
132- Err (v ) => raise EdnCommonError (v .to_string ())
139+ Err (v ) => raise @strconv . StrConvError (v .to_string ())
133140 }
134141 }
135142 Edn ::Set (EdnSetView (ys ))
@@ -142,7 +149,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
142149 }
143150 match x {
144151 Leaf (s ) =>
145- raise EdnCommonError (
152+ raise @strconv . StrConvError (
146153 "expected a pair, invalid map entry: " + s ,
147154 )
148155 List (ys ) =>
@@ -151,11 +158,11 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
151158 (extract_cirru_edn? (ys [0 ]), extract_cirru_edn? (ys [1 ])) {
152159 (Ok (k ), Ok (v )) => zs .set (k , v )
153160 (Err (e ), _ ) =>
154- raise EdnCommonError (
161+ raise @strconv . StrConvError (
155162 "invalid map entry `\{ e } ` from `\{ ys [0 ]} `" ,
156163 )
157164 (Ok (k ), Err (e )) =>
158- raise EdnCommonError (
165+ raise @strconv . StrConvError (
159166 "invalid map entry for `\{ k } `, got \{ e } " ,
160167 )
161168 }
@@ -169,7 +176,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
169176 let name = match xs [1 ] {
170177 Leaf (s ) => EdnTag ::new (s .trim_start (":" ))
171178 List (e ) =>
172- raise EdnCommonError (
179+ raise @strconv . StrConvError (
173180 "expected record name in string: " + e .to_string (),
174181 )
175182 }
@@ -180,7 +187,7 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
180187 }
181188 match x {
182189 Leaf (s ) =>
183- raise EdnCommonError (
190+ raise @strconv . StrConvError (
184191 "expected record, invalid record entry: " + s ,
185192 )
186193 List (ys ) =>
@@ -189,27 +196,29 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
189196 (Leaf (s ), Ok (v )) =>
190197 entries .push ((EdnTag ::new (s .trim_start (":" )), v ))
191198 (Leaf (s ), Err (e )) =>
192- raise EdnCommonError (
199+ raise @strconv . StrConvError (
193200 "invalid record value for `\{ s } `, got: \{ e } " ,
194201 )
195202 (List (zs ), _ ) =>
196- raise EdnCommonError (
203+ raise @strconv . StrConvError (
197204 "invalid list as record key: \{ zs } " ,
198205 )
199206 }
200207 } else {
201- raise EdnCommonError (
208+ raise @strconv . StrConvError (
202209 "expected pair of 2: " + ys .to_string (),
203210 )
204211 }
205212 }
206213 }
207214 if entries .is_empty () {
208- raise EdnCommonError ("empty record is invalid" )
215+ raise @strconv . StrConvError ("empty record is invalid" )
209216 }
210217 Edn ::Record ({ tag : name , extra : entries })
211218 } else {
212- raise EdnCommonError ("insufficient items for edn record" )
219+ raise @strconv .StrConvError (
220+ "insufficient items for edn record" ,
221+ )
213222 }
214223 "buf" => {
215224 let ys : Array [UInt ] = []
@@ -225,28 +234,30 @@ fn extract_cirru_edn(node : @cirru_parser.Cirru) -> Edn!EdnCommonError {
225234 Err (e ) =>
226235 match e {
227236 @strconv .StrConvError (s ) =>
228- raise EdnCommonError (
237+ raise @strconv . StrConvError (
229238 "expected length 2 hex string in buffer, got: \{ y } \{ s } " ,
230239 )
231240 }
232241 }
233242 } else {
234- raise EdnCommonError (
243+ raise @strconv . StrConvError (
235244 "expected length 2 hex string in buffer, got: \{ y } " ,
236245 )
237246 }
238247 _ =>
239- raise EdnCommonError (
248+ raise @strconv . StrConvError (
240249 "expected hex string in buffer, got: " + x .to_string (),
241250 )
242251 }
243252 }
244253 Edn ::Buffer (ys )
245254 }
246- a => raise EdnCommonError ("invalid operator for edn: " + a )
255+ a => raise @strconv . StrConvError ("invalid operator for edn: " + a )
247256 }
248257 List (a ) =>
249- raise EdnCommonError ("invalid nodes for edn: " + a .to_string ())
258+ raise @strconv .StrConvError (
259+ "invalid nodes for edn: " + a .to_string (),
260+ )
250261 }
251262 }
252263 }
0 commit comments