emma
/
breed
Archived
forked from mars/breed
1
0
Fork 0

Import Path in main.rs

This commit is contained in:
mars 2023-04-12 17:16:07 -04:00
parent 6f81bab277
commit 5bf1212b2a
1 changed files with 19 additions and 27 deletions

View File

@ -20,15 +20,10 @@
use std::{ use std::{
env::args, env::args,
ffi::OsString, ffi::OsString,
fs::{ File, OpenOptions }, fs::{File, OpenOptions},
io::{ io::{stdout, Read, Stdout, Write},
Read,
stdout,
Stdout,
Write,
},
os::fd::FromRawFd, os::fd::FromRawFd,
path::PathBuf, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
@ -358,22 +353,17 @@ impl State {
handle = match command_parts.get(1) { handle = match command_parts.get(1) {
Some(part) => OsString::from(part), Some(part) => OsString::from(part),
None => { None => {
return Err( return Err(format!("{}: No file name.", command));
format!("{}: No file name.", command) }
);
},
}; };
} }
self.write_buffer(handle).map_err(|err| { self.write_buffer(handle)
format!("{}", err) .map_err(|err| format!("{}", err))?;
})?; }
},
command => { command => {
return Err( return Err(format!("{}: Unrecognized command.", command));
format!("{}: Unrecognized command.", command) }
);
},
} }
} }
Ok(()) Ok(())
@ -420,13 +410,13 @@ fn main() -> Result<()> {
let stdin = 0; // get stdin as a file descriptor let stdin = 0; // get stdin as a file descriptor
if unsafe { libc::isatty(stdin) } == 0 { if unsafe { libc::isatty(stdin) } == 0 {
unsafe { File::from_raw_fd(stdin) } unsafe { File::from_raw_fd(stdin) }
} else { File::open("/dev/null").unwrap() } } else {
}, File::open("/dev/null").unwrap()
}
}
Some(path) => { Some(path) => {
let input_file = Path::new(path); let input_file = Path::new(path);
file_name = Some(OsString::from( file_name = Some(OsString::from(input_file.clone().display().to_string()));
input_file.clone().display().to_string()
));
File::open(input_file).unwrap_or_else(|_| { File::open(input_file).unwrap_or_else(|_| {
let mut err = String::new(); let mut err = String::new();
@ -438,8 +428,10 @@ fn main() -> Result<()> {
eprintln!("{}: {}: {}", argv[0], path, err); eprintln!("{}: {}: {}", argv[0], path, err);
exit(EX_UNAVAILABLE); exit(EX_UNAVAILABLE);
}) })
}, }
}.read_to_end(&mut buf).unwrap(); }
.read_to_end(&mut buf)
.unwrap();
let text = String::from_utf8(buf).unwrap_or_else(|_| { let text = String::from_utf8(buf).unwrap_or_else(|_| {
eprintln!( eprintln!(