diff options
author | cel 🌸 <cel@blos.sm> | 2023-07-04 21:27:15 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-07-04 21:27:15 +0100 |
commit | 143a0365d0822e6786cdac3530a725bbf450f38f (patch) | |
tree | 8b540aa73c1365ddc658e502ed93847dbb522064 /src/jid/mod.rs | |
parent | c0a7116eef13ea75340fe7d75da97dfbd04fac20 (diff) | |
download | luz-143a0365d0822e6786cdac3530a725bbf450f38f.tar.gz luz-143a0365d0822e6786cdac3530a725bbf450f38f.tar.bz2 luz-143a0365d0822e6786cdac3530a725bbf450f38f.zip |
horrible
Diffstat (limited to 'src/jid/mod.rs')
-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() |