Rewrite to_as_string() using method chain instead of if expressions

This commit is contained in:
Kitaiti Makoto 2022-02-13 01:05:25 +09:00
parent 249fbbe891
commit a6a21d5dfa
1 changed files with 7 additions and 8 deletions

View File

@ -414,13 +414,12 @@ pub trait ToAsString {
impl ToAsString for OneOrMany<&AnyString> { impl ToAsString for OneOrMany<&AnyString> {
fn to_as_string(&self) -> Option<String> { fn to_as_string(&self) -> Option<String> {
if let Some(prop) = self.as_one() { self.as_one()
prop.as_as_str() .and_then(|prop| prop.as_as_str())
} else if let Some(props) = self.as_many() { .or_else(|| {
props.iter().next().and_then(|prop| prop.as_as_str()) self.as_many()
} else { .and_then(|props| props.iter().next().and_then(|prop| prop.as_as_str()))
None })
}
.map(|s| s.to_string()) .map(|s| s.to_string())
} }
} }