Skip to content

Commit

Permalink
Simplify parse_param
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Dec 27, 2024
1 parent aea3efe commit b7aeb8b
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions aya-log-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,27 @@ fn push_literal(frag: &mut Vec<Fragment>, unescaped_literal: &str) -> Result<(),
Ok(())
}

/// Parses the display hint (e.g. the `ipv4` in `{:ipv4}`).
fn parse_display_hint(s: &str) -> Result<DisplayHint, String> {
Ok(match s {
"p" | "x" => DisplayHint::LowerHex,
"X" => DisplayHint::UpperHex,
"i" => DisplayHint::Ip,
"mac" => DisplayHint::LowerMac,
"MAC" => DisplayHint::UpperMac,
_ => return Err(format!("unknown display hint: {s:?}")),
})
}

/// Parse `Param` from the given `&str` which can specify an optional format
/// like `:x` or `:ipv4` (without curly braces, which are parsed by the `parse`
/// function).
fn parse_param(mut input: &str) -> Result<Parameter, String> {
const HINT_PREFIX: &str = ":";

// Then, optional hint
let mut hint = DisplayHint::Default;

if input.starts_with(HINT_PREFIX) {
// skip the prefix
input = &input[HINT_PREFIX.len()..];
if input.is_empty() {
return Err("malformed format string (missing display hint after ':')".into());
fn parse_param(input: &str) -> Result<Parameter, String> {
let hint = match input.strip_prefix(":") {
Some(input) => match input {
"" => return Err("malformed format string (missing display hint after ':')".into()),
"p" | "x" => DisplayHint::LowerHex,
"X" => DisplayHint::UpperHex,
"i" => DisplayHint::Ip,
"mac" => DisplayHint::LowerMac,
"MAC" => DisplayHint::UpperMac,
input => return Err(format!("unknown display hint: {input:?}")),
},
None => {
if !input.is_empty() {
return Err(format!("unexpected content {input:?} in format string"));
}
DisplayHint::Default
}

hint = parse_display_hint(input)?;
} else if !input.is_empty() {
return Err(format!("unexpected content {input:?} in format string"));
}

};
Ok(Parameter { hint })
}

Expand Down

0 comments on commit b7aeb8b

Please sign in to comment.