From 3fd52c19dd614268b7b8a64e25fcd5ca13ad1544 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 01:02:55 +0200 Subject: [PATCH] refactor: move Format enum to a new module --- src/format.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 51 +++------------------------------------------------ 2 files changed, 45 insertions(+), 48 deletions(-) create mode 100644 src/format.rs 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(()) } +