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, 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 EventId(Format);
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum Format {
Original(Original),
Base64(String),
UrlSafeBase64(String),
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct Original {
pub hostname: Host,
pub localpart: String,
pub port: u16,
}
struct EventIdVisitor;
impl EventId {
pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let event_id = format!("${}:{}", generate_localpart(18), homeserver_host);
let (localpart, host, port) = parse_id('$', &event_id)?;
Ok(Self(Format::Original(Original {
hostname: host,
localpart: localpart.to_string(),
port,
})))
}
pub fn hostname(&self) -> Option<&Host> {
if let Format::Original(original) = &self.0 {
Some(&original.hostname)
} else {
None
}
}
pub fn localpart(&self) -> &str {
match &self.0 {
Format::Original(original) => &original.localpart,
Format::Base64(id) | Format::UrlSafeBase64(id) => id,
}
}
pub fn port(&self) -> Option<u16> {
if let Format::Original(original) = &self.0 {
Some(original.port)
} else {
None
}
}
}
impl Display for EventId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.0 {
Format::Original(original) => display(
f,
'$',
&original.localpart,
&original.hostname,
original.port,
),
Format::Base64(id) | Format::UrlSafeBase64(id) => write!(f, "${}", id),
}
}
}
impl Serialize for EventId {
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 EventId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(EventIdVisitor)
}
}
impl<'a> TryFrom<&'a str> for EventId {
type Error = Error;
fn try_from(event_id: &'a str) -> Result<Self, Self::Error> {
if event_id.contains(':') {
let (localpart, host, port) = parse_id('$', event_id)?;
Ok(Self(Format::Original(Original {
hostname: host,
localpart: localpart.to_owned(),
port,
})))
} else if !event_id.starts_with('$') {
Err(Error::MissingSigil)
} else if event_id.contains(|chr| chr == '+' || chr == '/') {
Ok(Self(Format::Base64(event_id[1..].to_string())))
} else {
Ok(Self(Format::UrlSafeBase64(event_id[1..].to_string())))
}
}
}
impl<'de> Visitor<'de> for EventIdVisitor {
type Value = EventId;
fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult {
write!(formatter, "a Matrix event ID as a string")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: SerdeError,
{
match EventId::try_from(v) {
Ok(event_id) => Ok(event_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::EventId;
use crate::error::Error;
#[test]
fn valid_original_event_id() {
assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com")
.expect("Failed to create EventId.")
.to_string(),
"$39hvsi03hlne:example.com"
);
}
#[test]
fn valid_base64_event_id() {
assert_eq!(
EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk")
.expect("Failed to create EventId.")
.to_string(),
"$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk"
)
}
#[test]
fn valid_url_safe_base64_event_id() {
assert_eq!(
EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg")
.expect("Failed to create EventId.")
.to_string(),
"$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
)
}
#[test]
fn generate_random_valid_event_id() {
let event_id = EventId::new("example.com")
.expect("Failed to generate EventId.")
.to_string();
assert!(event_id.to_string().starts_with('$'));
assert_eq!(event_id.len(), 31);
}
#[test]
fn generate_random_invalid_event_id() {
assert!(EventId::new("").is_err());
}
#[test]
fn serialize_valid_original_event_id() {
assert_eq!(
to_string(
&EventId::try_from("$39hvsi03hlne:example.com").expect("Failed to create EventId.")
)
.expect("Failed to convert EventId to JSON."),
r#""$39hvsi03hlne:example.com""#
);
}
#[test]
fn serialize_valid_base64_event_id() {
assert_eq!(
to_string(
&EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk")
.expect("Failed to create EventId.")
)
.expect("Failed to convert EventId to JSON."),
r#""$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk""#
);
}
#[test]
fn serialize_valid_url_safe_base64_event_id() {
assert_eq!(
to_string(
&EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg")
.expect("Failed to create EventId.")
)
.expect("Failed to convert EventId to JSON."),
r#""$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg""#
);
}
#[test]
fn deserialize_valid_original_event_id() {
assert_eq!(
from_str::<EventId>(r#""$39hvsi03hlne:example.com""#)
.expect("Failed to convert JSON to EventId"),
EventId::try_from("$39hvsi03hlne:example.com").expect("Failed to create EventId.")
);
}
#[test]
fn deserialize_valid_base64_event_id() {
assert_eq!(
from_str::<EventId>(r#""$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk""#)
.expect("Failed to convert JSON to EventId"),
EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk")
.expect("Failed to create EventId.")
);
}
#[test]
fn deserialize_valid_url_safe_base64_event_id() {
assert_eq!(
from_str::<EventId>(r#""$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg""#)
.expect("Failed to convert JSON to EventId"),
EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg")
.expect("Failed to create EventId.")
);
}
#[test]
fn valid_original_event_id_with_explicit_standard_port() {
assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com:443")
.expect("Failed to create EventId.")
.to_string(),
"$39hvsi03hlne:example.com"
);
}
#[test]
fn valid_original_event_id_with_non_standard_port() {
assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com:5000")
.expect("Failed to create EventId.")
.to_string(),
"$39hvsi03hlne:example.com:5000"
);
}
#[test]
fn missing_original_event_id_sigil() {
assert_eq!(
EventId::try_from("39hvsi03hlne:example.com").err().unwrap(),
Error::MissingSigil
);
}
#[test]
fn missing_base64_event_id_sigil() {
assert_eq!(
EventId::try_from("acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk")
.err()
.unwrap(),
Error::MissingSigil
);
}
#[test]
fn missing_url_safe_base64_event_id_sigil() {
assert_eq!(
EventId::try_from("Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg")
.err()
.unwrap(),
Error::MissingSigil
);
}
#[test]
fn invalid_event_id_host() {
assert_eq!(
EventId::try_from("$39hvsi03hlne:/").err().unwrap(),
Error::InvalidHost
);
}
#[test]
fn invalid_event_id_port() {
assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com:notaport")
.err()
.unwrap(),
Error::InvalidHost
);
}
}