luz/src/jid.rs

182 lines
4.4 KiB
Rust
Raw Normal View History

2023-06-13 00:53:11 +01:00
use std::str::FromStr;
2023-06-13 00:46:59 +01:00
use serde::Serialize;
2023-07-11 21:28:42 +01:00
#[derive(PartialEq, Debug, Clone)]
2023-06-16 14:48:19 +01:00
pub struct JID {
2023-06-13 00:46:59 +01:00
// TODO: validate localpart (length, char]
2023-06-16 14:48:19 +01:00
pub localpart: Option<String>,
pub domainpart: String,
pub resourcepart: Option<String>,
2023-06-13 00:46:59 +01:00
}
impl Serialize for JID {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
2023-07-04 21:27:15 +01:00
pub enum JIDError {
NoResourcePart,
ParseError(ParseError),
}
2023-06-13 00:46:59 +01:00
#[derive(Debug)]
2023-07-04 21:27:15 +01:00
pub enum ParseError {
2023-06-13 00:46:59 +01:00
Empty,
Malformed,
}
impl JID {
2023-06-16 14:48:19 +01:00
pub fn new(
localpart: Option<String>,
domainpart: String,
resourcepart: Option<String>,
) -> Self {
2023-06-13 00:46:59 +01:00
Self {
localpart,
domainpart: domainpart.parse().unwrap(),
resourcepart,
}
}
2023-07-04 21:27:15 +01:00
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)
}
}
2023-06-13 00:46:59 +01:00
}
impl FromStr for JID {
2023-07-04 21:27:15 +01:00
type Err = ParseError;
2023-06-13 00:46:59 +01:00
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split: Vec<&str> = s.split('@').collect();
match split.len() {
2023-07-04 21:27:15 +01:00
0 => Err(ParseError::Empty),
2023-06-13 00:46:59 +01:00
1 => {
let split: Vec<&str> = split[0].split('/').collect();
match split.len() {
1 => Ok(JID::new(None, split[0].to_string(), None)),
2 => Ok(JID::new(
None,
split[0].to_string(),
Some(split[1].to_string()),
)),
2023-07-04 21:27:15 +01:00
_ => Err(ParseError::Malformed),
2023-06-13 00:46:59 +01:00
}
}
2 => {
let split2: Vec<&str> = split[1].split('/').collect();
match split2.len() {
1 => Ok(JID::new(
Some(split[0].to_string()),
split2[0].to_string(),
None,
)),
2 => Ok(JID::new(
Some(split[0].to_string()),
split2[0].to_string(),
Some(split2[1].to_string()),
)),
2023-07-04 21:27:15 +01:00
_ => Err(ParseError::Malformed),
2023-06-13 00:46:59 +01:00
}
}
2023-07-04 21:27:15 +01:00
_ => Err(ParseError::Malformed),
2023-06-13 00:46:59 +01:00
}
}
}
impl TryFrom<String> for JID {
2023-07-04 21:27:15 +01:00
type Error = ParseError;
2023-06-13 00:46:59 +01:00
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}
impl TryFrom<&str> for JID {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
2023-06-13 00:46:59 +01:00
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(),
2023-06-13 00:53:11 +01:00
self.domainpart,
2023-06-13 00:46:59 +01:00
self.resourcepart
.clone()
.map(|r| "/".to_owned() + &r)
.unwrap_or_default()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn jid_to_string() {
assert_eq!(
JID::new(Some("cel".into()), "blos.sm".into(), None).to_string(),
"cel@blos.sm".to_owned()
);
}
#[test]
fn parse_full_jid() {
assert_eq!(
"cel@blos.sm/greenhouse".parse::<JID>().unwrap(),
JID::new(
Some("cel".into()),
"blos.sm".into(),
Some("greenhouse".into())
)
)
}
#[test]
fn parse_bare_jid() {
assert_eq!(
"cel@blos.sm".parse::<JID>().unwrap(),
JID::new(Some("cel".into()), "blos.sm".into(), None)
)
}
#[test]
fn parse_domain_jid() {
assert_eq!(
"component.blos.sm".parse::<JID>().unwrap(),
JID::new(None, "component.blos.sm".into(), None)
)
}
#[test]
fn parse_full_domain_jid() {
assert_eq!(
"component.blos.sm/bot".parse::<JID>().unwrap(),
JID::new(None, "component.blos.sm".into(), Some("bot".into()))
)
}
}