1
1
#![ deny( warnings) ]
2
+ #![ feature( conservative_impl_trait) ]
2
3
3
4
extern crate chrono;
4
5
extern crate clap;
@@ -25,7 +26,7 @@ use std::fs::File;
25
26
#[ cfg( unix) ]
26
27
use std:: os:: unix:: ffi:: OsStringExt ;
27
28
28
- use clap:: { App , Arg } ;
29
+ use clap:: { Arg , App , ArgMatches } ;
29
30
use log:: { LogRecord , LogLevelFilter } ;
30
31
use ref_slice:: ref_slice_mut;
31
32
@@ -93,56 +94,18 @@ fn run() -> Result<()> {
93
94
} ) )
94
95
. get_matches ( ) ;
95
96
96
- let pipe = PathBuf :: from ( matches. value_of ( "PATH" ) . unwrap ( ) ) ;
97
- let pipe_ = pipe. display ( ) ;
98
-
99
97
let stim_port = matches. value_of ( "port" )
100
98
. unwrap ( ) // We supplied a default value
101
99
. parse :: < u8 > ( )
102
100
. expect ( "Arg validator should ensure this parses" ) ;
103
101
104
- if pipe. exists ( ) {
105
- try!( fs:: remove_file ( & pipe)
106
- . chain_err ( || format ! ( "couldn't remove {}" , pipe_) ) ) ;
107
- }
108
-
109
- let mut stream = match ( ) {
110
- #[ cfg( unix) ]
111
- ( ) => {
112
- let cpipe =
113
- try!( CString :: new ( pipe. clone ( ) . into_os_string ( ) . into_vec ( ) )
114
- . chain_err ( || {
115
- format ! ( "error converting {} to a C string" , pipe_)
116
- } ) ) ;
117
-
118
- match unsafe { libc:: mkfifo ( cpipe. as_ptr ( ) , 0o644 ) } {
119
- 0 => { }
120
- e => {
121
- try!( Err ( io:: Error :: from_raw_os_error ( e) ) . chain_err ( || {
122
- format ! ( "couldn't create a named pipe in {}" , pipe_)
123
- } ) )
124
- }
125
- }
126
-
127
- try!( File :: open ( & pipe)
128
- . chain_err ( || format ! ( "couldn't open {}" , pipe_) ) )
129
- }
130
- #[ cfg( not( unix) ) ]
131
- ( ) => {
132
- try!( OpenOptions :: new ( )
133
- . create ( true )
134
- . read ( true )
135
- . write ( true )
136
- . open ( & pipe)
137
- . chain_err ( || format ! ( "couldn't open {}" , pipe_) ) )
138
- }
139
- } ;
140
-
141
- let mut header = 0 ;
102
+ let mut stream = open_read ( & matches) ?;
142
103
143
104
let stdout = io:: stdout ( ) ;
144
105
let mut stdout = stdout. lock ( ) ;
145
106
loop {
107
+ let mut header = 0 ;
108
+
146
109
if let Err ( e) = ( || {
147
110
try!( stream. read_exact ( ref_slice_mut ( & mut header) ) ) ;
148
111
let port = header >> 3 ;
@@ -186,3 +149,44 @@ fn run() -> Result<()> {
186
149
}
187
150
}
188
151
}
152
+
153
+ fn open_read ( matches : & ArgMatches ) -> Result < impl io:: Read > {
154
+ let pipe = PathBuf :: from ( matches. value_of ( "PATH" ) . unwrap ( ) ) ;
155
+ let pipe_ = pipe. display ( ) ;
156
+
157
+ if pipe. exists ( ) {
158
+ try!( fs:: remove_file ( & pipe)
159
+ . chain_err ( || format ! ( "couldn't remove {}" , pipe_) ) ) ;
160
+ }
161
+
162
+ Ok (
163
+ if cfg ! ( unix) {
164
+ // Use a named pipe.
165
+ let cpipe =
166
+ try!( CString :: new ( pipe. clone ( ) . into_os_string ( ) . into_vec ( ) )
167
+ . chain_err ( || {
168
+ format ! ( "error converting {} to a C string" , pipe_)
169
+ } ) ) ;
170
+
171
+ match unsafe { libc:: mkfifo ( cpipe. as_ptr ( ) , 0o644 ) } {
172
+ 0 => { }
173
+ e => {
174
+ try!( Err ( io:: Error :: from_raw_os_error ( e) ) . chain_err ( || {
175
+ format ! ( "couldn't create a named pipe in {}" , pipe_)
176
+ } ) )
177
+ }
178
+ }
179
+
180
+ try!( File :: open ( & pipe)
181
+ . chain_err ( || format ! ( "couldn't open {}" , pipe_) ) )
182
+ } else {
183
+ // Not unix.
184
+ try!( OpenOptions :: new ( )
185
+ . create ( true )
186
+ . read ( true )
187
+ . write ( true )
188
+ . open ( & pipe)
189
+ . chain_err ( || format ! ( "couldn't open {}" , pipe_) ) )
190
+ }
191
+ )
192
+ }
0 commit comments