netdoc: teach MaybeItem about parse_args_as_str().

This simplifies a couple of places in routerdesc parsing.
This commit is contained in:
Nick Mathewson 2020-05-15 13:44:53 -04:00
parent ebfeefeba8
commit 0427abcee2
2 changed files with 13 additions and 13 deletions

View File

@ -75,7 +75,7 @@ pub struct RouterDesc {
is_extrainfo_cache: bool,
// TODO: these families can get bulky. Perhaps we should de-duplicate
// them in a cache, like Tor does.
family: RelayFamily,
family: Option<RelayFamily>,
platform: Option<RelayPlatform>,
// TODO: these polices can get bulky too. Perhaps we should
// de-duplicate them too.
@ -497,13 +497,7 @@ impl RouterDesc {
}
// Family
let family = {
if let Some(fam_tok) = body.get(FAMILY) {
fam_tok.args_as_str().parse::<RelayFamily>()?
} else {
RelayFamily(Vec::new())
}
};
let family = body.maybe(FAMILY).parse_args_as_str::<RelayFamily>()?;
// or-address
// Extract at most one ipv6 address from the list. It's not great,
@ -518,11 +512,7 @@ impl RouterDesc {
}
// platform
let platform = if let Some(p_tok) = body.get(PLATFORM) {
Some(p_tok.args_as_str().parse::<RelayPlatform>()?)
} else {
None
};
let platform = body.maybe(PLATFORM).parse_args_as_str::<RelayPlatform>()?;
// ipv4_policy
let ipv4_policy = {

View File

@ -411,6 +411,16 @@ impl<'a, 'b> MaybeItem<'a, 'b> {
self.0.map(|item| item.args_as_str())
}
#[allow(dead_code)]
pub fn parse_args_as_str<V>(&self) -> Result<Option<V>>
where
V: FromStr<Err = Error>,
{
match self.0 {
Some(item) => Ok(Some(item.args_as_str().parse::<V>()?)),
None => Ok(None),
}
}
#[allow(dead_code)]
pub fn get_obj(&self, want_tag: &str) -> Result<Option<Vec<u8>>> {
match self.0 {
Some(item) => Ok(Some(item.get_obj(want_tag)?)),