Simplify `parse_param`

pull/1063/merge
Tamir Duberstein 3 weeks ago
parent 4f0559f2af
commit 1bf6a38619

@ -55,39 +55,27 @@ fn push_literal(frag: &mut Vec<Fragment>, unescaped_literal: &str) -> Result<(),
Ok(()) Ok(())
} }
/// Parses the display hint (e.g. the `ipv4` in `{:ipv4}`). /// Parse `Param` from the given `&str` which can specify an optional format
fn parse_display_hint(s: &str) -> Result<DisplayHint, String> { /// like `:x` or `:ipv4` (without curly braces, which are parsed by the `parse`
Ok(match s { /// function).
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, "p" | "x" => DisplayHint::LowerHex,
"X" => DisplayHint::UpperHex, "X" => DisplayHint::UpperHex,
"i" => DisplayHint::Ip, "i" => DisplayHint::Ip,
"mac" => DisplayHint::LowerMac, "mac" => DisplayHint::LowerMac,
"MAC" => DisplayHint::UpperMac, "MAC" => DisplayHint::UpperMac,
_ => return Err(format!("unknown display hint: {s:?}")), input => return Err(format!("unknown display hint: {input:?}")),
}) },
} None => {
if !input.is_empty() {
/// 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());
}
hint = parse_display_hint(input)?;
} else if !input.is_empty() {
return Err(format!("unexpected content {input:?} in format string")); return Err(format!("unexpected content {input:?} in format string"));
} }
DisplayHint::Default
}
};
Ok(Parameter { hint }) Ok(Parameter { hint })
} }

Loading…
Cancel
Save