From 1bd25373cd626473a56bd9a049755bda22c05479 Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Thu, 27 Mar 2025 20:15:40 +0200 Subject: [PATCH] feat: add the base32 command --- coreutils/src/commands/base32.rs | 39 ++++++++++++++++++++++++++++++++ coreutils/src/commands/mod.rs | 1 + src/registry.rs | 1 + utils/src/encoding/base16.rs | 1 + utils/src/encoding/base32.rs | 1 + utils/src/encoding/base64.rs | 1 + 6 files changed, 44 insertions(+) create mode 100644 coreutils/src/commands/base32.rs diff --git a/coreutils/src/commands/base32.rs b/coreutils/src/commands/base32.rs new file mode 100644 index 0000000..7764ac4 --- /dev/null +++ b/coreutils/src/commands/base32.rs @@ -0,0 +1,39 @@ +use boxutils::args::ArgParser; +use boxutils::commands::Command; +use boxutils::encoding::base32; +use std::fs::OpenOptions; +use std::io::Read; + +// TODO: Add the -w flag +// we dont have a way to do text +// warping in boxutils yet haha + +pub struct Base32; + +impl Command for Base32 { + fn execute(&self) { + let args = ArgParser::builder() + .add_flag("-d") // decode flag + .parse_args("base32"); + + let decode = args.get_flag("-d"); + + // FIXME: This is jank! + let mut file: Box = match &args.get_normal_args()[..] { + [] => Box::new(std::io::stdin()), + [file] => Box::new(OpenOptions::new().read(true).open(file).unwrap()), + _ => panic!("base32: multiple files provided"), + }; + + let mut buffer = String::new(); + while boxutils::input::repl_with_file(&mut file, &mut buffer) { + let data = if decode { + base32::decode(buffer.clone()) + } else { + base32::encode(buffer.clone()) + }; + + println!("{}", data); + } + } +} diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index 44e70eb..81cad30 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -21,3 +21,4 @@ command!(whoami::WhoAmI); command!(hostname::Hostname); command!(tee::Tee); command!(base64::Base64); +command!(base32::Base32); diff --git a/src/registry.rs b/src/registry.rs index 50e51c8..ea7b363 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -31,6 +31,7 @@ pub fn get_registry() -> CommandRegistry { "hostname" => coreutils::commands::Hostname, "tee" => coreutils::commands::Tee, "base64" => coreutils::commands::Base64, + "base32" => coreutils::commands::Base32, "box" => Boxcmd }); diff --git a/utils/src/encoding/base16.rs b/utils/src/encoding/base16.rs index 25e259e..8f5f463 100644 --- a/utils/src/encoding/base16.rs +++ b/utils/src/encoding/base16.rs @@ -25,6 +25,7 @@ pub fn decode(to_decode: String) -> String { } mod tests { + #[allow(unused_imports)] use super::*; #[test] diff --git a/utils/src/encoding/base32.rs b/utils/src/encoding/base32.rs index 7dcdf24..087a0e6 100644 --- a/utils/src/encoding/base32.rs +++ b/utils/src/encoding/base32.rs @@ -48,6 +48,7 @@ pub fn decode(to_decode: String) -> String { } mod tests { + #[allow(unused_imports)] use super::*; #[test] diff --git a/utils/src/encoding/base64.rs b/utils/src/encoding/base64.rs index d18d8bb..da54d0a 100644 --- a/utils/src/encoding/base64.rs +++ b/utils/src/encoding/base64.rs @@ -64,6 +64,7 @@ pub fn decode(to_decode: String) -> String { } mod tests { + #[allow(unused_imports)] use super::*; #[test]