simplify domainpart

This commit is contained in:
cel 🌸 2023-06-13 00:53:11 +01:00
parent 2b2306b57c
commit 9cdf4953fe
Signed by: cel
GPG Key ID: 48E29AF13B5F1349
1 changed files with 3 additions and 48 deletions

View File

@ -1,53 +1,16 @@
use std::{
net::{Ipv4Addr, Ipv6Addr},
str::FromStr,
};
use std::str::FromStr;
#[derive(PartialEq, Debug)]
struct JID {
// TODO: validate localpart (length, char]
localpart: Option<String>,
domainpart: Domainpart,
domainpart: String,
resourcepart: Option<String>,
}
#[derive(PartialEq, Debug)]
enum Domainpart {
IPLiteral(Ipv6Addr),
IPv4Address(Ipv4Addr),
// TODO: domain name type, not string
IFQDN(String),
}
impl FromStr for Domainpart {
type Err = DomainpartParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<Ipv6Addr>() {
Ok(ip) => Ok(Domainpart::IPLiteral(ip)),
Err(_) => match s.parse::<Ipv4Addr>() {
Ok(ip) => Ok(Domainpart::IPv4Address(ip)),
Err(_) => Ok(Domainpart::IFQDN(s.to_owned())),
},
}
}
}
impl TryFrom<String> for Domainpart {
type Error = DomainpartParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
value.parse()
}
}
#[derive(Debug)]
enum DomainpartParseError {}
#[derive(Debug)]
enum JIDParseError {
Empty,
Domainpart(DomainpartParseError),
Malformed,
}
@ -59,10 +22,6 @@ impl JID {
resourcepart,
}
}
fn validate(&self) -> bool {
todo!()
}
}
impl FromStr for JID {
@ -119,11 +78,7 @@ impl std::fmt::Display for JID {
f,
"{}{}{}",
self.localpart.clone().map(|l| l + "@").unwrap_or_default(),
match &self.domainpart {
Domainpart::IPLiteral(addr) => addr.to_string(),
Domainpart::IPv4Address(addr) => addr.to_string(),
Domainpart::IFQDN(domain) => domain.to_owned(),
},
self.domainpart,
self.resourcepart
.clone()
.map(|r| "/".to_owned() + &r)