Implemented basic "cat" functionality

Added newline to rbecho.rs
Shortened TryFrom<Result<DirEntry, io::Error>> for FileInfo
This commit is contained in:
Egor 2024-04-25 02:59:54 +03:00
parent 0f0bee6802
commit f780929dbf
3 changed files with 29 additions and 6 deletions

27
src/bin/rbcat.rs Normal file
View 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(());
}

View file

@ -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)]