1
- use bstr:: { BStr , BString , ByteSlice } ;
1
+ use bstr:: { BStr , ByteSlice } ;
2
2
use git_attributes:: ignore:: pattern:: Mode ;
3
3
use git_attributes:: { ignore, parse, State } ;
4
4
use git_testtools:: fixture_path;
5
5
6
6
#[ test]
7
7
fn byte_order_marks_are_no_patterns ( ) {
8
- assert_eq ! ( line( "\u{feff} hello" ) , ( r"hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
8
+ assert_eq ! ( line( "\u{feff} hello" ) , ( pattern ( r"hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
9
9
assert_eq ! (
10
10
line( "\u{feff} \" hello\" " ) ,
11
- ( r"hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 )
11
+ ( pattern ( r"hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 )
12
12
) ;
13
13
}
14
14
@@ -18,11 +18,11 @@ fn line_numbers_are_counted_correctly() {
18
18
assert_eq ! (
19
19
try_lines( & String :: from_utf8( ignore) . unwrap( ) ) . unwrap( ) ,
20
20
vec![
21
- ( r"*.[oa]" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 2 ) ,
22
- ( r"*.html" . into ( ) , Mode :: NO_SUB_DIR | Mode :: ENDS_WITH , vec![ ] , 5 ) ,
23
- ( r"!foo.html" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 8 ) ,
24
- ( r"#a/path" . into ( ) , Mode :: empty( ) , vec![ ] , 10 ) ,
25
- ( r"/*" . into ( ) , Mode :: empty( ) , vec![ ] , 11 ) ,
21
+ ( pattern ( r"*.[oa]" ) , Mode :: NO_SUB_DIR , vec![ ] , 2 ) ,
22
+ ( pattern ( r"*.html" ) , Mode :: NO_SUB_DIR | Mode :: ENDS_WITH , vec![ ] , 5 ) ,
23
+ ( pattern ( r"!foo.html" ) , Mode :: NO_SUB_DIR , vec![ ] , 8 ) ,
24
+ ( pattern ( r"#a/path" ) , Mode :: empty( ) , vec![ ] , 10 ) ,
25
+ ( pattern ( r"/*" ) , Mode :: empty( ) , vec![ ] , 11 ) ,
26
26
]
27
27
) ;
28
28
}
@@ -32,9 +32,9 @@ fn line_endings_can_be_windows_or_unix() {
32
32
assert_eq ! (
33
33
try_lines( "unix\n windows\r \n last" ) . unwrap( ) ,
34
34
vec![
35
- ( r"unix" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
36
- ( r"windows" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 2 ) ,
37
- ( r"last" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 3 )
35
+ ( pattern ( r"unix" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
36
+ ( pattern ( r"windows" ) , Mode :: NO_SUB_DIR , vec![ ] , 2 ) ,
37
+ ( pattern ( r"last" ) , Mode :: NO_SUB_DIR , vec![ ] , 3 )
38
38
]
39
39
) ;
40
40
}
@@ -51,47 +51,44 @@ fn comment_lines_are_ignored() {
51
51
52
52
#[ test]
53
53
fn leading_whitespace_is_ignored ( ) {
54
- assert_eq ! ( line( " \r \t p" ) , ( r"p" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
55
- assert_eq ! ( line( " \r \t \" p\" " ) , ( r"p" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
54
+ assert_eq ! ( line( " \r \t p" ) , ( pattern ( r"p" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
55
+ assert_eq ! ( line( " \r \t \" p\" " ) , ( pattern ( r"p" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
56
56
}
57
57
58
58
#[ test]
59
59
fn comment_can_be_escaped_like_gitignore_or_quoted ( ) {
60
60
assert_eq ! (
61
61
line( r"\#hello" ) ,
62
- ( r"#hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
62
+ ( pattern ( r"#hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
63
63
"undocumented, but definitely works"
64
64
) ;
65
- assert_eq ! ( line( "\" # hello\" " ) , ( r"# hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
65
+ assert_eq ! ( line( "\" # hello\" " ) , ( pattern ( r"# hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
66
66
}
67
67
68
68
#[ test]
69
69
fn exclamation_marks_must_be_escaped_or_error_unlike_gitignore ( ) {
70
- assert_eq ! ( line( r"\!hello" ) , ( r"!hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
70
+ assert_eq ! ( line( r"\!hello" ) , ( pattern ( r"!hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ) ;
71
71
assert ! ( matches!(
72
72
try_line( r"!hello" ) ,
73
- Err ( parse:: attribute :: Error :: PatternNegation { line_number: 1 , .. } )
73
+ Err ( parse:: Error :: PatternNegation { line_number: 1 , .. } )
74
74
) ) ;
75
75
assert ! (
76
76
matches!(
77
77
try_line( r#""!hello""# ) ,
78
- Err ( parse:: attribute :: Error :: PatternNegation { line_number: 1 , .. } ) ,
78
+ Err ( parse:: Error :: PatternNegation { line_number: 1 , .. } ) ,
79
79
) ,
80
80
"even in quotes they trigger…"
81
81
) ;
82
82
assert_eq ! (
83
83
line( r#""\\!hello""# ) ,
84
- ( r"!hello" . into ( ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
84
+ ( pattern ( r"!hello" ) , Mode :: NO_SUB_DIR , vec![ ] , 1 ) ,
85
85
"…and must be double-escaped, once to get through quote, then to get through parse ignore line"
86
86
) ;
87
87
}
88
88
89
89
#[ test]
90
90
fn invalid_escapes_in_quotes_are_an_error ( ) {
91
- assert ! ( matches!(
92
- try_line( r#""\!hello""# ) ,
93
- Err ( parse:: attribute:: Error :: Unquote ( _) ) ,
94
- ) , ) ;
91
+ assert ! ( matches!( try_line( r#""\!hello""# ) , Err ( parse:: Error :: Unquote ( _) ) , ) , ) ;
95
92
}
96
93
97
94
#[ test]
@@ -104,19 +101,19 @@ fn custom_macros_can_be_defined() {
104
101
fn attribute_names_must_not_begin_with_dash_and_must_be_ascii_only ( ) {
105
102
assert ! ( matches!(
106
103
try_line( r"p !-a" ) ,
107
- Err ( parse:: attribute :: Error :: AttributeName { line_number: 1 , .. } )
104
+ Err ( parse:: Error :: AttributeName { line_number: 1 , .. } )
108
105
) ) ;
109
106
assert ! (
110
107
matches!(
111
108
try_line( r#"p !!a"# ) ,
112
- Err ( parse:: attribute :: Error :: AttributeName { line_number: 1 , .. } )
109
+ Err ( parse:: Error :: AttributeName { line_number: 1 , .. } )
113
110
) ,
114
111
"exclamation marks aren't allowed either"
115
112
) ;
116
113
assert ! (
117
114
matches!(
118
115
try_line( r#"p 你好"# ) ,
119
- Err ( parse:: attribute :: Error :: AttributeName { line_number: 1 , .. } )
116
+ Err ( parse:: Error :: AttributeName { line_number: 1 , .. } )
120
117
) ,
121
118
"nor is utf-8 encoded characters - gitoxide could consider to relax this when established"
122
119
) ;
@@ -126,32 +123,32 @@ fn attribute_names_must_not_begin_with_dash_and_must_be_ascii_only() {
126
123
fn attributes_are_parsed_behind_various_whitespace_characters ( ) {
127
124
assert_eq ! (
128
125
line( r#"p a b"# ) ,
129
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
126
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
130
127
"behind space"
131
128
) ;
132
129
assert_eq ! (
133
130
line( r#""p" a b"# ) ,
134
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
131
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
135
132
"behind space"
136
133
) ;
137
134
assert_eq ! (
138
135
line( "p\t a\t b" ) ,
139
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
136
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
140
137
"behind tab"
141
138
) ;
142
139
assert_eq ! (
143
140
line( "\" p\" \t a\t b" ) ,
144
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
141
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
145
142
"behind tab"
146
143
) ;
147
144
assert_eq ! (
148
145
line( "p \t a \t b" ) ,
149
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
146
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
150
147
"behind a mix of space and tab"
151
148
) ;
152
149
assert_eq ! (
153
150
line( "\" p\" \t a \t b" ) ,
154
- ( "p" . into ( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
151
+ ( pattern ( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) , set( "b" ) ] , 1 ) ,
155
152
"behind a mix of space and tab"
156
153
) ;
157
154
}
@@ -161,7 +158,7 @@ fn attributes_come_in_different_flavors_due_to_prefixes() {
161
158
assert_eq ! (
162
159
line( r#"p set -unset !unspecified -set"# ) ,
163
160
(
164
- "p" . into ( ) ,
161
+ pattern ( "p" ) ,
165
162
Mode :: NO_SUB_DIR ,
166
163
vec![ set( "set" ) , unset( "unset" ) , unspecified( "unspecified" ) , unset( "set" ) ] ,
167
164
1
@@ -175,7 +172,7 @@ fn attributes_can_have_values() {
175
172
assert_eq ! (
176
173
line( r#"p a=one b=2 c=你好 "# ) ,
177
174
(
178
- "p" . into ( ) ,
175
+ pattern ( "p" ) ,
179
176
Mode :: NO_SUB_DIR ,
180
177
vec![ value( "a" , "one" ) , value( "b" , "2" ) , value( "c" , "你好" ) ] ,
181
178
1
@@ -189,7 +186,7 @@ fn attributes_see_state_adjustments_over_value_assignments() {
189
186
assert_eq ! (
190
187
line( r#"p set -unset=a !unspecified=b"# ) ,
191
188
(
192
- "p" . into ( ) ,
189
+ pattern ( "p" ) ,
193
190
Mode :: NO_SUB_DIR ,
194
191
vec![ set( "set" ) , unset( "unset" ) , unspecified( "unspecified" ) ] ,
195
192
1
@@ -199,12 +196,15 @@ fn attributes_see_state_adjustments_over_value_assignments() {
199
196
200
197
#[ test]
201
198
fn trailing_whitespace_in_attributes_is_ignored ( ) {
202
- assert_eq ! ( line( "p a \r \t " ) , ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) , ) ;
203
- assert_eq ! ( line( "\" p\" a \r \t " ) , ( "p" . into( ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) , ) ;
199
+ assert_eq ! ( line( "p a \r \t " ) , ( pattern( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) , ) ;
200
+ assert_eq ! (
201
+ line( "\" p\" a \r \t " ) ,
202
+ ( pattern( "p" ) , Mode :: NO_SUB_DIR , vec![ set( "a" ) ] , 1 ) ,
203
+ ) ;
204
204
}
205
205
206
206
type ExpandedAttribute < ' a > = (
207
- BString ,
207
+ parse :: Kind ,
208
208
ignore:: pattern:: Mode ,
209
209
Vec < ( & ' a BStr , git_attributes:: State < ' a > ) > ,
210
210
usize ,
@@ -226,7 +226,11 @@ fn value<'a, 'b>(attr: &'a str, value: &'b str) -> (&'a BStr, State<'b>) {
226
226
( attr. as_bytes ( ) . as_bstr ( ) , State :: Value ( value. as_bytes ( ) . as_bstr ( ) ) )
227
227
}
228
228
229
- fn try_line ( input : & str ) -> Result < ExpandedAttribute , parse:: attribute:: Error > {
229
+ fn pattern ( name : & str ) -> parse:: Kind {
230
+ parse:: Kind :: Pattern ( name. into ( ) )
231
+ }
232
+
233
+ fn try_line ( input : & str ) -> Result < ExpandedAttribute , parse:: Error > {
230
234
let mut lines = git_attributes:: parse ( input. as_bytes ( ) ) ;
231
235
let res = expand ( lines. next ( ) . unwrap ( ) ) ?;
232
236
assert ! ( lines. next( ) . is_none( ) , "expected only one line" ) ;
@@ -240,13 +244,13 @@ fn line(input: &str) -> ExpandedAttribute {
240
244
res
241
245
}
242
246
243
- fn try_lines ( input : & str ) -> Result < Vec < ExpandedAttribute > , parse:: attribute :: Error > {
247
+ fn try_lines ( input : & str ) -> Result < Vec < ExpandedAttribute > , parse:: Error > {
244
248
git_attributes:: parse ( input. as_bytes ( ) ) . map ( expand) . collect ( )
245
249
}
246
250
247
251
fn expand (
248
- input : Result < ( BString , ignore:: pattern:: Mode , parse:: attribute :: Iter < ' _ > , usize ) , parse:: attribute :: Error > ,
249
- ) -> Result < ExpandedAttribute < ' _ > , parse:: attribute :: Error > {
252
+ input : Result < ( parse :: Kind , ignore:: pattern:: Mode , parse:: Iter < ' _ > , usize ) , parse:: Error > ,
253
+ ) -> Result < ExpandedAttribute < ' _ > , parse:: Error > {
250
254
let ( pattern, mode, attrs, line_no) = input?;
251
255
let attrs = attrs. collect :: < Result < Vec < _ > , _ > > ( ) ?;
252
256
Ok ( ( pattern, mode, attrs, line_no) )
0 commit comments