diff --git a/src/format.rs b/src/format.rs new file mode 100644 index 0000000..baaea0b --- /dev/null +++ b/src/format.rs @@ -0,0 +1,42 @@ +pub type Num = u128; + +#[derive(Copy, Clone, Debug)] +pub enum Format { + Dec, + Hex, + Bin, + Octal, +} + +impl Format { + pub fn prefix(&self) -> String { + match self { + Format::Dec => "0d", + Format::Hex => "0x", + Format::Bin => "0b", + Format::Octal => "0o", + } + .to_string() + } + pub fn format(&self, num: Num, prefix: bool) -> String { + let mut buf = String::new(); + if prefix { + buf += &self.prefix(); + } + match self { + Format::Hex => { + buf += &format!("{num:X}"); + } + Format::Bin => { + buf += &format!("{num:b}"); + } + Format::Octal => { + buf += &format!("{num:o}"); + } + Format::Dec => { + buf += &format!("{num}"); + } + } + buf + } +} diff --git a/src/main.rs b/src/main.rs index ff8b59a..1e4f86a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,48 +6,8 @@ use clap::{ArgGroup, Parser}; use clap_num::maybe_hex; -pub type Num = u128; - -#[derive(Copy, Clone, Debug)] -enum Format { - Dec, - Hex, - Bin, - Octal, -} - -impl Format { - fn prefix(&self) -> String { - match self { - Format::Dec => "0d", - Format::Hex => "0x", - Format::Bin => "0b", - Format::Octal => "0o", - } - .to_string() - } - fn format(&self, num: Num, prefix: bool) -> String { - let mut buf = String::new(); - if prefix { - buf += &self.prefix(); - } - match self { - Format::Hex => { - buf += &format!("{num:X}"); - } - Format::Bin => { - buf += &format!("{num:b}"); - } - Format::Octal => { - buf += &format!("{num:o}"); - } - Format::Dec => { - buf += &format!("{num}"); - } - } - buf - } -} +mod format; +use format::*; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] @@ -95,10 +55,6 @@ impl Cli { } fn main() { - let _ = formatter(); -} - -fn formatter() -> anyhow::Result<()> { let cli = Cli::parse(); let mut out: Vec = Vec::new(); @@ -109,6 +65,5 @@ fn formatter() -> anyhow::Result<()> { for o in out { println!("{o}") } - - Ok(()) } +