use std::{
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")]
use diesel::sql_types::Text;
use serde::{
de::{Error as SerdeError, Unexpected, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use url::Host;
use crate::{display, error::Error, 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 RoomAliasId {
alias: String,
hostname: Host,
port: u16,
}
struct RoomAliasIdVisitor;
impl RoomAliasId {
pub fn hostname(&self) -> &Host {
&self.hostname
}
pub fn alias(&self) -> &str {
&self.alias
}
pub fn port(&self) -> u16 {
self.port
}
}
impl Display for RoomAliasId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
display(f, '#', &self.alias, &self.hostname, self.port)
}
}
impl Serialize for RoomAliasId {
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 RoomAliasId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(RoomAliasIdVisitor)
}
}
impl<'a> TryFrom<&'a str> for RoomAliasId {
type Error = Error;
fn try_from(room_id: &'a str) -> Result<Self, Error> {
let (alias, host, port) = parse_id('#', room_id)?;
Ok(Self {
alias: alias.to_owned(),
hostname: host,
port,
})
}
}
impl<'de> Visitor<'de> for RoomAliasIdVisitor {
type Value = RoomAliasId;
fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult {
write!(formatter, "a Matrix room alias ID as a string")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: SerdeError,
{
match RoomAliasId::try_from(v) {
Ok(room_alias_id) => Ok(room_alias_id),
Err(_) => Err(SerdeError::invalid_value(Unexpected::Str(v), &self)),
}
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use serde_json::{from_str, to_string};
use super::RoomAliasId;
use crate::error::Error;
#[test]
fn valid_room_alias_id() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com"
);
}
#[test]
fn serialize_valid_room_alias_id() {
assert_eq!(
to_string(
&RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
)
.expect("Failed to convert RoomAliasId to JSON."),
r##""#ruma:example.com""##
);
}
#[test]
fn deserialize_valid_room_alias_id() {
assert_eq!(
from_str::<RoomAliasId>(r##""#ruma:example.com""##)
.expect("Failed to convert JSON to RoomAliasId"),
RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
);
}
#[test]
fn valid_room_alias_id_with_explicit_standard_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:443")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com"
);
}
#[test]
fn valid_room_alias_id_with_non_standard_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:5000")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#ruma:example.com:5000"
);
}
#[test]
fn valid_room_alias_id_unicode() {
assert_eq!(
RoomAliasId::try_from("#老虎£я:example.com")
.expect("Failed to create RoomAliasId.")
.to_string(),
"#老虎£я:example.com"
);
}
#[test]
fn missing_room_alias_id_sigil() {
assert_eq!(
RoomAliasId::try_from("39hvsi03hlne:example.com")
.err()
.unwrap(),
Error::MissingSigil
);
}
#[test]
fn missing_room_alias_id_delimiter() {
assert_eq!(
RoomAliasId::try_from("#ruma").err().unwrap(),
Error::MissingDelimiter
);
}
#[test]
fn invalid_room_alias_id_host() {
assert_eq!(
RoomAliasId::try_from("#ruma:/").err().unwrap(),
Error::InvalidHost
);
}
#[test]
fn invalid_room_alias_id_port() {
assert_eq!(
RoomAliasId::try_from("#ruma:example.com:notaport")
.err()
.unwrap(),
Error::InvalidHost
);
}
}