parse_size,dd: turn instance fns to associated fns

This commit is contained in:
Daniel Hofstetter 2023-08-26 14:35:35 +02:00
parent f0602b0ce6
commit 2e2387d434
2 changed files with 16 additions and 17 deletions

View file

@ -245,29 +245,29 @@ impl Parser {
None => return Err(ParseError::UnrecognizedOperand(operand.to_string())), None => return Err(ParseError::UnrecognizedOperand(operand.to_string())),
Some((k, v)) => match k { Some((k, v)) => match k {
"bs" => { "bs" => {
let bs = self.parse_bytes(k, v)?; let bs = Self::parse_bytes(k, v)?;
self.ibs = bs; self.ibs = bs;
self.obs = bs; self.obs = bs;
} }
"cbs" => self.cbs = Some(self.parse_bytes(k, v)?), "cbs" => self.cbs = Some(Self::parse_bytes(k, v)?),
"conv" => self.parse_conv_flags(v)?, "conv" => self.parse_conv_flags(v)?,
"count" => self.count = Some(self.parse_n(v)?), "count" => self.count = Some(Self::parse_n(v)?),
"ibs" => self.ibs = self.parse_bytes(k, v)?, "ibs" => self.ibs = Self::parse_bytes(k, v)?,
"if" => self.infile = Some(v.to_string()), "if" => self.infile = Some(v.to_string()),
"iflag" => self.parse_input_flags(v)?, "iflag" => self.parse_input_flags(v)?,
"obs" => self.obs = self.parse_bytes(k, v)?, "obs" => self.obs = Self::parse_bytes(k, v)?,
"of" => self.outfile = Some(v.to_string()), "of" => self.outfile = Some(v.to_string()),
"oflag" => self.parse_output_flags(v)?, "oflag" => self.parse_output_flags(v)?,
"seek" | "oseek" => self.seek = self.parse_n(v)?, "seek" | "oseek" => self.seek = Self::parse_n(v)?,
"skip" | "iseek" => self.skip = self.parse_n(v)?, "skip" | "iseek" => self.skip = Self::parse_n(v)?,
"status" => self.status = Some(self.parse_status_level(v)?), "status" => self.status = Some(Self::parse_status_level(v)?),
_ => return Err(ParseError::UnrecognizedOperand(operand.to_string())), _ => return Err(ParseError::UnrecognizedOperand(operand.to_string())),
}, },
} }
Ok(()) Ok(())
} }
fn parse_n(&self, val: &str) -> Result<Num, ParseError> { fn parse_n(val: &str) -> Result<Num, ParseError> {
let n = parse_bytes_with_opt_multiplier(val)?; let n = parse_bytes_with_opt_multiplier(val)?;
Ok(if val.ends_with('B') { Ok(if val.ends_with('B') {
Num::Bytes(n) Num::Bytes(n)
@ -276,13 +276,13 @@ impl Parser {
}) })
} }
fn parse_bytes(&self, arg: &str, val: &str) -> Result<usize, ParseError> { fn parse_bytes(arg: &str, val: &str) -> Result<usize, ParseError> {
parse_bytes_with_opt_multiplier(val)? parse_bytes_with_opt_multiplier(val)?
.try_into() .try_into()
.map_err(|_| ParseError::BsOutOfRange(arg.to_string())) .map_err(|_| ParseError::BsOutOfRange(arg.to_string()))
} }
fn parse_status_level(&self, val: &str) -> Result<StatusLevel, ParseError> { fn parse_status_level(val: &str) -> Result<StatusLevel, ParseError> {
match val { match val {
"none" => Ok(StatusLevel::None), "none" => Ok(StatusLevel::None),
"noxfer" => Ok(StatusLevel::Noxfer), "noxfer" => Ok(StatusLevel::Noxfer),

View file

@ -75,7 +75,7 @@ impl<'parser> Parser<'parser> {
return Err(ParseSizeError::parse_failure(size)); return Err(ParseSizeError::parse_failure(size));
} }
let number_system: NumberSystem = self.determine_number_system(size); let number_system = Self::determine_number_system(size);
// Split the size argument into numeric and unit parts // Split the size argument into numeric and unit parts
// For example, if the argument is "123K", the numeric part is "123", and // For example, if the argument is "123K", the numeric part is "123", and
@ -156,16 +156,16 @@ impl<'parser> Parser<'parser> {
if numeric_string.is_empty() { if numeric_string.is_empty() {
1 1
} else { } else {
self.parse_number(&numeric_string, 10, size)? Self::parse_number(&numeric_string, 10, size)?
} }
} }
NumberSystem::Octal => { NumberSystem::Octal => {
let trimmed_string = numeric_string.trim_start_matches('0'); let trimmed_string = numeric_string.trim_start_matches('0');
self.parse_number(trimmed_string, 8, size)? Self::parse_number(trimmed_string, 8, size)?
} }
NumberSystem::Hexadecimal => { NumberSystem::Hexadecimal => {
let trimmed_string = numeric_string.trim_start_matches("0x"); let trimmed_string = numeric_string.trim_start_matches("0x");
self.parse_number(trimmed_string, 16, size)? Self::parse_number(trimmed_string, 16, size)?
} }
}; };
@ -174,7 +174,7 @@ impl<'parser> Parser<'parser> {
.ok_or_else(|| ParseSizeError::size_too_big(size)) .ok_or_else(|| ParseSizeError::size_too_big(size))
} }
fn determine_number_system(&self, size: &str) -> NumberSystem { fn determine_number_system(size: &str) -> NumberSystem {
if size.len() <= 1 { if size.len() <= 1 {
return NumberSystem::Decimal; return NumberSystem::Decimal;
} }
@ -197,7 +197,6 @@ impl<'parser> Parser<'parser> {
} }
fn parse_number( fn parse_number(
&self,
numeric_string: &str, numeric_string: &str,
radix: u32, radix: u32,
original_size: &str, original_size: &str,