byte go write
init
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 12 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | src/main.rs | 54 |
5 files changed, 109 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..077de30 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "wrat" +version = "0.1.0" +edition = "2021" +description = "write bytes to a file" +license = "MIT" +keywords = ["fs", "cli"] +categories = ["development-tools"] +repository = "https://github.com/bend-n/wrat" + +[dependencies] +comat = "0.1.3" @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 bendn + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1ec3320 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# wrat + +fs util for writing bytes to a file + +## usage + +```bash +wrat [0, 0, 0, 255, 126, 126, 255, 255] outfile +``` + +### get + +```bash +cargo install wrat +``` + +### see also + +[erad](https://github.com/bend-n/erad) +[hew](https://github.com/bend-n/hew) diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..88f7dcd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,54 @@ +use comat::comat; +use std::{io::ErrorKind, process::ExitCode}; + +macro_rules! fail { + () => { + fail!("[$($x:u8),+ $(,)?] <outfile>") + }; + ($usage:literal) => {{ + eprintln!(concat!("usage: wrat ", comat!($usage))); + return ExitCode::FAILURE; + }}; +} +fn main() -> ExitCode { + let mut args = std::env::args().skip(1); + let first = args.next(); + let Some(Some(first)) = first.map(|x| x.strip_prefix('[').map(ToOwned::to_owned)) else { + fail!() + }; + let Ok(n) = first.trim_end_matches(',').parse::<u8>() else { + fail!("[{red}u8{reset}]"); + }; + + let mut dat = Vec::with_capacity(64); + dat.push(n); + loop { + let Some(x) = args.next() else { + fail!("[{red}u8{reset}]") + }; + let Ok(n) = x.trim_end_matches(',').trim_end_matches(']').parse::<u8>() else { + fail!("[{red}u8{reset}]"); + }; + dat.push(n); + match x.strip_suffix(']') { + Some(_) => break, + None => continue, + } + } + let Some(out) = args.next() else { + fail!("[..] <{red}filename{reset}>") + }; + match std::fs::write(out, dat).map_err(|e| e.kind()) { + Ok(()) => {} + Err(ErrorKind::PermissionDenied) => { + fail!("[..] <{red}accessible filename{reset}>") + } + Err(ErrorKind::NotFound) => { + fail!("[..] <{red}valid filename{reset}>") + } + Err(_) => { + fail!("[..] <{red}writable file{reset}>") + } + } + ExitCode::SUCCESS +} |