refactor: move Format enum to a new module
cargo devel CI / cargo CI (push) Successful in 1m22s Details

This commit is contained in:
Christoph J. Scherr 2024-05-12 01:02:55 +02:00
parent 8f49160f4f
commit 3fd52c19dd
2 changed files with 45 additions and 48 deletions

42
src/format.rs Normal file
View File

@ -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
}
}

View File

@ -6,48 +6,8 @@
use clap::{ArgGroup, Parser}; use clap::{ArgGroup, Parser};
use clap_num::maybe_hex; use clap_num::maybe_hex;
pub type Num = u128; mod format;
use format::*;
#[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
}
}
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -95,10 +55,6 @@ impl Cli {
} }
fn main() { fn main() {
let _ = formatter();
}
fn formatter() -> anyhow::Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
let mut out: Vec<String> = Vec::new(); let mut out: Vec<String> = Vec::new();
@ -109,6 +65,5 @@ fn formatter() -> anyhow::Result<()> {
for o in out { for o in out {
println!("{o}") println!("{o}")
} }
Ok(())
} }