1- use std:: ffi:: { OsStr , OsString } ;
1+ use std:: ffi:: OsString ;
22
33use regex:: Regex ;
44
@@ -11,17 +11,6 @@ pub enum Format {
1111 Ed ,
1212}
1313
14- #[ cfg( unix) ]
15- fn osstr_bytes ( osstr : & OsStr ) -> & [ u8 ] {
16- use std:: os:: unix:: ffi:: OsStrExt ;
17- osstr. as_bytes ( )
18- }
19-
20- #[ cfg( not( unix) ) ]
21- fn osstr_bytes ( osstr : & OsStr ) -> Vec < u8 > {
22- osstr. to_string_lossy ( ) . bytes ( ) . collect ( )
23- }
24-
2514#[ derive( Clone , Debug , Eq , PartialEq ) ]
2615pub struct Params {
2716 pub from : OsString ,
@@ -92,6 +81,20 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
9281 params. expand_tabs = true ;
9382 continue ;
9483 }
84+ if param == "--normal" {
85+ if format. is_some ( ) && format != Some ( Format :: Normal ) {
86+ return Err ( "Conflicting output style options" . to_string ( ) ) ;
87+ }
88+ format = Some ( Format :: Normal ) ;
89+ continue ;
90+ }
91+ if param == "-e" || param == "--ed" {
92+ if format. is_some ( ) && format != Some ( Format :: Ed ) {
93+ return Err ( "Conflicting output style options" . to_string ( ) ) ;
94+ }
95+ format = Some ( Format :: Ed ) ;
96+ continue ;
97+ }
9598 if tabsize_re. is_match ( param. to_string_lossy ( ) . as_ref ( ) ) {
9699 // Because param matches the regular expression,
97100 // it is safe to assume it is valid UTF-8.
@@ -176,21 +179,10 @@ pub fn parse_params<I: IntoIterator<Item = OsString>>(opts: I) -> Result<Params,
176179 }
177180 continue ;
178181 }
179- let p = osstr_bytes ( & param) ;
180- if p. first ( ) == Some ( & b'-' ) && p. get ( 1 ) != Some ( & b'-' ) {
181- let mut bit = p[ 1 ..] . iter ( ) . copied ( ) . peekable ( ) ;
182- while let Some ( b) = bit. next ( ) {
183- match b {
184- b'e' => {
185- if format. is_some ( ) && format != Some ( Format :: Ed ) {
186- return Err ( "Conflicting output style options" . to_string ( ) ) ;
187- }
188- format = Some ( Format :: Ed ) ;
189- }
190- _ => return Err ( format ! ( "Unknown option: {}" , String :: from_utf8_lossy( & [ b] ) ) ) ,
191- }
192- }
193- } else if from. is_none ( ) {
182+ if param. to_string_lossy ( ) . starts_with ( '-' ) {
183+ return Err ( format ! ( "Unknown option: {:?}" , param) ) ;
184+ }
185+ if from. is_none ( ) {
194186 from = Some ( param) ;
195187 } else if to. is_none ( ) {
196188 to = Some ( param) ;
@@ -235,20 +227,34 @@ mod tests {
235227 } ) ,
236228 parse_params( [ os( "diff" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
237229 ) ;
238- }
239- #[ test]
240- fn basics_ed ( ) {
241230 assert_eq ! (
242231 Ok ( Params {
243232 from: os( "foo" ) ,
244233 to: os( "bar" ) ,
245- format: Format :: Ed ,
246234 ..Default :: default ( )
247235 } ) ,
248- parse_params( [ os( "diff" ) , os( "-e" ) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
236+ parse_params(
237+ [ os( "diff" ) , os( "--normal" ) , os( "foo" ) , os( "bar" ) ]
238+ . iter( )
239+ . cloned( )
240+ )
249241 ) ;
250242 }
251243 #[ test]
244+ fn basics_ed ( ) {
245+ for arg in [ "-e" , "--ed" ] {
246+ assert_eq ! (
247+ Ok ( Params {
248+ from: os( "foo" ) ,
249+ to: os( "bar" ) ,
250+ format: Format :: Ed ,
251+ ..Default :: default ( )
252+ } ) ,
253+ parse_params( [ os( "diff" ) , os( arg) , os( "foo" ) , os( "bar" ) ] . iter( ) . cloned( ) )
254+ ) ;
255+ }
256+ }
257+ #[ test]
252258 fn context_valid ( ) {
253259 for args in [ vec ! [ "-c" ] , vec ! [ "--context" ] , vec ! [ "--context=" ] ] {
254260 let mut params = vec ! [ "diff" ] ;
@@ -655,7 +661,15 @@ mod tests {
655661 }
656662 #[ test]
657663 fn conflicting_output_styles ( ) {
658- for ( arg1, arg2) in [ ( "-u" , "-c" ) , ( "-u" , "-e" ) , ( "-c" , "-u" ) , ( "-c" , "-U42" ) ] {
664+ for ( arg1, arg2) in [
665+ ( "-u" , "-c" ) ,
666+ ( "-u" , "-e" ) ,
667+ ( "-c" , "-u" ) ,
668+ ( "-c" , "-U42" ) ,
669+ ( "-u" , "--normal" ) ,
670+ ( "--normal" , "-e" ) ,
671+ ( "--context" , "--normal" ) ,
672+ ] {
659673 assert ! ( parse_params(
660674 [ os( "diff" ) , os( arg1) , os( arg2) , os( "foo" ) , os( "bar" ) ]
661675 . iter( )
0 commit comments