@@ -2,16 +2,15 @@ use std::ffi::{OsStr, OsString};
2
2
3
3
use regex:: Regex ;
4
4
5
- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
5
+ #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
6
6
pub enum Format {
7
+ #[ default]
7
8
Normal ,
8
9
Unified ,
9
10
Context ,
10
11
Ed ,
11
12
}
12
13
13
- const DEFAULT_TABSIZE : usize = 8 ;
14
-
15
14
#[ cfg( unix) ]
16
15
fn osstr_bytes ( osstr : & OsStr ) -> & [ u8 ] {
17
16
use std:: os:: unix:: ffi:: OsStrExt ;
@@ -35,22 +34,33 @@ pub struct Params {
35
34
pub tabsize : usize ,
36
35
}
37
36
37
+ impl Default for Params {
38
+ fn default ( ) -> Self {
39
+ Self {
40
+ from : OsString :: default ( ) ,
41
+ to : OsString :: default ( ) ,
42
+ format : Format :: default ( ) ,
43
+ context_count : 3 ,
44
+ report_identical_files : false ,
45
+ brief : false ,
46
+ expand_tabs : false ,
47
+ tabsize : 8 ,
48
+ }
49
+ }
50
+ }
51
+
38
52
pub fn parse_params < I : IntoIterator < Item = OsString > > ( opts : I ) -> Result < Params , String > {
39
53
let mut opts = opts. into_iter ( ) ;
40
54
// parse CLI
41
55
42
56
let Some ( exe) = opts. next ( ) else {
43
57
return Err ( "Usage: <exe> <from> <to>" . to_string ( ) ) ;
44
58
} ;
59
+ let mut params = Params :: default ( ) ;
45
60
let mut from = None ;
46
61
let mut to = None ;
47
62
let mut format = None ;
48
- let mut context_count = 3 ;
49
- let mut report_identical_files = false ;
50
- let mut brief = false ;
51
- let mut expand_tabs = false ;
52
63
let tabsize_re = Regex :: new ( r"^--tabsize=(?<num>\d+)$" ) . unwrap ( ) ;
53
- let mut tabsize = DEFAULT_TABSIZE ;
54
64
while let Some ( param) = opts. next ( ) {
55
65
if param == "--" {
56
66
break ;
@@ -66,15 +76,15 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
66
76
continue ;
67
77
}
68
78
if param == "-s" || param == "--report-identical-files" {
69
- report_identical_files = true ;
79
+ params . report_identical_files = true ;
70
80
continue ;
71
81
}
72
82
if param == "-q" || param == "--brief" {
73
- brief = true ;
83
+ params . brief = true ;
74
84
continue ;
75
85
}
76
86
if param == "-t" || param == "--expand-tabs" {
77
- expand_tabs = true ;
87
+ params . expand_tabs = true ;
78
88
continue ;
79
89
}
80
90
if tabsize_re. is_match ( param. to_string_lossy ( ) . as_ref ( ) ) {
@@ -87,7 +97,7 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
87
97
. name ( "num" )
88
98
. unwrap ( )
89
99
. as_str ( ) ;
90
- tabsize = match tabsize_str. parse :: < usize > ( ) {
100
+ params . tabsize = match tabsize_str. parse :: < usize > ( ) {
91
101
Ok ( num) => num,
92
102
Err ( _) => return Err ( format ! ( "invalid tabsize «{}»" , tabsize_str) ) ,
93
103
} ;
@@ -101,10 +111,10 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
101
111
while let Some ( b) = bit. next ( ) {
102
112
match b {
103
113
b'0' ..=b'9' => {
104
- context_count = ( b - b'0' ) as usize ;
114
+ params . context_count = ( b - b'0' ) as usize ;
105
115
while let Some ( b'0' ..=b'9' ) = bit. peek ( ) {
106
- context_count *= 10 ;
107
- context_count += ( bit. next ( ) . unwrap ( ) - b'0' ) as usize ;
116
+ params . context_count *= 10 ;
117
+ params . context_count += ( bit. next ( ) . unwrap ( ) - b'0' ) as usize ;
108
118
}
109
119
}
110
120
b'c' => {
@@ -138,7 +148,7 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
138
148
if let Some ( context_count_maybe) =
139
149
context_count_maybe. and_then ( |x| x. parse ( ) . ok ( ) )
140
150
{
141
- context_count = context_count_maybe;
151
+ params . context_count = context_count_maybe;
142
152
break ;
143
153
}
144
154
return Err ( "Invalid context count" . to_string ( ) ) ;
@@ -154,31 +164,22 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
154
164
return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
155
165
}
156
166
}
157
- let from = if let Some ( from) = from {
167
+ params . from = if let Some ( from) = from {
158
168
from
159
169
} else if let Some ( param) = opts. next ( ) {
160
170
param
161
171
} else {
162
172
return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
163
173
} ;
164
- let to = if let Some ( to) = to {
174
+ params . to = if let Some ( to) = to {
165
175
to
166
176
} else if let Some ( param) = opts. next ( ) {
167
177
param
168
178
} else {
169
179
return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
170
180
} ;
171
- let format = format. unwrap_or ( Format :: Normal ) ;
172
- Ok ( Params {
173
- from,
174
- to,
175
- format,
176
- context_count,
177
- report_identical_files,
178
- brief,
179
- expand_tabs,
180
- tabsize,
181
- } )
181
+ params. format = format. unwrap_or ( Format :: default ( ) ) ;
182
+ Ok ( params)
182
183
}
183
184
184
185
#[ cfg( test) ]
@@ -193,12 +194,7 @@ mod tests {
193
194
Ok ( Params {
194
195
from: os( "foo" ) ,
195
196
to: os( "bar" ) ,
196
- format: Format :: Normal ,
197
- context_count: 3 ,
198
- report_identical_files: false ,
199
- brief: false ,
200
- expand_tabs: false ,
201
- tabsize: DEFAULT_TABSIZE ,
197
+ ..Default :: default ( )
202
198
} ) ,
203
199
parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
204
200
) ;
@@ -210,11 +206,7 @@ mod tests {
210
206
from: os( "foo" ) ,
211
207
to: os( "bar" ) ,
212
208
format: Format :: Ed ,
213
- context_count: 3 ,
214
- report_identical_files: false ,
215
- brief: false ,
216
- expand_tabs: false ,
217
- tabsize: DEFAULT_TABSIZE ,
209
+ ..Default :: default ( )
218
210
} ) ,
219
211
parse_params( [ os( "diff" ) , os( "-e" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
220
212
) ;
@@ -227,10 +219,7 @@ mod tests {
227
219
to: os( "bar" ) ,
228
220
format: Format :: Unified ,
229
221
context_count: 54 ,
230
- report_identical_files: false ,
231
- brief: false ,
232
- expand_tabs: false ,
233
- tabsize: DEFAULT_TABSIZE ,
222
+ ..Default :: default ( )
234
223
} ) ,
235
224
parse_params(
236
225
[ os( "diff" ) , os( "-u54" ) , os( "foo" ) , os( "bar" ) ]
@@ -244,10 +233,7 @@ mod tests {
244
233
to: os( "bar" ) ,
245
234
format: Format :: Unified ,
246
235
context_count: 54 ,
247
- report_identical_files: false ,
248
- brief: false ,
249
- expand_tabs: false ,
250
- tabsize: DEFAULT_TABSIZE ,
236
+ ..Default :: default ( )
251
237
} ) ,
252
238
parse_params(
253
239
[ os( "diff" ) , os( "-U54" ) , os( "foo" ) , os( "bar" ) ]
@@ -261,10 +247,7 @@ mod tests {
261
247
to: os( "bar" ) ,
262
248
format: Format :: Unified ,
263
249
context_count: 54 ,
264
- report_identical_files: false ,
265
- brief: false ,
266
- expand_tabs: false ,
267
- tabsize: DEFAULT_TABSIZE ,
250
+ ..Default :: default ( )
268
251
} ) ,
269
252
parse_params(
270
253
[ os( "diff" ) , os( "-U" ) , os( "54" ) , os( "foo" ) , os( "bar" ) ]
@@ -278,10 +261,7 @@ mod tests {
278
261
to: os( "bar" ) ,
279
262
format: Format :: Context ,
280
263
context_count: 54 ,
281
- report_identical_files: false ,
282
- brief: false ,
283
- expand_tabs: false ,
284
- tabsize: DEFAULT_TABSIZE ,
264
+ ..Default :: default ( )
285
265
} ) ,
286
266
parse_params(
287
267
[ os( "diff" ) , os( "-c54" ) , os( "foo" ) , os( "bar" ) ]
@@ -296,38 +276,25 @@ mod tests {
296
276
Ok ( Params {
297
277
from: os( "foo" ) ,
298
278
to: os( "bar" ) ,
299
- format: Format :: Normal ,
300
- context_count: 3 ,
301
- report_identical_files: false ,
302
- brief: false ,
303
- expand_tabs: false ,
304
- tabsize: DEFAULT_TABSIZE ,
279
+ ..Default :: default ( )
305
280
} ) ,
306
281
parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
307
282
) ;
308
283
assert_eq ! (
309
284
Ok ( Params {
310
285
from: os( "foo" ) ,
311
286
to: os( "bar" ) ,
312
- format: Format :: Normal ,
313
- context_count: 3 ,
314
287
report_identical_files: true ,
315
- brief: false ,
316
- expand_tabs: false ,
317
- tabsize: DEFAULT_TABSIZE ,
288
+ ..Default :: default ( )
318
289
} ) ,
319
290
parse_params( [ os( "diff" ) , os( "-s" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
320
291
) ;
321
292
assert_eq ! (
322
293
Ok ( Params {
323
294
from: os( "foo" ) ,
324
295
to: os( "bar" ) ,
325
- format: Format :: Normal ,
326
- context_count: 3 ,
327
296
report_identical_files: true ,
328
- brief: false ,
329
- expand_tabs: false ,
330
- tabsize: DEFAULT_TABSIZE ,
297
+ ..Default :: default ( )
331
298
} ) ,
332
299
parse_params(
333
300
[
@@ -347,38 +314,25 @@ mod tests {
347
314
Ok ( Params {
348
315
from: os( "foo" ) ,
349
316
to: os( "bar" ) ,
350
- format: Format :: Normal ,
351
- context_count: 3 ,
352
- report_identical_files: false ,
353
- brief: false ,
354
- expand_tabs: false ,
355
- tabsize: 8 ,
317
+ ..Default :: default ( )
356
318
} ) ,
357
319
parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
358
320
) ;
359
321
assert_eq ! (
360
322
Ok ( Params {
361
323
from: os( "foo" ) ,
362
324
to: os( "bar" ) ,
363
- format: Format :: Normal ,
364
- context_count: 3 ,
365
- report_identical_files: false ,
366
325
brief: true ,
367
- expand_tabs: false ,
368
- tabsize: 8 ,
326
+ ..Default :: default ( )
369
327
} ) ,
370
328
parse_params( [ os( "diff" ) , os( "-q" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
371
329
) ;
372
330
assert_eq ! (
373
331
Ok ( Params {
374
332
from: os( "foo" ) ,
375
333
to: os( "bar" ) ,
376
- format: Format :: Normal ,
377
- context_count: 3 ,
378
- report_identical_files: false ,
379
334
brief: true ,
380
- expand_tabs: false ,
381
- tabsize: 8 ,
335
+ ..Default :: default ( )
382
336
} ) ,
383
337
parse_params(
384
338
[ os( "diff" ) , os( "--brief" ) , os( "foo" ) , os( "bar" ) , ]
@@ -393,12 +347,7 @@ mod tests {
393
347
Ok ( Params {
394
348
from: os( "foo" ) ,
395
349
to: os( "bar" ) ,
396
- format: Format :: Normal ,
397
- context_count: 3 ,
398
- report_identical_files: false ,
399
- brief: false ,
400
- expand_tabs: false ,
401
- tabsize: DEFAULT_TABSIZE ,
350
+ ..Default :: default ( )
402
351
} ) ,
403
352
parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
404
353
) ;
@@ -407,12 +356,8 @@ mod tests {
407
356
Ok ( Params {
408
357
from: os( "foo" ) ,
409
358
to: os( "bar" ) ,
410
- format: Format :: Normal ,
411
- context_count: 3 ,
412
- report_identical_files: false ,
413
- brief: false ,
414
359
expand_tabs: true ,
415
- tabsize : DEFAULT_TABSIZE ,
360
+ .. Default :: default ( )
416
361
} ) ,
417
362
parse_params(
418
363
[ os( "diff" ) , os( option) , os( "foo" ) , os( "bar" ) ]
@@ -428,25 +373,16 @@ mod tests {
428
373
Ok ( Params {
429
374
from: os( "foo" ) ,
430
375
to: os( "bar" ) ,
431
- format: Format :: Normal ,
432
- context_count: 3 ,
433
- report_identical_files: false ,
434
- brief: false ,
435
- expand_tabs: false ,
436
- tabsize: DEFAULT_TABSIZE ,
376
+ ..Default :: default ( )
437
377
} ) ,
438
378
parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
439
379
) ;
440
380
assert_eq ! (
441
381
Ok ( Params {
442
382
from: os( "foo" ) ,
443
383
to: os( "bar" ) ,
444
- format: Format :: Normal ,
445
- context_count: 3 ,
446
- report_identical_files: false ,
447
- brief: false ,
448
- expand_tabs: false ,
449
384
tabsize: 0 ,
385
+ ..Default :: default ( )
450
386
} ) ,
451
387
parse_params(
452
388
[ os( "diff" ) , os( "--tabsize=0" ) , os( "foo" ) , os( "bar" ) ]
@@ -458,12 +394,8 @@ mod tests {
458
394
Ok ( Params {
459
395
from: os( "foo" ) ,
460
396
to: os( "bar" ) ,
461
- format: Format :: Normal ,
462
- context_count: 3 ,
463
- report_identical_files: false ,
464
- brief: false ,
465
- expand_tabs: false ,
466
397
tabsize: 42 ,
398
+ ..Default :: default ( )
467
399
} ) ,
468
400
parse_params(
469
401
[ os( "diff" ) , os( "--tabsize=42" ) , os( "foo" ) , os( "bar" ) ]
@@ -519,12 +451,7 @@ mod tests {
519
451
Ok ( Params {
520
452
from: os( "-g" ) ,
521
453
to: os( "-h" ) ,
522
- format: Format :: Normal ,
523
- context_count: 3 ,
524
- report_identical_files: false ,
525
- brief: false ,
526
- expand_tabs: false ,
527
- tabsize: DEFAULT_TABSIZE ,
454
+ ..Default :: default ( )
528
455
} ) ,
529
456
parse_params( [ os( "diff" ) , os( "--" ) , os( "-g" ) , os( "-h" ) ] . iter( ) . cloned( ) )
530
457
) ;
0 commit comments