2
2
3
3
//! Parsing support for [`PatchLocator`].
4
4
5
- use nom:: {
6
- branch:: alt,
7
- bytes:: complete:: tag,
8
- character:: complete:: { char as the_char, hex_digit1} ,
9
- combinator:: { map, map_res, opt, recognize} ,
10
- multi:: { many0, many0_count} ,
11
- sequence:: tuple,
5
+ use winnow:: {
6
+ ascii:: hex_digit1,
7
+ combinator:: { alt, opt, repeat} ,
8
+ PResult , Parser ,
12
9
} ;
13
10
14
11
use super :: {
@@ -18,115 +15,106 @@ use super::{
18
15
} ;
19
16
use crate :: patch:: { PatchId , PatchLocator , PatchOffsetAtom , PatchOffsets } ;
20
17
21
- pub ( in super :: super ) fn patch_locator ( input : & str ) -> nom :: IResult < & str , PatchLocator > {
18
+ pub ( in super :: super ) fn patch_locator ( input : & mut & str ) -> PResult < PatchLocator > {
22
19
alt ( (
23
20
patch_locator_name,
24
21
patch_locator_from_last,
25
22
patch_locator_top,
26
23
patch_locator_base,
27
- ) ) ( input)
24
+ ) )
25
+ . parse_next ( input)
28
26
}
29
27
30
- fn patch_locator_name ( input : & str ) -> nom:: IResult < & str , PatchLocator > {
31
- map (
32
- tuple ( ( patch_name, patch_offsets) ) ,
33
- |( patchname, offsets) | PatchLocator {
28
+ fn patch_locator_name ( input : & mut & str ) -> PResult < PatchLocator > {
29
+ ( patch_name, patch_offsets)
30
+ . map ( |( patchname, offsets) | PatchLocator {
34
31
id : PatchId :: Name ( patchname) ,
35
32
offsets,
36
- } ,
37
- ) ( input)
33
+ } )
34
+ . parse_next ( input)
38
35
}
39
36
40
- fn patch_locator_from_last ( input : & str ) -> nom:: IResult < & str , PatchLocator > {
41
- map (
42
- tuple ( ( the_char ( '^' ) , opt ( nonplussed_int) , patch_offsets) ) ,
43
- |( _, below_last, offsets) | PatchLocator {
37
+ fn patch_locator_from_last ( input : & mut & str ) -> PResult < PatchLocator > {
38
+ ( '^' , opt ( nonplussed_int) , patch_offsets)
39
+ . map ( |( _, below_last, offsets) | PatchLocator {
44
40
id : PatchId :: BelowLast ( below_last) ,
45
41
offsets,
46
- } ,
47
- ) ( input)
42
+ } )
43
+ . parse_next ( input)
48
44
}
49
45
50
- pub ( in super :: super ) fn patch_locator_top ( input : & str ) -> nom :: IResult < & str , PatchLocator > {
46
+ pub ( in super :: super ) fn patch_locator_top ( input : & mut & str ) -> PResult < PatchLocator > {
51
47
alt ( (
52
- map ( tuple ( ( the_char ( '@' ) , patch_offsets) ) , |( _, offsets) | {
53
- PatchLocator {
54
- id : PatchId :: Top ,
55
- offsets,
56
- }
48
+ ( '@' , patch_offsets) . map ( |( _, offsets) | PatchLocator {
49
+ id : PatchId :: Top ,
50
+ offsets,
51
+ } ) ,
52
+ ( '~' , opt ( unsigned_int) , patch_offsets) . map ( |( _, n, offsets) | PatchLocator {
53
+ id : PatchId :: BelowTop ( n) ,
54
+ offsets,
57
55
} ) ,
58
- map (
59
- tuple ( ( the_char ( '~' ) , opt ( unsigned_int) , patch_offsets) ) ,
60
- |( _, n, offsets) | PatchLocator {
61
- id : PatchId :: BelowTop ( n) ,
62
- offsets,
63
- } ,
64
- ) ,
65
- ) ) ( input)
56
+ ) )
57
+ . parse_next ( input)
66
58
}
67
59
68
- pub ( in super :: super ) fn patch_locator_base ( input : & str ) -> nom :: IResult < & str , PatchLocator > {
69
- map ( tuple ( ( tag ( "{base}" ) , patch_offsets) ) , | ( _ , offsets ) | {
70
- PatchLocator {
60
+ pub ( in super :: super ) fn patch_locator_base ( input : & mut & str ) -> PResult < PatchLocator > {
61
+ ( "{base}" , patch_offsets)
62
+ . map ( | ( _ , offsets ) | PatchLocator {
71
63
id : PatchId :: Base ,
72
64
offsets,
73
- }
74
- } ) ( input)
65
+ } )
66
+ . parse_next ( input)
75
67
}
76
68
77
- pub ( in super :: super ) fn patch_offsets ( input : & str ) -> nom:: IResult < & str , PatchOffsets > {
78
- map ( recognize ( many0_count ( patch_offset_atom) ) , |s| {
79
- PatchOffsets ( String :: from ( s) )
80
- } ) ( input)
69
+ pub ( in super :: super ) fn patch_offsets ( input : & mut & str ) -> PResult < PatchOffsets > {
70
+ repeat :: < _ , _ , Vec < PatchOffsetAtom > , _ , _ > ( 0 .., patch_offset_atom)
71
+ . take ( )
72
+ . map ( |s : & str | PatchOffsets ( s. to_string ( ) ) )
73
+ . parse_next ( input)
81
74
}
82
75
83
- pub ( in super :: super ) fn patch_offset_atoms (
84
- input : & str ,
85
- ) -> nom:: IResult < & str , Vec < PatchOffsetAtom > > {
86
- many0 ( patch_offset_atom) ( input)
76
+ pub ( in super :: super ) fn patch_offset_atoms ( input : & mut & str ) -> PResult < Vec < PatchOffsetAtom > > {
77
+ repeat ( 0 .., patch_offset_atom) . parse_next ( input)
87
78
}
88
79
89
- pub ( in super :: super ) fn patch_offset_atom ( input : & str ) -> nom :: IResult < & str , PatchOffsetAtom > {
90
- alt ( ( patch_offset_atom_plus, patch_offset_atom_tilde) ) ( input)
80
+ pub ( in super :: super ) fn patch_offset_atom ( input : & mut & str ) -> PResult < PatchOffsetAtom > {
81
+ alt ( ( patch_offset_atom_plus, patch_offset_atom_tilde) ) . parse_next ( input)
91
82
}
92
83
93
- fn patch_offset_atom_plus ( input : & str ) -> nom :: IResult < & str , PatchOffsetAtom > {
94
- map ( tuple ( ( the_char ( '+' ) , opt ( unsigned_int) ) ) , | ( _ , n ) | {
95
- PatchOffsetAtom :: Plus ( n)
96
- } ) ( input)
84
+ fn patch_offset_atom_plus ( input : & mut & str ) -> PResult < PatchOffsetAtom > {
85
+ ( '+' , opt ( unsigned_int) )
86
+ . map ( | ( _ , n ) | PatchOffsetAtom :: Plus ( n) )
87
+ . parse_next ( input)
97
88
}
98
89
99
- fn patch_offset_atom_tilde ( input : & str ) -> nom :: IResult < & str , PatchOffsetAtom > {
100
- map ( tuple ( ( the_char ( '~' ) , opt ( unsigned_int) ) ) , | ( _ , n ) | {
101
- PatchOffsetAtom :: Tilde ( n)
102
- } ) ( input)
90
+ fn patch_offset_atom_tilde ( input : & mut & str ) -> PResult < PatchOffsetAtom > {
91
+ ( '~' , opt ( unsigned_int) )
92
+ . map ( | ( _ , n ) | PatchOffsetAtom :: Tilde ( n) )
93
+ . parse_next ( input)
103
94
}
104
95
105
96
pub ( in super :: super ) fn oid_prefix_offsets (
106
- input : & str ,
107
- ) -> nom :: IResult < & str , ( gix:: hash:: Prefix , PatchOffsets ) > {
108
- tuple ( ( oid_prefix, patch_offsets) ) ( input)
97
+ input : & mut & str ,
98
+ ) -> PResult < ( gix:: hash:: Prefix , PatchOffsets ) > {
99
+ ( oid_prefix, patch_offsets) . parse_next ( input)
109
100
}
110
101
111
- fn oid_prefix ( input : & str ) -> nom:: IResult < & str , gix:: hash:: Prefix > {
112
- map_res ( hex_digit1, gix:: hash:: Prefix :: from_hex) ( input)
102
+ fn oid_prefix ( input : & mut & str ) -> PResult < gix:: hash:: Prefix > {
103
+ hex_digit1
104
+ . try_map ( gix:: hash:: Prefix :: from_hex)
105
+ . parse_next ( input)
113
106
}
114
107
115
108
pub ( in super :: super ) fn sign_number_offsets (
116
- input : & str ,
117
- ) -> nom :: IResult < & str , ( Option < Sign > , Option < usize > , PatchOffsets ) > {
109
+ input : & mut & str ,
110
+ ) -> PResult < ( Option < Sign > , Option < usize > , PatchOffsets ) > {
118
111
alt ( (
119
- map ( tuple ( ( negative_int, patch_offsets) ) , |( n, offsets) | {
120
- ( Some ( Sign :: Minus ) , Some ( n. unsigned_abs ( ) ) , offsets)
121
- } ) ,
122
- map ( tuple ( ( plusative_int, patch_offsets) ) , |( n, offsets) | {
123
- ( Some ( Sign :: Plus ) , Some ( n. unsigned_abs ( ) ) , offsets)
124
- } ) ,
125
- map ( tuple ( ( unsigned_int, patch_offsets) ) , |( n, offsets) | {
126
- ( None , Some ( n) , offsets)
127
- } ) ,
128
- map ( tuple ( ( sign, patch_offsets) ) , |( sign, offsets) | {
129
- ( Some ( sign) , None , offsets)
130
- } ) ,
131
- ) ) ( input)
112
+ ( negative_int, patch_offsets)
113
+ . map ( |( n, offsets) | ( Some ( Sign :: Minus ) , Some ( n. unsigned_abs ( ) ) , offsets) ) ,
114
+ ( plusative_int, patch_offsets)
115
+ . map ( |( n, offsets) | ( Some ( Sign :: Plus ) , Some ( n. unsigned_abs ( ) ) , offsets) ) ,
116
+ ( unsigned_int, patch_offsets) . map ( |( n, offsets) | ( None , Some ( n) , offsets) ) ,
117
+ ( sign, patch_offsets) . map ( |( sign, offsets) | ( Some ( sign) , None , offsets) ) ,
118
+ ) )
119
+ . parse_next ( input)
132
120
}
0 commit comments