From 8449b97658b892cc2fe439f246bd2af303e00245 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Thu, 14 Sep 2023 18:39:44 +0300 Subject: [PATCH] 0.3.0 - add read_file_bytes - rename read_file_to_str() -> read_file_str() - change return types of read_file_* to Result - unpub shell::run() - don't overwrite $PATH in shell::run() - remove unnecessary tests Signed-off-by: Ivan Bushchik --- Cargo.toml | 2 +- README.md | 10 ++++++---- src/io.rs | 46 +++++++++++++++++++++------------------------- src/shell.rs | 34 ++++++++-------------------------- 4 files changed, 36 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e16028..a71f50a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "smurf" -version = "0.2.0" +version = "0.3.0" edition = "2021" authors = ["Ivan Bushchik "] description = "SMall Useful Rust Functions" diff --git a/README.md b/README.md index 81c2dc2..00cdc8d 100644 --- a/README.md +++ b/README.md @@ -7,18 +7,20 @@ ### IO - macros input!(T) and input_vec!(T), that could be used to simplify stdin process -- read_file_to_str() basically reads file to string (yes.) +- read_file_str() basically reads file to string (yes.) +- read_file_bytes() see read_file_str() ### Shell -- macros shell!() +- macros shell!() + ## Usage Add this to your `Cargo.toml`: ```toml [dependencies] -smurf = "0.2" +smurf = "0.3" ``` and this to your crate root: @@ -49,4 +51,4 @@ fn main() { ## License -[MIT](LICENSE-MIT) +[MIT](./LICENSE) diff --git a/src/io.rs b/src/io.rs index 01a2c4e..f26ce1e 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,40 +1,36 @@ use std::fs::File; use std::io::{prelude::*, BufReader}; -pub fn read_file_to_str(path: &std::path::PathBuf) -> Option { - let file = File::open(&path); - let file = match file { - Ok(data) => data, - Err(_) => { - eprintln!("Cannot open file \"{}\"", path.display()); - return None - } - }; +pub fn read_file_str(path: &std::path::PathBuf) -> Result { + let file = File::open(&path)?; let mut buf_reader = BufReader::new(file); let mut content = String::new(); - buf_reader.read_to_string(&mut content).unwrap(); - Some(content) + buf_reader.read_to_string(&mut content)?; + Ok(content) } +pub fn read_file_bytes(path: &std::path::PathBuf) -> Result, std::io::Error> { + let file = File::open(&path)?; + let mut buf_reader = BufReader::new(file); + let mut content: Vec = Vec::new(); + buf_reader.read_to_end(&mut content)?; + Ok(content) +} #[macro_export] macro_rules! input { - ($t:ty) => { - { - let mut input = String::new(); - std::io::stdin().read_line(&mut input).unwrap(); - input.trim().parse::<$t>().unwrap() - } - } + ($t:ty) => {{ + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + input.trim().parse::<$t>().unwrap() + }}; } #[macro_export] macro_rules! input_vec { - ($t:ty) => { - { - let mut input = String::new(); - std::io::stdin().read_line(&mut input).unwrap(); - input.trim().split_whitespace().map(|x| x.parse::<$t>().unwrap()).collect::>() - } - } + ($t:ty) => {{ + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + input.trim().split_whitespace().map(|x| x.parse::<$t>().unwrap()).collect::>() + }}; } diff --git a/src/shell.rs b/src/shell.rs index 5b6c647..3a40a47 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -12,8 +12,9 @@ pub struct ShellResult { pub stdout: String, pub stderr: String, } - -pub fn run(command: String) -> ShellResult { + +#[allow(dead_code)] +fn run(command: String) -> ShellResult { let mut iter = command.split_whitespace(); let mut current = iter.next(); let mut words: Vec<&str> = vec![]; @@ -23,7 +24,6 @@ pub fn run(command: String) -> ShellResult { } let command = Command::new(&words[0]) .args(&words[1..]) - .env("PATH", "/bin:/usr/bin") // TODO: Make this configurable .output() .expect(&format!("Failed to execute '{}'", command)); return ShellResult { @@ -35,6 +35,9 @@ pub fn run(command: String) -> ShellResult { #[cfg(test)] mod tests { + // Testing basic usage and only on unix + + #[cfg(unix)] #[test] fn test_shell_macro() { let result = shell!("echo hello"); @@ -43,17 +46,7 @@ mod tests { assert_eq!(result.stderr, ""); } - /* You literally can't use pipe using std::process::Command */ - // TODO: figure out how to test this - #[ignore] - #[test] - fn test_shell_macro_with_stderr() { - let result = shell!("echo hello 1>&2"); - assert_eq!(result.code, 0); - assert_eq!(result.stdout, ""); - assert_eq!(result.stderr, "hello\n"); - } - + #[cfg(unix)] #[test] fn test_shell_macro_with_args() { let result = shell!("echo {} {}", "hello", "world"); @@ -61,15 +54,4 @@ mod tests { assert_eq!(result.stdout, "hello world\n"); assert_eq!(result.stderr, ""); } - - // TODO: figure out how to test this - #[ignore] - #[test] - fn test_shell_macro_with_code() { - let result = shell!("sh -c exit 1"); - assert_eq!(result.code, 1); - assert_eq!(result.stdout, ""); - assert_eq!(result.stderr, ""); - } - -} \ No newline at end of file +}