diff options
Diffstat (limited to 'src/jid')
| -rw-r--r-- | src/jid/mod.rs | 35 | 
1 files changed, 28 insertions, 7 deletions
| diff --git a/src/jid/mod.rs b/src/jid/mod.rs index 4baa857..b2a03ea 100644 --- a/src/jid/mod.rs +++ b/src/jid/mod.rs @@ -8,8 +8,13 @@ pub struct JID {      pub resourcepart: Option<String>,  } +pub enum JIDError { +    NoResourcePart, +    ParseError(ParseError), +} +  #[derive(Debug)] -pub enum JIDParseError { +pub enum ParseError {      Empty,      Malformed,  } @@ -26,15 +31,31 @@ impl JID {              resourcepart,          }      } + +    pub fn as_bare(&self) -> Self { +        Self { +            localpart: self.localpart.clone(), +            domainpart: self.domainpart.clone(), +            resourcepart: None, +        } +    } + +    pub fn as_full(&self) -> Result<&Self, JIDError> { +        if let Some(_) = self.resourcepart { +            Ok(&self) +        } else { +            Err(JIDError::NoResourcePart) +        } +    }  }  impl FromStr for JID { -    type Err = JIDParseError; +    type Err = ParseError;      fn from_str(s: &str) -> Result<Self, Self::Err> {          let split: Vec<&str> = s.split('@').collect();          match split.len() { -            0 => Err(JIDParseError::Empty), +            0 => Err(ParseError::Empty),              1 => {                  let split: Vec<&str> = split[0].split('/').collect();                  match split.len() { @@ -44,7 +65,7 @@ impl FromStr for JID {                          split[0].to_string(),                          Some(split[1].to_string()),                      )), -                    _ => Err(JIDParseError::Malformed), +                    _ => Err(ParseError::Malformed),                  }              }              2 => { @@ -60,16 +81,16 @@ impl FromStr for JID {                          split2[0].to_string(),                          Some(split2[1].to_string()),                      )), -                    _ => Err(JIDParseError::Malformed), +                    _ => Err(ParseError::Malformed),                  }              } -            _ => Err(JIDParseError::Malformed), +            _ => Err(ParseError::Malformed),          }      }  }  impl TryFrom<String> for JID { -    type Error = JIDParseError; +    type Error = ParseError;      fn try_from(value: String) -> Result<Self, Self::Error> {          value.parse() | 
