use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")]
use diesel::sql_types::Text;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use url::Host;
use crate::{deserialize_id, display, error::Error, generate_localpart, parse_id};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct UserId {
hostname: Host,
localpart: String,
port: u16,
is_historical: bool,
}
impl UserId {
pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let user_id = format!(
"@{}:{}",
generate_localpart(12).to_lowercase(),
homeserver_host
);
let (localpart, host, port) = parse_id('@', &user_id)?;
Ok(Self {
hostname: host,
localpart: localpart.to_string(),
port,
is_historical: false,
})
}
pub fn hostname(&self) -> &Host {
&self.hostname
}
pub fn localpart(&self) -> &str {
&self.localpart
}
pub fn port(&self) -> u16 {
self.port
}
pub fn is_historical(&self) -> bool {
self.is_historical
}
}
impl Display for UserId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
display(f, '@', &self.localpart, &self.hostname, self.port)
}
}
impl Serialize for UserId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for UserId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_id(deserializer, "a Matrix user ID as a string")
}
}
impl<'a> TryFrom<&'a str> for UserId {
type Error = Error;
fn try_from(user_id: &'a str) -> Result<Self, Error> {
let (localpart, host, port) = parse_id('@', user_id)?;
let downcased_localpart = localpart.to_lowercase();
let is_fully_conforming = downcased_localpart.bytes().all(|b| match b {
b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.' | b'=' | b'_' | b'/' => true,
_ => false,
});
if !is_fully_conforming
&& downcased_localpart
.bytes()
.any(|b| b < 0x21 || b == b':' || b > 0x7E)
{
return Err(Error::InvalidCharacters);
}
Ok(Self {
hostname: host,
port,
localpart: downcased_localpart,
is_historical: !is_fully_conforming,
})
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use serde_json::{from_str, to_string};
use super::UserId;
use crate::error::Error;
#[test]
fn valid_user_id() {
let user_id = UserId::try_from("@carl:example.com").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@carl:example.com");
assert!(!user_id.is_historical());
}
#[test]
fn valid_historical_user_id() {
let user_id = UserId::try_from("@a%b[irc]:example.com").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@a%b[irc]:example.com");
assert!(user_id.is_historical());
}
#[test]
fn downcase_user_id() {
assert_eq!(
UserId::try_from("@CARL:example.com")
.expect("Failed to create UserId.")
.to_string(),
"@carl:example.com"
);
}
#[test]
fn generate_random_valid_user_id() {
let user_id = UserId::new("example.com")
.expect("Failed to generate UserId.")
.to_string();
assert!(user_id.to_string().starts_with('@'));
assert_eq!(user_id.len(), 25);
}
#[test]
fn generate_random_invalid_user_id() {
assert!(UserId::new("").is_err());
}
#[test]
fn serialize_valid_user_id() {
assert_eq!(
to_string(&UserId::try_from("@carl:example.com").expect("Failed to create UserId."))
.expect("Failed to convert UserId to JSON."),
r#""@carl:example.com""#
);
}
#[test]
fn deserialize_valid_user_id() {
assert_eq!(
from_str::<UserId>(r#""@carl:example.com""#).expect("Failed to convert JSON to UserId"),
UserId::try_from("@carl:example.com").expect("Failed to create UserId.")
);
}
#[test]
fn valid_user_id_with_explicit_standard_port() {
assert_eq!(
UserId::try_from("@carl:example.com:443")
.expect("Failed to create UserId.")
.to_string(),
"@carl:example.com"
);
}
#[test]
fn valid_user_id_with_non_standard_port() {
let user_id = UserId::try_from("@carl:example.com:5000").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@carl:example.com:5000");
assert!(!user_id.is_historical());
}
#[test]
fn invalid_characters_in_user_id_localpart() {
assert_eq!(
UserId::try_from("@te\nst:example.com").unwrap_err(),
Error::InvalidCharacters
);
}
#[test]
fn missing_user_id_sigil() {
assert_eq!(
UserId::try_from("carl:example.com").unwrap_err(),
Error::MissingSigil
);
}
#[test]
fn missing_user_id_delimiter() {
assert_eq!(
UserId::try_from("@carl").unwrap_err(),
Error::MissingDelimiter
);
}
#[test]
fn invalid_user_id_host() {
assert_eq!(UserId::try_from("@carl:/").unwrap_err(), Error::InvalidHost);
}
#[test]
fn invalid_user_id_port() {
assert_eq!(
UserId::try_from("@carl:example.com:notaport").unwrap_err(),
Error::InvalidHost
);
}
}