feat: output raw binary #26

This commit is contained in:
Christoph J. Scherr 2024-09-06 11:58:40 +02:00
parent e210b688f8
commit d315e24034
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
2 changed files with 30 additions and 9 deletions

View File

@ -21,20 +21,25 @@
//! assert_eq!(Format::Hex.format(0x1337, &options), vec![48, 120, 49, 51, 51, 55]);
//! ```
#![allow(dead_code)] // this is exported to lib.rs
#![allow(dead_code)]
use std::default;
use std::fmt::Display;
// this is exported to lib.rs
use anyhow::anyhow;
use clap::{ArgGroup, Parser};
use libpt::bintols::{join, split};
use libpt::cli::args::VerbosityLevel;
use libpt::log::debug;
use libpt::log::{debug, trace};
/// The number type [numf](crate) uses
pub type NumberType = u128;
/// formats supported by numf
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)]
pub enum Format {
Dec,
#[default]
Hex,
Bin,
Octal,
@ -44,6 +49,12 @@ pub enum Format {
Raw,
}
impl Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
/// Describes what the formatter should do
///
/// Use [Self::default] to get a basic variant or create a object yourself.
@ -78,7 +89,7 @@ pub struct FormatOptions {
/// For example, `0b1100` will be `0b00001100` with this.
/// This does not apply to all formats, only hexadecimal and binary.
padding: bool,
#[arg(short = 'x', long, default_value_t = true)]
#[arg(short = 'x', long)]
/// format to hexadecimal
hex: bool,
#[arg(short, long)]
@ -137,6 +148,7 @@ pub struct FormatOptions {
impl FormatOptions {
/// get the format that the user has configured
pub fn format(&self) -> Format {
trace!("self.hex: {}", self.hex);
if self.oct {
Format::Octal
} else if self.bin {
@ -152,7 +164,9 @@ impl FormatOptions {
} else if self.raw {
Format::Raw
} else {
unreachable!()
// none was explicitly selected
debug!("no mode was explicitly selected, going with the default");
Format::default()
}
}
@ -238,7 +252,7 @@ impl Default for FormatOptions {
padding: false,
prefix: false,
oct: false,
hex: true,
hex: false,
bin: false,
raw: false,
base32: false,
@ -283,6 +297,7 @@ impl Format {
/// format a number with a [Format] and [FormatOptions]
pub fn format(&self, num: NumberType, options: &FormatOptions) -> Vec<u8> {
debug!("formatting mode: {self}");
let mut buf: Vec<u8> = Vec::new();
if options.prefix() {
buf.append(&mut self.prefix());
@ -320,7 +335,11 @@ impl Format {
.as_bytes()
.to_owned(),
),
Format::Raw => buf.append(&mut split::unsigned_to_vec(num)),
// Format::Raw => buf.append(&mut split::unsigned_to_vec(num)),
Format::Raw => {
debug!("do the raw thing");
buf.append(&mut split::unsigned_to_vec(num))
}
}
buf
}

View File

@ -4,9 +4,9 @@ use std::process::exit;
use clap::{CommandFactory, Parser};
mod format;
use crate::format::{numf_parser, Format};
use format::*;
use libpt::log::debug;
use numf::format::numf_parser;
fn main() -> anyhow::Result<()> {
// try to read from stdin first, appending the numbers we read to the FormatOptions
@ -79,7 +79,9 @@ fn main() -> anyhow::Result<()> {
for o in out {
let mut stdout = std::io::stdout();
stdout.write_all(&o)?;
if options.format() != Format::Raw {
stdout.write_all(b"\n")?;
}
stdout.flush()?;
}
Ok(())