mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-06 10:32:10 +00:00
argument: Add a key numeric parser.
Sometimes I want numeric key in addition to value. BUG=b:215297064 TEST=cargo test Change-Id: I75ae37905d1a27709e2fa694ab01e0ac8ed8596c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3556930 Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Junichi Uekawa <uekawa@chromium.org>
This commit is contained in:
parent
29950ef5a6
commit
3cdc5c5d3c
1 changed files with 33 additions and 2 deletions
|
@ -445,14 +445,30 @@ impl<'a> KeyValuePair<'a> {
|
|||
)))
|
||||
}
|
||||
|
||||
fn get_numeric<T>(&self, val: &str) -> Result<T>
|
||||
where
|
||||
T: TryFrom<u64>,
|
||||
<T as TryFrom<u64>>::Error: std::error::Error,
|
||||
{
|
||||
let num = parse_hex_or_decimal(val)?;
|
||||
self.handle_parse_err(T::try_from(num))
|
||||
}
|
||||
|
||||
pub fn parse_numeric<T>(&self) -> Result<T>
|
||||
where
|
||||
T: TryFrom<u64>,
|
||||
<T as TryFrom<u64>>::Error: std::error::Error,
|
||||
{
|
||||
let val = self.value()?;
|
||||
let num = parse_hex_or_decimal(val)?;
|
||||
self.handle_parse_err(T::try_from(num))
|
||||
self.get_numeric(val)
|
||||
}
|
||||
|
||||
pub fn key_numeric<T>(&self) -> Result<T>
|
||||
where
|
||||
T: TryFrom<u64>,
|
||||
<T as TryFrom<u64>>::Error: std::error::Error,
|
||||
{
|
||||
self.get_numeric(self.key())
|
||||
}
|
||||
|
||||
pub fn parse<T>(&self) -> Result<T>
|
||||
|
@ -726,4 +742,19 @@ mod tests {
|
|||
assert!(parse_hex_or_decimal("0xz").is_err());
|
||||
assert!(parse_hex_or_decimal("hello world").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_key_value_options_numeric_key() {
|
||||
let mut opts = parse_key_value_options("test", "0x30,0x100=value,nonnumeric=value", ',');
|
||||
let kv = opts.next().unwrap();
|
||||
assert_eq!(kv.key_numeric::<u32>().unwrap(), 0x30);
|
||||
|
||||
let kv = opts.next().unwrap();
|
||||
assert_eq!(kv.key_numeric::<u32>().unwrap(), 0x100);
|
||||
assert_eq!(kv.value().unwrap(), "value");
|
||||
|
||||
let kv = opts.next().unwrap();
|
||||
assert!(kv.key_numeric::<u32>().is_err());
|
||||
assert_eq!(kv.key(), "nonnumeric");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue