xmpp-parsers: Simplify with String::split_once() instead of splitn(2)

This directly returns a 2-uple, instead of a Vec which has to be matched
again as if we didn’t know it would return up to two elements.
This commit is contained in:
Link Mauve 2025-11-22 22:40:53 +01:00
commit 9b77b968a9
2 changed files with 16 additions and 31 deletions

View file

@ -26,26 +26,15 @@ impl FromStr for ContentId {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
let temp: Vec<_> = s.splitn(2, '@').collect();
let temp: Vec<_> = match temp[..] {
[lhs, rhs] => {
if rhs != "bob.xmpp.org" {
return Err(Error::Other("Wrong domain for cid URI."));
}
lhs.splitn(2, '+').collect()
}
_ => return Err(Error::Other("Missing @ in cid URI.")),
};
let (algo, hex) = match temp[..] {
[lhs, rhs] => {
let algo = match lhs {
"sha1" => Algo::Sha_1,
"sha256" => Algo::Sha_256,
_ => unimplemented!(),
};
(algo, rhs)
}
_ => return Err(Error::Other("Missing + in cid URI.")),
let (lhs, rhs) = s.split_once('@').ok_or(Error::Other("Missing @ in cid URI."))?;
if rhs != "bob.xmpp.org" {
return Err(Error::Other("Wrong domain for cid URI."));
}
let (algo, hex) = lhs.split_once('+').ok_or(Error::Other("Missing + in cid URI."))?;
let algo = match algo {
"sha1" => Algo::Sha_1,
"sha256" => Algo::Sha_256,
_ => unimplemented!(),
};
let hash = Hash::from_hex(algo, hex).map_err(Error::text_parse_error)?;
Ok(ContentId { hash })

View file

@ -169,9 +169,7 @@ impl TryFrom<Element> for Body {
Ok(Body {
style: parse_css(elem.attr("style")),
xml_lang: elem
.attr_ns("xml", "lang")
.map(ToString::to_string),
xml_lang: elem.attr_ns("xml", "lang").map(ToString::to_string),
children,
})
}
@ -502,14 +500,12 @@ fn parse_css(style: Option<&str>) -> Css {
let mut properties = vec![];
if let Some(style) = style {
// TODO: make that parser a bit more resilient to things.
for part in style.split(';') {
let mut part = part
.splitn(2, ':')
.map(ToString::to_string)
.collect::<Vec<_>>();
let key = part.pop().unwrap();
let value = part.pop().unwrap();
properties.push(Property { key, value });
for declaration in style.split(';') {
let (key, value) = declaration.split_once(':').unwrap();
properties.push(Property {
key: key.to_string(),
value: value.to_string(),
});
}
}
properties