From b4b0a7dbba287df50ae8c7ea781adffe1fcf4dba Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 15 Jul 2024 18:27:35 +0200 Subject: [PATCH] jid: Simplify from_parts() a bit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By defining the variables in the parent scope, we can avoid one level of indentation for the tuple, which makes things more readable. Additionally, we don’t need to call .to_str() on the passed objects, they automatically Deref to &str for the format!() call. --- jid/src/lib.rs | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/jid/src/lib.rs b/jid/src/lib.rs index 770044cf09a8a0054d4aafa09496a79556e773e2..d9927bd0575f4675b0577322932dbef0119dc490 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -687,25 +687,18 @@ impl FullJid { domain: &DomainRef, resource: &ResourceRef, ) -> FullJid { - let (at, slash, normalized) = if let Some(node) = node { + let (at, slash, normalized); + + if let Some(node) = node { // Parts are never empty so len > 0 for NonZeroU16::new is always Some - ( - NonZeroU16::new(node.len() as u16), - NonZeroU16::new((node.len() + 1 + domain.len()) as u16), - format!( - "{}@{}/{}", - node.as_str(), - domain.as_str(), - resource.as_str() - ), - ) + at = NonZeroU16::new(node.len() as u16); + slash = NonZeroU16::new((node.len() + 1 + domain.len()) as u16); + normalized = format!("{node}@{domain}/{resource}"); } else { - ( - None, - NonZeroU16::new(domain.len() as u16), - format!("{}/{}", domain.as_str(), resource.as_str()), - ) - }; + at = None; + slash = NonZeroU16::new(domain.len() as u16); + normalized = format!("{domain}/{resource}"); + } let inner = Jid { normalized, @@ -778,15 +771,16 @@ impl BareJid { /// allocation if `node` is known to be `None` and `domain` is owned, you /// can use `domain.into()`. pub fn from_parts(node: Option<&NodeRef>, domain: &DomainRef) -> Self { - let (at, normalized) = if let Some(node) = node { + let (at, normalized); + + if let Some(node) = node { // Parts are never empty so len > 0 for NonZeroU16::new is always Some - ( - NonZeroU16::new(node.len() as u16), - format!("{}@{}", node.as_str(), domain.as_str()), - ) + at = NonZeroU16::new(node.len() as u16); + normalized = format!("{node}@{domain}"); } else { - (None, domain.to_string()) - }; + at = None; + normalized = domain.to_string(); + } let inner = Jid { normalized,