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

View file

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