@@ -72,12 +72,10 @@ compile_error!("open is not supported on this platform");
72
72
use std:: {
73
73
ffi:: OsStr ,
74
74
io,
75
- process:: { Command , Output , Stdio } ,
75
+ process:: { Command , Stdio } ,
76
76
thread,
77
77
} ;
78
78
79
- type Result = io:: Result < ( ) > ;
80
-
81
79
/// Open path with the default application.
82
80
///
83
81
/// # Examples
@@ -95,7 +93,7 @@ type Result = io::Result<()>;
95
93
///
96
94
/// A [`std::io::Error`] is returned on failure. Because different operating systems
97
95
/// handle errors differently it is recommend to not match on a certain error.
98
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
96
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
99
97
os:: that ( path)
100
98
}
101
99
@@ -117,14 +115,14 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> Result {
117
115
///
118
116
/// A [`std::io::Error`] is returned on failure. Because different operating systems
119
117
/// handle errors differently it is recommend to not match on a certain error.
120
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
118
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
121
119
os:: with ( path, app)
122
120
}
123
121
124
122
/// Open path with the default application in a new thread.
125
123
///
126
124
/// See documentation of [`that`] for more details.
127
- pub fn that_in_background < T : AsRef < OsStr > > ( path : T ) -> thread:: JoinHandle < Result > {
125
+ pub fn that_in_background < T : AsRef < OsStr > > ( path : T ) -> thread:: JoinHandle < io :: Result < ( ) > > {
128
126
let path = path. as_ref ( ) . to_os_string ( ) ;
129
127
thread:: spawn ( || that ( path) )
130
128
}
@@ -135,7 +133,7 @@ pub fn that_in_background<T: AsRef<OsStr>>(path: T) -> thread::JoinHandle<Result
135
133
pub fn with_in_background < T : AsRef < OsStr > > (
136
134
path : T ,
137
135
app : impl Into < String > ,
138
- ) -> thread:: JoinHandle < Result > {
136
+ ) -> thread:: JoinHandle < io :: Result < ( ) > > {
139
137
let path = path. as_ref ( ) . to_os_string ( ) ;
140
138
let app = app. into ( ) ;
141
139
thread:: spawn ( || with ( path, app) )
@@ -145,68 +143,42 @@ trait IntoResult<T> {
145
143
fn into_result ( self ) -> T ;
146
144
}
147
145
148
- impl IntoResult < Result > for io:: Result < Output > {
149
- fn into_result ( self ) -> Result {
146
+ impl IntoResult < io :: Result < ( ) > > for io:: Result < std :: process :: ExitStatus > {
147
+ fn into_result ( self ) -> io :: Result < ( ) > {
150
148
match self {
151
- Ok ( o) if o. status . success ( ) => Ok ( ( ) ) ,
152
- Ok ( o) => Err ( from_output ( o) ) ,
149
+ Ok ( status) if status. success ( ) => Ok ( ( ) ) ,
150
+ Ok ( status) => Err ( io:: Error :: new (
151
+ io:: ErrorKind :: Other ,
152
+ format ! ( "Launcher failed with {:?}" , status) ,
153
+ ) ) ,
153
154
Err ( err) => Err ( err) ,
154
155
}
155
156
}
156
157
}
157
158
158
159
#[ cfg( windows) ]
159
- impl IntoResult < Result > for std:: os:: raw:: c_int {
160
- fn into_result ( self ) -> Result {
160
+ impl IntoResult < io :: Result < ( ) > > for std:: os:: raw:: c_int {
161
+ fn into_result ( self ) -> io :: Result < ( ) > {
161
162
match self {
162
163
i if i > 32 => Ok ( ( ) ) ,
163
164
_ => Err ( io:: Error :: last_os_error ( ) ) ,
164
165
}
165
166
}
166
167
}
167
168
168
- fn from_output ( output : Output ) -> io:: Error {
169
- let error_msg = match output. stderr . is_empty ( ) {
170
- true => output. status . to_string ( ) ,
171
- false => format ! (
172
- "{} ({})" ,
173
- String :: from_utf8_lossy( & output. stderr) . trim( ) ,
174
- output. status
175
- ) ,
176
- } ;
177
-
178
- io:: Error :: new ( io:: ErrorKind :: Other , error_msg)
179
- }
180
-
181
169
trait CommandExt {
182
- fn output_stderr ( & mut self ) -> io:: Result < Output > ;
170
+ fn status_without_output ( & mut self ) -> io:: Result < std :: process :: ExitStatus > ;
183
171
}
184
172
185
173
impl CommandExt for Command {
186
- fn output_stderr ( & mut self ) -> io:: Result < Output > {
174
+ fn status_without_output ( & mut self ) -> io:: Result < std :: process :: ExitStatus > {
187
175
let mut process = self
188
176
. stdin ( Stdio :: null ( ) )
189
177
. stdout ( Stdio :: null ( ) )
190
- . stderr ( Stdio :: piped ( ) )
178
+ . stderr ( Stdio :: null ( ) )
191
179
. spawn ( ) ?;
192
180
193
- // Consume all stderr - it's open just for a few programs which can't handle it being closed.
194
- use std:: io:: Read ;
195
- let mut stderr = vec ! [ 0 ; 256 ] ;
196
- let mut stderr_src = process. stderr . take ( ) . expect ( "piped stderr" ) ;
197
-
198
- let len = stderr_src. read ( & mut stderr) . unwrap_or ( 0 ) ;
199
- stderr. truncate ( len) ;
200
-
201
- // consume the rest to avoid blocking
202
- std:: io:: copy ( & mut stderr_src, & mut std:: io:: sink ( ) ) . ok ( ) ;
203
-
204
- let status = process. wait ( ) ?;
205
- Ok ( Output {
206
- status,
207
- stderr,
208
- stdout : vec ! [ ] ,
209
- } )
181
+ process. wait ( )
210
182
}
211
183
}
212
184
@@ -217,7 +189,7 @@ mod windows {
217
189
use std:: os:: raw:: c_int;
218
190
use windows_sys:: Win32 :: UI :: Shell :: ShellExecuteW ;
219
191
220
- use crate :: { IntoResult , Result } ;
192
+ use crate :: IntoResult ;
221
193
222
194
fn convert_path ( path : & OsStr ) -> io:: Result < Vec < u16 > > {
223
195
let mut maybe_result: Vec < _ > = path. encode_wide ( ) . collect ( ) ;
@@ -231,7 +203,7 @@ mod windows {
231
203
Ok ( maybe_result)
232
204
}
233
205
234
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
206
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
235
207
const SW_SHOW : c_int = 5 ;
236
208
237
209
let path = convert_path ( path. as_ref ( ) ) ?;
@@ -249,7 +221,7 @@ mod windows {
249
221
( result as c_int ) . into_result ( )
250
222
}
251
223
252
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
224
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
253
225
const SW_SHOW : c_int = 5 ;
254
226
255
227
let path = convert_path ( path. as_ref ( ) ) ?;
@@ -273,69 +245,69 @@ mod windows {
273
245
274
246
#[ cfg( target_os = "macos" ) ]
275
247
mod macos {
276
- use std:: { ffi:: OsStr , process:: Command } ;
248
+ use std:: { ffi:: OsStr , io , process:: Command } ;
277
249
278
- use crate :: { CommandExt , IntoResult , Result } ;
250
+ use crate :: { CommandExt , IntoResult } ;
279
251
280
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
252
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
281
253
Command :: new ( "/usr/bin/open" )
282
254
. arg ( path. as_ref ( ) )
283
- . output_stderr ( )
255
+ . status_without_output ( )
284
256
. into_result ( )
285
257
}
286
258
287
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
259
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
288
260
Command :: new ( "/usr/bin/open" )
289
261
. arg ( path. as_ref ( ) )
290
262
. arg ( "-a" )
291
263
. arg ( app. into ( ) )
292
- . output_stderr ( )
264
+ . status_without_output ( )
293
265
. into_result ( )
294
266
}
295
267
}
296
268
297
269
#[ cfg( target_os = "ios" ) ]
298
270
mod ios {
299
- use std:: { ffi:: OsStr , process:: Command } ;
271
+ use std:: { ffi:: OsStr , io , process:: Command } ;
300
272
301
- use crate :: { CommandExt , IntoResult , Result } ;
273
+ use crate :: { CommandExt , IntoResult } ;
302
274
303
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
275
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
304
276
Command :: new ( "uiopen" )
305
277
. arg ( "--url" )
306
278
. arg ( path. as_ref ( ) )
307
- . output_stderr ( )
279
+ . status_without_output ( )
308
280
. into_result ( )
309
281
}
310
282
311
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
283
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
312
284
Command :: new ( "uiopen" )
313
285
. arg ( "--url" )
314
286
. arg ( path. as_ref ( ) )
315
287
. arg ( "--bundleid" )
316
288
. arg ( app. into ( ) )
317
- . output_stderr ( )
289
+ . status_without_output ( )
318
290
. into_result ( )
319
291
}
320
292
}
321
293
322
294
#[ cfg( target_os = "haiku" ) ]
323
295
mod haiku {
324
- use std:: { ffi:: OsStr , process:: Command } ;
296
+ use std:: { ffi:: OsStr , io , process:: Command } ;
325
297
326
- use crate :: { CommandExt , IntoResult , Result } ;
298
+ use crate :: { CommandExt , IntoResult } ;
327
299
328
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
300
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
329
301
Command :: new ( "/bin/open" )
330
302
. arg ( path. as_ref ( ) )
331
- . output_stderr ( )
303
+ . status_without_output ( )
332
304
. into_result ( )
333
305
}
334
306
335
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
307
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
336
308
Command :: new ( app. into ( ) )
337
309
. arg ( path. as_ref ( ) )
338
- . output_stderr ( )
310
+ . status_without_output ( )
339
311
. into_result ( )
340
312
}
341
313
}
@@ -354,13 +326,14 @@ mod unix {
354
326
use std:: {
355
327
env,
356
328
ffi:: { OsStr , OsString } ,
329
+ io,
357
330
path:: { Path , PathBuf } ,
358
331
process:: Command ,
359
332
} ;
360
333
361
- use crate :: { CommandExt , IntoResult , Result } ;
334
+ use crate :: { CommandExt , IntoResult } ;
362
335
363
- pub fn that < T : AsRef < OsStr > > ( path : T ) -> Result {
336
+ pub fn that < T : AsRef < OsStr > > ( path : T ) -> io :: Result < ( ) > {
364
337
let path = path. as_ref ( ) ;
365
338
let open_handlers = [
366
339
( "xdg-open" , & [ path] as & [ _ ] ) ,
@@ -374,11 +347,18 @@ mod unix {
374
347
let mut io_error = None ;
375
348
376
349
for ( command, args) in & open_handlers {
377
- let result = Command :: new ( command) . args ( * args) . output_stderr ( ) ;
350
+ let result = Command :: new ( command) . args ( * args) . status_without_output ( ) ;
378
351
379
352
match result {
380
- Ok ( o) if o. status . success ( ) => return Ok ( ( ) ) ,
381
- Ok ( o) => unsuccessful = unsuccessful. or_else ( || Some ( crate :: from_output ( o) ) ) ,
353
+ Ok ( status) if status. success ( ) => return Ok ( ( ) ) ,
354
+ Ok ( status) => {
355
+ unsuccessful = unsuccessful. or_else ( || {
356
+ Some ( std:: io:: Error :: new (
357
+ std:: io:: ErrorKind :: Other ,
358
+ status. to_string ( ) ,
359
+ ) )
360
+ } )
361
+ }
382
362
Err ( err) => io_error = io_error. or ( Some ( err) ) ,
383
363
}
384
364
}
@@ -388,10 +368,10 @@ mod unix {
388
368
. expect ( "successful cases don't get here" ) )
389
369
}
390
370
391
- pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> Result {
371
+ pub fn with < T : AsRef < OsStr > > ( path : T , app : impl Into < String > ) -> io :: Result < ( ) > {
392
372
Command :: new ( app. into ( ) )
393
373
. arg ( path. as_ref ( ) )
394
- . output_stderr ( )
374
+ . status_without_output ( )
395
375
. into_result ( )
396
376
}
397
377
0 commit comments