@@ -10,7 +10,10 @@ use std::ffi::OsStr;
10
10
use std:: fs:: { File , Metadata } ;
11
11
use std:: io:: { Seek , SeekFrom } ;
12
12
#[ cfg( unix) ]
13
- use std:: os:: unix:: fs:: { FileTypeExt , MetadataExt } ;
13
+ use std:: os:: unix:: {
14
+ fs:: { FileTypeExt , MetadataExt } ,
15
+ prelude:: OsStrExt ,
16
+ } ;
14
17
use std:: path:: { Path , PathBuf } ;
15
18
use uucore:: error:: UResult ;
16
19
@@ -20,6 +23,30 @@ pub enum InputKind {
20
23
Stdin ,
21
24
}
22
25
26
+ #[ cfg( unix) ]
27
+ impl From < & OsStr > for InputKind {
28
+ fn from ( value : & OsStr ) -> Self {
29
+ const DASH : [ u8 ; 1 ] = [ b'-' ] ;
30
+
31
+ if value. as_bytes ( ) == DASH {
32
+ Self :: Stdin
33
+ } else {
34
+ Self :: File ( PathBuf :: from ( value) )
35
+ }
36
+ }
37
+ }
38
+
39
+ #[ cfg( not( unix) ) ]
40
+ impl From < & OsStr > for InputKind {
41
+ fn from ( value : & OsStr ) -> Self {
42
+ if value == OsStr :: new ( text:: DASH ) {
43
+ Self :: Stdin
44
+ } else {
45
+ Self :: File ( PathBuf :: from ( value) )
46
+ }
47
+ }
48
+ }
49
+
23
50
#[ derive( Debug , Clone ) ]
24
51
pub struct Input {
25
52
kind : InputKind ,
@@ -29,21 +56,11 @@ pub struct Input {
29
56
impl Input {
30
57
pub fn from < T : AsRef < OsStr > > ( string : T ) -> Self {
31
58
let string = string. as_ref ( ) ;
32
- let kind = if string == OsStr :: new ( text:: DASH ) {
33
- InputKind :: Stdin
34
- } else {
35
- InputKind :: File ( PathBuf :: from ( string) )
36
- } ;
37
59
60
+ let kind = string. into ( ) ;
38
61
let display_name = match kind {
39
62
InputKind :: File ( _) => string. to_string_lossy ( ) . to_string ( ) ,
40
- InputKind :: Stdin => {
41
- if cfg ! ( unix) {
42
- text:: STDIN_HEADER . to_string ( )
43
- } else {
44
- string. to_string_lossy ( ) . to_string ( )
45
- }
46
- }
63
+ InputKind :: Stdin => text:: STDIN_HEADER . to_string ( ) ,
47
64
} ;
48
65
49
66
Self { kind, display_name }
0 commit comments