@@ -2,16 +2,15 @@ use std::ffi::{OsStr, OsString};
22
33use regex:: Regex ;
44
5- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
5+ #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
66pub enum Format {
7+ #[ default]
78 Normal ,
89 Unified ,
910 Context ,
1011 Ed ,
1112}
1213
13- const DEFAULT_TABSIZE : usize = 8 ;
14-
1514#[ cfg( unix) ]
1615fn osstr_bytes ( osstr : & OsStr ) -> & [ u8 ] {
1716 use std:: os:: unix:: ffi:: OsStrExt ;
@@ -35,22 +34,33 @@ pub struct Params {
3534 pub tabsize : usize ,
3635}
3736
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+
3852pub fn parse_params < I : IntoIterator < Item = OsString > > ( opts : I ) -> Result < Params , String > {
3953 let mut opts = opts. into_iter ( ) ;
4054 // parse CLI
4155
4256 let Some ( exe) = opts. next ( ) else {
4357 return Err ( "Usage: <exe> <from> <to>" . to_string ( ) ) ;
4458 } ;
59+ let mut params = Params :: default ( ) ;
4560 let mut from = None ;
4661 let mut to = None ;
4762 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 ;
5263 let tabsize_re = Regex :: new ( r"^--tabsize=(?<num>\d+)$" ) . unwrap ( ) ;
53- let mut tabsize = DEFAULT_TABSIZE ;
5464 while let Some ( param) = opts. next ( ) {
5565 if param == "--" {
5666 break ;
@@ -66,15 +76,15 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
6676 continue ;
6777 }
6878 if param == "-s" || param == "--report-identical-files" {
69- report_identical_files = true ;
79+ params . report_identical_files = true ;
7080 continue ;
7181 }
7282 if param == "-q" || param == "--brief" {
73- brief = true ;
83+ params . brief = true ;
7484 continue ;
7585 }
7686 if param == "-t" || param == "--expand-tabs" {
77- expand_tabs = true ;
87+ params . expand_tabs = true ;
7888 continue ;
7989 }
8090 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,
8797 . name ( "num" )
8898 . unwrap ( )
8999 . as_str ( ) ;
90- tabsize = match tabsize_str. parse :: < usize > ( ) {
100+ params . tabsize = match tabsize_str. parse :: < usize > ( ) {
91101 Ok ( num) => num,
92102 Err ( _) => return Err ( format ! ( "invalid tabsize «{}»" , tabsize_str) ) ,
93103 } ;
@@ -101,10 +111,10 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
101111 while let Some ( b) = bit. next ( ) {
102112 match b {
103113 b'0' ..=b'9' => {
104- context_count = ( b - b'0' ) as usize ;
114+ params . context_count = ( b - b'0' ) as usize ;
105115 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 ;
108118 }
109119 }
110120 b'c' => {
@@ -138,7 +148,7 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
138148 if let Some ( context_count_maybe) =
139149 context_count_maybe. and_then ( |x| x. parse ( ) . ok ( ) )
140150 {
141- context_count = context_count_maybe;
151+ params . context_count = context_count_maybe;
142152 break ;
143153 }
144154 return Err ( "Invalid context count" . to_string ( ) ) ;
@@ -154,31 +164,22 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
154164 return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
155165 }
156166 }
157- let from = if let Some ( from) = from {
167+ params . from = if let Some ( from) = from {
158168 from
159169 } else if let Some ( param) = opts. next ( ) {
160170 param
161171 } else {
162172 return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
163173 } ;
164- let to = if let Some ( to) = to {
174+ params . to = if let Some ( to) = to {
165175 to
166176 } else if let Some ( param) = opts. next ( ) {
167177 param
168178 } else {
169179 return Err ( format ! ( "Usage: {} <from> <to>" , exe. to_string_lossy( ) ) ) ;
170180 } ;
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)
182183}
183184
184185#[ cfg( test) ]
@@ -193,12 +194,7 @@ mod tests {
193194 Ok ( Params {
194195 from: os( "foo" ) ,
195196 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 ( )
202198 } ) ,
203199 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
204200 ) ;
@@ -210,11 +206,7 @@ mod tests {
210206 from: os( "foo" ) ,
211207 to: os( "bar" ) ,
212208 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 ( )
218210 } ) ,
219211 parse_params( [ os( "diff" ) , os( "-e" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
220212 ) ;
@@ -227,10 +219,7 @@ mod tests {
227219 to: os( "bar" ) ,
228220 format: Format :: Unified ,
229221 context_count: 54 ,
230- report_identical_files: false ,
231- brief: false ,
232- expand_tabs: false ,
233- tabsize: DEFAULT_TABSIZE ,
222+ ..Default :: default ( )
234223 } ) ,
235224 parse_params(
236225 [ os( "diff" ) , os( "-u54" ) , os( "foo" ) , os( "bar" ) ]
@@ -244,10 +233,7 @@ mod tests {
244233 to: os( "bar" ) ,
245234 format: Format :: Unified ,
246235 context_count: 54 ,
247- report_identical_files: false ,
248- brief: false ,
249- expand_tabs: false ,
250- tabsize: DEFAULT_TABSIZE ,
236+ ..Default :: default ( )
251237 } ) ,
252238 parse_params(
253239 [ os( "diff" ) , os( "-U54" ) , os( "foo" ) , os( "bar" ) ]
@@ -261,10 +247,7 @@ mod tests {
261247 to: os( "bar" ) ,
262248 format: Format :: Unified ,
263249 context_count: 54 ,
264- report_identical_files: false ,
265- brief: false ,
266- expand_tabs: false ,
267- tabsize: DEFAULT_TABSIZE ,
250+ ..Default :: default ( )
268251 } ) ,
269252 parse_params(
270253 [ os( "diff" ) , os( "-U" ) , os( "54" ) , os( "foo" ) , os( "bar" ) ]
@@ -278,10 +261,7 @@ mod tests {
278261 to: os( "bar" ) ,
279262 format: Format :: Context ,
280263 context_count: 54 ,
281- report_identical_files: false ,
282- brief: false ,
283- expand_tabs: false ,
284- tabsize: DEFAULT_TABSIZE ,
264+ ..Default :: default ( )
285265 } ) ,
286266 parse_params(
287267 [ os( "diff" ) , os( "-c54" ) , os( "foo" ) , os( "bar" ) ]
@@ -296,38 +276,25 @@ mod tests {
296276 Ok ( Params {
297277 from: os( "foo" ) ,
298278 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 ( )
305280 } ) ,
306281 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
307282 ) ;
308283 assert_eq ! (
309284 Ok ( Params {
310285 from: os( "foo" ) ,
311286 to: os( "bar" ) ,
312- format: Format :: Normal ,
313- context_count: 3 ,
314287 report_identical_files: true ,
315- brief: false ,
316- expand_tabs: false ,
317- tabsize: DEFAULT_TABSIZE ,
288+ ..Default :: default ( )
318289 } ) ,
319290 parse_params( [ os( "diff" ) , os( "-s" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
320291 ) ;
321292 assert_eq ! (
322293 Ok ( Params {
323294 from: os( "foo" ) ,
324295 to: os( "bar" ) ,
325- format: Format :: Normal ,
326- context_count: 3 ,
327296 report_identical_files: true ,
328- brief: false ,
329- expand_tabs: false ,
330- tabsize: DEFAULT_TABSIZE ,
297+ ..Default :: default ( )
331298 } ) ,
332299 parse_params(
333300 [
@@ -347,38 +314,25 @@ mod tests {
347314 Ok ( Params {
348315 from: os( "foo" ) ,
349316 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 ( )
356318 } ) ,
357319 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
358320 ) ;
359321 assert_eq ! (
360322 Ok ( Params {
361323 from: os( "foo" ) ,
362324 to: os( "bar" ) ,
363- format: Format :: Normal ,
364- context_count: 3 ,
365- report_identical_files: false ,
366325 brief: true ,
367- expand_tabs: false ,
368- tabsize: 8 ,
326+ ..Default :: default ( )
369327 } ) ,
370328 parse_params( [ os( "diff" ) , os( "-q" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
371329 ) ;
372330 assert_eq ! (
373331 Ok ( Params {
374332 from: os( "foo" ) ,
375333 to: os( "bar" ) ,
376- format: Format :: Normal ,
377- context_count: 3 ,
378- report_identical_files: false ,
379334 brief: true ,
380- expand_tabs: false ,
381- tabsize: 8 ,
335+ ..Default :: default ( )
382336 } ) ,
383337 parse_params(
384338 [ os( "diff" ) , os( "--brief" ) , os( "foo" ) , os( "bar" ) , ]
@@ -393,12 +347,7 @@ mod tests {
393347 Ok ( Params {
394348 from: os( "foo" ) ,
395349 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 ( )
402351 } ) ,
403352 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
404353 ) ;
@@ -407,12 +356,8 @@ mod tests {
407356 Ok ( Params {
408357 from: os( "foo" ) ,
409358 to: os( "bar" ) ,
410- format: Format :: Normal ,
411- context_count: 3 ,
412- report_identical_files: false ,
413- brief: false ,
414359 expand_tabs: true ,
415- tabsize : DEFAULT_TABSIZE ,
360+ .. Default :: default ( )
416361 } ) ,
417362 parse_params(
418363 [ os( "diff" ) , os( option) , os( "foo" ) , os( "bar" ) ]
@@ -428,25 +373,16 @@ mod tests {
428373 Ok ( Params {
429374 from: os( "foo" ) ,
430375 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 ( )
437377 } ) ,
438378 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
439379 ) ;
440380 assert_eq ! (
441381 Ok ( Params {
442382 from: os( "foo" ) ,
443383 to: os( "bar" ) ,
444- format: Format :: Normal ,
445- context_count: 3 ,
446- report_identical_files: false ,
447- brief: false ,
448- expand_tabs: false ,
449384 tabsize: 0 ,
385+ ..Default :: default ( )
450386 } ) ,
451387 parse_params(
452388 [ os( "diff" ) , os( "--tabsize=0" ) , os( "foo" ) , os( "bar" ) ]
@@ -458,12 +394,8 @@ mod tests {
458394 Ok ( Params {
459395 from: os( "foo" ) ,
460396 to: os( "bar" ) ,
461- format: Format :: Normal ,
462- context_count: 3 ,
463- report_identical_files: false ,
464- brief: false ,
465- expand_tabs: false ,
466397 tabsize: 42 ,
398+ ..Default :: default ( )
467399 } ) ,
468400 parse_params(
469401 [ os( "diff" ) , os( "--tabsize=42" ) , os( "foo" ) , os( "bar" ) ]
@@ -519,12 +451,7 @@ mod tests {
519451 Ok ( Params {
520452 from: os( "-g" ) ,
521453 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 ( )
528455 } ) ,
529456 parse_params( [ os( "diff" ) , os( "--" ) , os( "-g" ) , os( "-h" ) ] . iter( ) . cloned( ) )
530457 ) ;
0 commit comments