generated from PlexSheep/rs-base
feat: output raw binary #26
This commit is contained in:
parent
e210b688f8
commit
d315e24034
|
@ -21,20 +21,25 @@
|
||||||
//! assert_eq!(Format::Hex.format(0x1337, &options), vec![48, 120, 49, 51, 51, 55]);
|
//! 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 anyhow::anyhow;
|
||||||
use clap::{ArgGroup, Parser};
|
use clap::{ArgGroup, Parser};
|
||||||
use libpt::bintols::{join, split};
|
use libpt::bintols::{join, split};
|
||||||
use libpt::cli::args::VerbosityLevel;
|
use libpt::cli::args::VerbosityLevel;
|
||||||
use libpt::log::debug;
|
use libpt::log::{debug, trace};
|
||||||
|
|
||||||
/// The number type [numf](crate) uses
|
/// The number type [numf](crate) uses
|
||||||
pub type NumberType = u128;
|
pub type NumberType = u128;
|
||||||
|
|
||||||
/// formats supported by numf
|
/// formats supported by numf
|
||||||
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)]
|
||||||
pub enum Format {
|
pub enum Format {
|
||||||
Dec,
|
Dec,
|
||||||
|
#[default]
|
||||||
Hex,
|
Hex,
|
||||||
Bin,
|
Bin,
|
||||||
Octal,
|
Octal,
|
||||||
|
@ -44,6 +49,12 @@ pub enum Format {
|
||||||
Raw,
|
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
|
/// Describes what the formatter should do
|
||||||
///
|
///
|
||||||
/// Use [Self::default] to get a basic variant or create a object yourself.
|
/// 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.
|
/// For example, `0b1100` will be `0b00001100` with this.
|
||||||
/// This does not apply to all formats, only hexadecimal and binary.
|
/// This does not apply to all formats, only hexadecimal and binary.
|
||||||
padding: bool,
|
padding: bool,
|
||||||
#[arg(short = 'x', long, default_value_t = true)]
|
#[arg(short = 'x', long)]
|
||||||
/// format to hexadecimal
|
/// format to hexadecimal
|
||||||
hex: bool,
|
hex: bool,
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
|
@ -137,6 +148,7 @@ pub struct FormatOptions {
|
||||||
impl FormatOptions {
|
impl FormatOptions {
|
||||||
/// get the format that the user has configured
|
/// get the format that the user has configured
|
||||||
pub fn format(&self) -> Format {
|
pub fn format(&self) -> Format {
|
||||||
|
trace!("self.hex: {}", self.hex);
|
||||||
if self.oct {
|
if self.oct {
|
||||||
Format::Octal
|
Format::Octal
|
||||||
} else if self.bin {
|
} else if self.bin {
|
||||||
|
@ -152,7 +164,9 @@ impl FormatOptions {
|
||||||
} else if self.raw {
|
} else if self.raw {
|
||||||
Format::Raw
|
Format::Raw
|
||||||
} else {
|
} 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,
|
padding: false,
|
||||||
prefix: false,
|
prefix: false,
|
||||||
oct: false,
|
oct: false,
|
||||||
hex: true,
|
hex: false,
|
||||||
bin: false,
|
bin: false,
|
||||||
raw: false,
|
raw: false,
|
||||||
base32: false,
|
base32: false,
|
||||||
|
@ -283,6 +297,7 @@ impl Format {
|
||||||
|
|
||||||
/// format a number with a [Format] and [FormatOptions]
|
/// format a number with a [Format] and [FormatOptions]
|
||||||
pub fn format(&self, num: NumberType, options: &FormatOptions) -> Vec<u8> {
|
pub fn format(&self, num: NumberType, options: &FormatOptions) -> Vec<u8> {
|
||||||
|
debug!("formatting mode: {self}");
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
if options.prefix() {
|
if options.prefix() {
|
||||||
buf.append(&mut self.prefix());
|
buf.append(&mut self.prefix());
|
||||||
|
@ -320,7 +335,11 @@ impl Format {
|
||||||
.as_bytes()
|
.as_bytes()
|
||||||
.to_owned(),
|
.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
|
buf
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ use std::process::exit;
|
||||||
use clap::{CommandFactory, Parser};
|
use clap::{CommandFactory, Parser};
|
||||||
|
|
||||||
mod format;
|
mod format;
|
||||||
|
use crate::format::{numf_parser, Format};
|
||||||
use format::*;
|
use format::*;
|
||||||
use libpt::log::debug;
|
use libpt::log::debug;
|
||||||
use numf::format::numf_parser;
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
// try to read from stdin first, appending the numbers we read to the FormatOptions
|
// 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 {
|
for o in out {
|
||||||
let mut stdout = std::io::stdout();
|
let mut stdout = std::io::stdout();
|
||||||
stdout.write_all(&o)?;
|
stdout.write_all(&o)?;
|
||||||
stdout.write_all(b"\n")?;
|
if options.format() != Format::Raw {
|
||||||
|
stdout.write_all(b"\n")?;
|
||||||
|
}
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Reference in New Issue