From 88264697cad07520b0c3da0fa165e63ea2cbabcf Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Fri, 17 Mar 2023 15:23:23 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ .rustfmt.toml | 10 ++++++++++ Cargo.toml | 12 ++++++++++++ LICENSE | 20 ++++++++++++++++++++ README.md | 13 +++++++++++++ src/file.rs | 18 ++++++++++++++++++ src/lib.rs | 2 ++ src/shell.rs | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 .rustfmt.toml create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/file.rs create mode 100644 src/lib.rs create mode 100644 src/shell.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..e8949b3 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,10 @@ +edition = "2021" +hard_tabs = true +merge_derives = true +reorder_imports = true +reorder_modules = true +use_field_init_shorthand = true +use_small_heuristics = "Off" +wrap_comments = true +comment_width = 80 +indent_style = "Block" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..433a5f2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "smurf" +version = "0.1.0" +edition = "2021" +authors = ["Ivan Bushchik "] +description = "SMall Useful Rust Functions" +readme = "README.md" +repository = "https://github.com/ivabus/smurf" +license = "MIT" + + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e5e9014 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +MIT License + +Copyright (c) 2023 Ivan Bushchik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d735809 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# SmURF + +> SMall Useful Rust Functions + +## Components + +### Shell + +- macro `shell!` + +### File + +- function `read_to_str` \ No newline at end of file diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..3aa6c18 --- /dev/null +++ b/src/file.rs @@ -0,0 +1,18 @@ +use log::error; +use std::fs::File; +use std::io::{prelude::*, BufReader}; + +pub fn read_to_str(path: &std::path::PathBuf) -> String { + let file = File::open(&path); + let file = match file { + Ok(data) => data, + Err(err) => { + error!("Cannot open file \"{}\"", path.display()); + std::process::exit(2) + } + }; + let mut buf_reader = BufReader::new(file); + let mut content = String::new(); + buf_reader.read_to_string(&mut content).unwrap(); + content +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..aad7062 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +mod file; +mod shell; diff --git a/src/shell.rs b/src/shell.rs new file mode 100644 index 0000000..eec40ad --- /dev/null +++ b/src/shell.rs @@ -0,0 +1,34 @@ +use std::process::Command; + +#[macro_export] +macro_rules! shell { + ($($arg:tt)*) => {{ + $crate::shell::run(format!($($arg)*)) + }}; +} + +pub struct ShellResult { + pub code: i32, + pub stdout: String, + pub stderr: String, +} + +#[allow(dead_code)] +pub fn run(command: String) -> ShellResult { + let mut iter = command.split_whitespace(); + let mut current = iter.next(); + let mut words: Vec<&str> = vec![]; + while current != None { + words.push(current.unwrap()); + current = iter.next(); + } + let command = Command::new(&words[0]) + .args(&words[1..]) + .output() + .expect(&format!("Failed to execute '{}'", command)); + return ShellResult { + code: command.status.code().unwrap(), + stdout: String::from_utf8(command.stdout).unwrap(), + stderr: String::from_utf8(command.stderr).unwrap(), + }; +}