diff options
| author | 2025-02-25 18:43:12 +0000 | |
|---|---|---|
| committer | 2025-02-25 18:43:12 +0000 | |
| commit | 65e908e36c2f9f9badc235de7a40e15836c77a8c (patch) | |
| tree | 14160a290b5e105d36cf60aa802286f9c3136dd7 /jid | |
| parent | eda4bd92ff1f83e905b84b4b2e9497389ee03466 (diff) | |
| download | luz-65e908e36c2f9f9badc235de7a40e15836c77a8c.tar.gz luz-65e908e36c2f9f9badc235de7a40e15836c77a8c.tar.bz2 luz-65e908e36c2f9f9badc235de7a40e15836c77a8c.zip | |
implement Into<Cow> for &JID
Diffstat (limited to 'jid')
| -rw-r--r-- | jid/src/lib.rs | 44 | 
1 files changed, 28 insertions, 16 deletions
| diff --git a/jid/src/lib.rs b/jid/src/lib.rs index 543d1ba..878b6f7 100644 --- a/jid/src/lib.rs +++ b/jid/src/lib.rs @@ -1,4 +1,9 @@ -use std::{error::Error, fmt::Display, str::FromStr}; +use std::{ +    borrow::Cow, +    error::Error, +    fmt::{Display, Write}, +    str::FromStr, +};  use sqlx::Sqlite; @@ -10,6 +15,28 @@ pub struct JID {      pub resourcepart: Option<String>,  } +impl<'a> Into<Cow<'a, str>> for &'a JID { +    fn into(self) -> Cow<'a, str> { +        let a = self.to_string(); +        Cow::Owned(a) +    } +} + +impl Display for JID { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        if let Some(localpart) = &self.localpart { +            f.write_str(localpart)?; +            f.write_str("@")?; +        } +        f.write_str(&self.domainpart)?; +        if let Some(resourcepart) = &self.resourcepart { +            f.write_str("/")?; +            f.write_str(resourcepart)?; +        } +        Ok(()) +    } +} +  // TODO: feature gate  impl sqlx::Type<Sqlite> for JID {      fn type_info() -> <Sqlite as sqlx::Database>::TypeInfo { @@ -147,21 +174,6 @@ impl TryFrom<&str> for JID {      }  } -impl std::fmt::Display for JID { -    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -        write!( -            f, -            "{}{}{}", -            self.localpart.clone().map(|l| l + "@").unwrap_or_default(), -            self.domainpart, -            self.resourcepart -                .clone() -                .map(|r| "/".to_owned() + &r) -                .unwrap_or_default() -        ) -    } -} -  #[cfg(test)]  mod tests {      use super::*; | 
