@@ -8,14 +8,14 @@ use regex_syntax::hir;
88use super :: SpecGrammar ;
99use crate :: { char_set, grammar, Grammar , TypeId , TypeVec } ;
1010
11- pub type ConvertResult < T > = Result < T , ConvertError > ;
11+ pub type Result < T > = std :: result :: Result < T , Error > ;
1212
1313use self :: utils:: { TokenMap , TypeMap } ;
1414
1515/// Convert a [`SpecGrammar`] (likely parsed from a TOML file) into a full [`Grammar`], or fail
1616/// with a [`ConvertError`]. This is a largely straightforward process, since the 'shapes' of
1717/// [`SpecGrammar`] and [`Grammar`] are (intentionally) similar.
18- pub ( crate ) fn convert ( grammar : SpecGrammar ) -> ConvertResult < Grammar > {
18+ pub ( crate ) fn convert ( grammar : SpecGrammar ) -> self :: Result < Grammar > {
1919 let SpecGrammar {
2020 root_type,
2121 whitespace,
@@ -36,7 +36,7 @@ pub(crate) fn convert(grammar: SpecGrammar) -> ConvertResult<Grammar> {
3636
3737/// The possibly ways that parsing a [`Grammar`] can fail.
3838#[ derive( Debug ) ]
39- pub enum ConvertError {
39+ pub enum Error {
4040 /// An error occurred whilst generating a [`Regex`] provided by the user
4141 Regex {
4242 type_name : String ,
@@ -67,7 +67,7 @@ pub enum ConvertError {
6767fn convert_types (
6868 types : HashMap < super :: TypeName , super :: Type > ,
6969 token_map : & mut TokenMap ,
70- ) -> ConvertResult < ( TypeVec < grammar:: Type > , TypeMap ) > {
70+ ) -> self :: Result < ( TypeVec < grammar:: Type > , TypeMap ) > {
7171 // Before generating types, assign all names to type IDs (because types may refer to child
7272 // types which appear after themselves in the HashMap iterator). If we see a `TypeName` which
7373 // is not in the `TypeMap`, then we know it must be invalid and we can generate an error.
@@ -113,7 +113,7 @@ fn convert_types(
113113 & type_map,
114114 )
115115 } )
116- . collect :: < Result < _ , _ > > ( ) ?;
116+ . collect :: < self :: Result < _ > > ( ) ?;
117117 Ok ( ( types, type_map) )
118118}
119119
@@ -124,7 +124,7 @@ fn convert_type(
124124 parseable_type_ids : & HashSet < TypeId > ,
125125 token_map : & mut TokenMap ,
126126 type_map : & TypeMap ,
127- ) -> ConvertResult < grammar:: Type > {
127+ ) -> self :: Result < grammar:: Type > {
128128 let ( key, mut keys, inner) = match t {
129129 super :: Type :: Pattern {
130130 key,
@@ -191,7 +191,7 @@ fn convert_type(
191191 } )
192192}
193193
194- fn convert_escape_rules ( rules : super :: EscapeRules ) -> ConvertResult < grammar:: EscapeRules > {
194+ fn convert_escape_rules ( rules : super :: EscapeRules ) -> self :: Result < grammar:: EscapeRules > {
195195 let super :: EscapeRules {
196196 start_sequence,
197197 rules,
@@ -215,18 +215,18 @@ fn convert_escape_rules(rules: super::EscapeRules) -> ConvertResult<grammar::Esc
215215fn compute_type_descendants (
216216 types : & IndexSlice < TypeId , [ ( super :: TypeName , super :: Type ) ] > ,
217217 type_map : & TypeMap ,
218- ) -> ConvertResult < TypeVec < Vec < TypeId > > > {
218+ ) -> self :: Result < TypeVec < Vec < TypeId > > > {
219219 // For each TypeId, determine the `TypeId`s of its children
220220 let child_type_ids: TypeVec < Vec < TypeId > > = types
221221 . iter ( )
222222 . map ( |( parent_name, ty) | match ty {
223223 super :: Type :: Pattern { children, .. } => children
224224 . iter ( )
225225 . map ( |child_name| type_map. get ( child_name, parent_name) )
226- . collect :: < ConvertResult < Vec < TypeId > > > ( ) ,
226+ . collect :: < self :: Result < Vec < TypeId > > > ( ) ,
227227 super :: Type :: Stringy { .. } => Ok ( Vec :: new ( ) ) , // Stringy nodes have no children
228228 } )
229- . collect :: < Result < _ , _ > > ( ) ?;
229+ . collect :: < self :: Result < _ > > ( ) ?;
230230 // For each node flatten its descendant tree, terminating if any cycles are found.
231231 //
232232 // PERF: This does a lot of duplicated work expanding nodes, which could be sped up by
@@ -261,7 +261,7 @@ fn enumerate_type_descendants(
261261 child_type_ids : & IndexSlice < TypeId , [ Vec < TypeId > ] > ,
262262 type_stack : & mut Vec < TypeId > ,
263263 out : & mut Vec < TypeId > ,
264- ) -> ConvertResult < ( ) > {
264+ ) -> self :: Result < ( ) > {
265265 // Check for cycles
266266 if let Some ( idx) = type_stack. iter ( ) . position ( |& i| i == id) {
267267 // `type_stack[idx..] + id` forms the cycle (i.e. a cycle which starts and ends with `id`
@@ -270,7 +270,7 @@ fn enumerate_type_descendants(
270270 . chain ( std:: iter:: once ( & id) )
271271 . map ( |& id| types[ id] . 0 . to_owned ( ) )
272272 . collect_vec ( ) ;
273- return Err ( ConvertError :: TypeCycle ( cycle) ) ;
273+ return Err ( Error :: TypeCycle ( cycle) ) ;
274274 }
275275 // Mark this type as a descendant (if it hasn't been listed already)
276276 if !out. contains ( & id) {
@@ -294,19 +294,19 @@ fn compile_pattern(
294294 parent_type_name : & str ,
295295 token_map : & mut TokenMap ,
296296 type_map : & TypeMap ,
297- ) -> ConvertResult < grammar:: Pattern > {
297+ ) -> self :: Result < grammar:: Pattern > {
298298 elems
299299 . into_iter ( )
300300 . map ( |e| compile_pattern_element ( e, parent_type_name, token_map, type_map) )
301- . collect :: < Result < _ , _ > > ( )
301+ . collect :: < self :: Result < _ > > ( )
302302}
303303
304304fn compile_pattern_element (
305305 elem : super :: PatternElement ,
306306 parent_type_name : & str ,
307307 token_map : & mut TokenMap ,
308308 type_map : & TypeMap ,
309- ) -> ConvertResult < grammar:: PatternElement > {
309+ ) -> self :: Result < grammar:: PatternElement > {
310310 use super :: PatternElement :: * ;
311311 use grammar:: PatternElement as PE ;
312312 Ok ( match elem {
@@ -323,10 +323,10 @@ fn compile_pattern_element(
323323// REGEX/WHITESPACE/CHAR SET //
324324///////////////////////////////
325325
326- fn convert_validity_regex ( regex_str : & str , type_name : & str ) -> ConvertResult < grammar:: Regexes > {
326+ fn convert_validity_regex ( regex_str : & str , type_name : & str ) -> self :: Result < grammar:: Regexes > {
327327 macro_rules! compile_regex {
328328 ( $string: expr) => {
329- Regex :: new( & $string) . map_err( |inner| ConvertError :: Regex {
329+ Regex :: new( & $string) . map_err( |inner| Error :: Regex {
330330 type_name: type_name. to_owned( ) ,
331331 regex: $string,
332332 inner,
@@ -345,19 +345,19 @@ fn convert_validity_regex(regex_str: &str, type_name: &str) -> ConvertResult<gra
345345 } )
346346}
347347
348- fn convert_whitespace ( ws_chars : super :: CharSet ) -> ConvertResult < grammar:: Whitespace > {
348+ fn convert_whitespace ( ws_chars : super :: CharSet ) -> self :: Result < grammar:: Whitespace > {
349349 convert_char_set ( ws_chars) . map ( grammar:: Whitespace :: from)
350350}
351351
352- fn convert_char_set ( set : super :: CharSet ) -> ConvertResult < char_set:: CharSet > {
352+ fn convert_char_set ( set : super :: CharSet ) -> self :: Result < char_set:: CharSet > {
353353 // Convert the source `CharSet` into the string for a regex which matches single `char`s from
354354 // the same set.
355355 let source_str = set. 0 ;
356356 let regex_str = format ! ( "[{}]" , source_str) ;
357357 // Parse that regex
358358 let regex_hir = regex_syntax:: Parser :: new ( )
359359 . parse ( & regex_str)
360- . map_err ( |inner| ConvertError :: CharSet {
360+ . map_err ( |inner| Error :: CharSet {
361361 source_str,
362362 regex_str,
363363 inner,
@@ -380,7 +380,7 @@ mod utils {
380380
381381 use crate :: { spec, Token , TokenId , TypeId , TypeVec } ;
382382
383- use super :: { ConvertError , ConvertResult } ;
383+ use super :: Error ;
384384
385385 /// Maps [`TypeName`]s to [`TypeId`]s, providing `get` methods which generate error messages
386386 #[ derive( Debug , Clone ) ]
@@ -404,21 +404,21 @@ mod utils {
404404 ( types, Self { inner : inner_map } )
405405 }
406406
407- pub ( super ) fn get ( & self , name : & str , parent_type_name : & str ) -> ConvertResult < TypeId > {
407+ pub ( super ) fn get ( & self , name : & str , parent_type_name : & str ) -> super :: Result < TypeId > {
408408 self . inner
409409 . get ( name)
410410 . copied ( )
411- . ok_or_else ( || ConvertError :: UnknownChildType {
411+ . ok_or_else ( || Error :: UnknownChildType {
412412 name : name. to_owned ( ) ,
413413 parent_name : parent_type_name. to_owned ( ) ,
414414 } )
415415 }
416416
417- pub ( super ) fn get_root ( & self , name : & str ) -> ConvertResult < TypeId > {
417+ pub ( super ) fn get_root ( & self , name : & str ) -> super :: Result < TypeId > {
418418 self . inner
419419 . get ( name)
420420 . copied ( )
421- . ok_or_else ( || ConvertError :: UnknownRootType ( name. to_owned ( ) ) )
421+ . ok_or_else ( || Error :: UnknownRootType ( name. to_owned ( ) ) )
422422 }
423423 }
424424
0 commit comments