Skip to content

Commit

Permalink
Clean up the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Oct 13, 2023
1 parent fee86c8 commit 504c2ee
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 202 deletions.
137 changes: 137 additions & 0 deletions bff-cli/src/crc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::io::{self, BufRead, Read};

use bff::crc32::{Asobo32, AsoboAlternate32, Kalisto32};
use bff::crc64::Asobo64;
use bff::traits::NameHashFunction;
use clap::ValueEnum;

use crate::error::BffCliResult;

#[derive(ValueEnum, Clone)]
pub enum CrcAlgorithm {
#[value(alias("a"))]
Asobo,
#[value(alias("alt"))]
AsoboAlternate,
#[value(alias("k"))]
Kalisto,
#[value(alias("a64"))]
Asobo64,
}

#[derive(ValueEnum, Clone)]
pub enum CrcMode {
Bytes,
Lines,
}

#[derive(ValueEnum, Clone)]
pub enum CrcFormat {
Signed,
Unsigned,
#[value(alias("hex"))]
Hexadecimal,
}

fn format_hash(hash: i32, format: &CrcFormat) -> String {
match format {
CrcFormat::Signed => {
format!("{}", hash)
}
CrcFormat::Unsigned => {
format!("{}", hash as u32)
}
CrcFormat::Hexadecimal => {
format!("{:#08x}", hash)
}
}
}

fn format_hash64(hash: i64, format: &CrcFormat) -> String {
match format {
CrcFormat::Signed => {
format!("{}", hash)
}
CrcFormat::Unsigned => {
format!("{}", hash as u64)
}
CrcFormat::Hexadecimal => {
format!("{:#016x}", hash)
}
}
}

fn hash(
bytes: &[u8],
starting: &Option<i64>,
algorithm: &CrcAlgorithm,
format: &CrcFormat,
) -> String {
let starting = *starting;
match starting {
None => match algorithm {
CrcAlgorithm::Asobo => format_hash(Asobo32::hash(bytes), format),
CrcAlgorithm::AsoboAlternate => format_hash(AsoboAlternate32::hash(bytes), format),
CrcAlgorithm::Kalisto => format_hash(Kalisto32::hash(bytes), format),
CrcAlgorithm::Asobo64 => format_hash64(Asobo64::hash(bytes), format),
},
Some(starting) => match algorithm {
CrcAlgorithm::Asobo => {
format_hash(Asobo32::hash_options(bytes, starting as i32), format)
}
CrcAlgorithm::AsoboAlternate => format_hash(
AsoboAlternate32::hash_options(bytes, starting as i32),
format,
),
CrcAlgorithm::Kalisto => {
format_hash(Kalisto32::hash_options(bytes, starting as i32), format)
}
CrcAlgorithm::Asobo64 => format_hash64(Asobo64::hash_options(bytes, starting), format),
},
}
}

pub fn crc(
string: &Option<String>,
starting: &Option<i64>,
algorithm: &CrcAlgorithm,
mode: &CrcMode,
format: &CrcFormat,
) -> BffCliResult<()> {
match (string, mode) {
(Some(string), CrcMode::Bytes) => {
println!(
"{} {:?}",
hash(string.as_bytes(), starting, algorithm, format),
string.as_bytes()
);
}
(Some(string), CrcMode::Lines) => {
for line in string.lines() {
println!(
"{} \"{}\"",
hash(line.as_bytes(), starting, algorithm, format),
line
);
}
}
(None, CrcMode::Bytes) => {
let stdin = io::stdin();
let mut buf: Vec<u8> = Vec::new();
stdin.lock().read_to_end(&mut buf)?;
println!("{} {:?}", hash(&buf, starting, algorithm, format), buf);
}
(None, CrcMode::Lines) => {
let stdin = io::stdin();
for line in stdin.lock().lines() {
let line = line?;
println!(
"{} \"{}\"",
hash(line.as_bytes(), starting, algorithm, format),
line
);
}
}
}
Ok(())
}
87 changes: 0 additions & 87 deletions bff-cli/src/crc32.rs

This file was deleted.

69 changes: 0 additions & 69 deletions bff-cli/src/crc64.rs

This file was deleted.

Loading

0 comments on commit 504c2ee

Please sign in to comment.