Implemented basic "cat" functionality
Added newline to rbecho.rs Shortened TryFrom<Result<DirEntry, io::Error>> for FileInfo
This commit is contained in:
parent
0f0bee6802
commit
f780929dbf
3 changed files with 29 additions and 6 deletions
27
src/bin/rbcat.rs
Normal file
27
src/bin/rbcat.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use std::{env, fs};
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
if env::args().len() < 2 {
|
||||
let mut stdin = io::stdin().lock();
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
stdin.read_line(&mut line)?;
|
||||
print!("{}", line);
|
||||
}
|
||||
}
|
||||
for file_name in env::args().skip(1) {
|
||||
match fs::read_to_string(&file_name) {
|
||||
Ok(contents) => print!("{}", contents),
|
||||
Err(error) => {
|
||||
let msg = match error.kind() {
|
||||
io::ErrorKind::PermissionDenied => "Permission denied",
|
||||
io::ErrorKind::NotFound => "No such file or directory",
|
||||
_ => "Unexpected I/O error"
|
||||
};
|
||||
eprintln!("rbcat: {}: {}", file_name, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
println!("{}", args.join(" "));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -80,11 +80,7 @@ impl TryFrom<DirEntry> for FileInfo {
|
|||
impl TryFrom<Result<DirEntry, io::Error>> for FileInfo {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(de: Result<DirEntry, io::Error>) -> Result<Self, Self::Error> {
|
||||
let de = de?;
|
||||
let name = de.file_name().into_string().unwrap();
|
||||
Ok(Self { name, metadata: de.metadata()? })
|
||||
}
|
||||
fn try_from(de: Result<DirEntry, io::Error>) -> Result<Self, Self::Error> { Self::try_from(de?) }
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
|
|
Loading…
Reference in a new issue