New representation for identifiers

Identifiers now always own just a single allocation, and implement AsRef<str>.
PartialOrd and Ord implementations were also added at the same time. All
identifiers except RoomVersionId can now also be converted to String without
allocation.
This commit is contained in:
Jonas Platte 2020-04-15 13:54:31 +02:00 committed by GitHub
parent 791b510760
commit 3945f88e10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 369 additions and 509 deletions

View File

@ -16,7 +16,6 @@ edition = "2018"
diesel = { version = "1.4.3", optional = true } diesel = { version = "1.4.3", optional = true }
rand = "0.7.2" rand = "0.7.2"
serde = "1.0.102" serde = "1.0.102"
url = "2.1.0"
[dev-dependencies] [dev-dependencies]
serde_json = "1.0.41" serde_json = "1.0.41"

View File

@ -16,7 +16,7 @@ macro_rules! diesel_impl {
DB: Backend, DB: Backend,
{ {
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> SerializeResult { fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> SerializeResult {
ToSql::<Text, DB>::to_sql(&self.to_string(), out) ToSql::<Text, DB>::to_sql(self.as_ref(), out)
} }
} }

View File

@ -1,11 +1,6 @@
//! Error conditions. //! Error conditions.
use std::{ use std::fmt::{self, Display, Formatter};
error::Error as StdError,
fmt::{Display, Formatter, Result as FmtResult},
};
use url::ParseError;
/// An error encountered when trying to parse an invalid ID string. /// An error encountered when trying to parse an invalid ID string.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
@ -14,6 +9,8 @@ pub enum Error {
/// ///
/// Only relevant for user IDs. /// Only relevant for user IDs.
InvalidCharacters, InvalidCharacters,
/// The localpart of the ID string is not valid (because it is empty).
InvalidLocalPart,
/// The domain part of the the ID string is not a valid IP address or DNS name. /// The domain part of the the ID string is not a valid IP address or DNS name.
InvalidHost, InvalidHost,
/// The ID exceeds 255 bytes (or 32 codepoints for a room version ID.) /// The ID exceeds 255 bytes (or 32 codepoints for a room version ID.)
@ -27,9 +24,10 @@ pub enum Error {
} }
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let message = match *self { let message = match self {
Error::InvalidCharacters => "localpart contains invalid characters", Error::InvalidCharacters => "localpart contains invalid characters",
Error::InvalidLocalPart => "localpart is empty",
Error::InvalidHost => "server name is not a valid IP address or domain name", Error::InvalidHost => "server name is not a valid IP address or domain name",
Error::MaximumLengthExceeded => "ID exceeds 255 bytes", Error::MaximumLengthExceeded => "ID exceeds 255 bytes",
Error::MinimumLengthNotSatisfied => "ID must be at least 4 characters", Error::MinimumLengthNotSatisfied => "ID must be at least 4 characters",
@ -41,10 +39,4 @@ impl Display for Error {
} }
} }
impl StdError for Error {} impl std::error::Error for Error {}
impl From<ParseError> for Error {
fn from(_: ParseError) -> Self {
Error::InvalidHost
}
}

View File

@ -1,16 +1,11 @@
//! Matrix event identifiers. //! Matrix event identifiers.
use std::{ use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
use diesel::sql_types::Text; 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}; use crate::{error::Error, generate_localpart, parse_id, validate_id};
/// A Matrix event ID. /// A Matrix event ID.
/// ///
@ -31,45 +26,26 @@ use crate::{deserialize_id, display, error::Error, generate_localpart, parse_id}
/// # use ruma_identifiers::EventId; /// # use ruma_identifiers::EventId;
/// // Original format /// // Original format
/// assert_eq!( /// assert_eq!(
/// EventId::try_from("$h29iv0s8:example.com").unwrap().to_string(), /// EventId::try_from("$h29iv0s8:example.com").unwrap().as_ref(),
/// "$h29iv0s8:example.com" /// "$h29iv0s8:example.com"
/// ); /// );
/// // Room version 3 format /// // Room version 3 format
/// assert_eq!( /// assert_eq!(
/// EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap().to_string(), /// EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk").unwrap().as_ref(),
/// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk" /// "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk"
/// ); /// );
/// // Room version 4 format /// // Room version 4 format
/// assert_eq!( /// assert_eq!(
/// EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap().to_string(), /// EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg").unwrap().as_ref(),
/// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" /// "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")] #[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct EventId(Format); pub struct EventId {
full_id: String,
/// Different event ID formats from the different Matrix room versions. colon_idx: Option<NonZeroU8>,
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum Format {
/// The original format as used by Matrix room versions 1 and 2.
Original(Original),
/// The format used by Matrix room version 3.
Base64(String),
/// The format used by Matrix room version 4.
UrlSafeBase64(String),
}
/// An event in the original format as used by Matrix room versions 1 and 2.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct Original {
/// The hostname of the homeserver.
pub hostname: Host,
/// The event's unique ID.
pub localpart: String,
/// The network port of the homeserver.
pub port: u16,
} }
impl EventId { impl EventId {
@ -77,86 +53,39 @@ impl EventId {
/// of 18 random ASCII characters. This should only be used for events in the original format /// of 18 random ASCII characters. This should only be used for events in the original format
/// as used by Matrix room versions 1 and 2. /// as used by Matrix room versions 1 and 2.
/// ///
/// Fails if the homeserver cannot be parsed as a valid host. /// Does not currently ever fail, but may fail in the future if the homeserver cannot be parsed
/// parsed as a valid host.
pub fn new(homeserver_host: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let event_id = format!("${}:{}", generate_localpart(18), homeserver_host); let full_id = format!("${}:{}", generate_localpart(18), homeserver_host);
let (localpart, host, port) = parse_id('$', &event_id)?;
Ok(Self(Format::Original(Original { Ok(Self {
hostname: host, full_id,
localpart: localpart.to_string(), colon_idx: NonZeroU8::new(19),
port, })
})))
} }
/// Returns a `Host` for the event ID, containing the server name (minus the port) of the /// Returns the host of the event ID, containing the server name (including the port) of the
/// originating homeserver. Only applicable to events in the original format as used by Matrix /// originating homeserver. Only applicable to events in the original format as used by Matrix
/// room versions 1 and 2. /// room versions 1 and 2.
/// pub fn hostname(&self) -> Option<&str> {
/// The host can be either a domain name, an IPv4 address, or an IPv6 address. self.colon_idx
pub fn hostname(&self) -> Option<&Host> { .map(|idx| &self.full_id[idx.get() as usize + 1..])
if let Format::Original(original) = &self.0 {
Some(&original.hostname)
} else {
None
}
} }
/// Returns the event's unique ID. For the original event format as used by Matrix room /// Returns the event's unique ID. For the original event format as used by Matrix room
/// versions 1 and 2, this is the "localpart" that precedes the homeserver. For later formats, /// versions 1 and 2, this is the "localpart" that precedes the homeserver. For later formats,
/// this is the entire ID without the leading $ sigil. /// this is the entire ID without the leading $ sigil.
pub fn localpart(&self) -> &str { pub fn localpart(&self) -> &str {
match &self.0 { let idx = match self.colon_idx {
Format::Original(original) => &original.localpart, Some(idx) => idx.get() as usize,
Format::Base64(id) | Format::UrlSafeBase64(id) => id, None => self.full_id.len(),
} };
}
/// Returns the port the originating homeserver can be accessed on. Only applicable to events &self.full_id[1..idx]
/// in the original format as used by Matrix room versions 1 and 2.
pub fn port(&self) -> Option<u16> {
if let Format::Original(original) = &self.0 {
Some(original.port)
} else {
None
}
} }
} }
impl Display for EventId { impl TryFrom<Cow<'_, str>> 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>,
{
deserialize_id(deserializer, "a Matrix event ID as a string")
}
}
impl TryFrom<&str> for EventId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix event ID from a string representation. /// Attempts to create a new Matrix event ID from a string representation.
@ -164,25 +93,27 @@ impl TryFrom<&str> for EventId {
/// If using the original event format as used by Matrix room versions 1 and 2, the string must /// If using the original event format as used by Matrix room versions 1 and 2, the string must
/// include the leading $ sigil, the localpart, a literal colon, and a valid homeserver /// include the leading $ sigil, the localpart, a literal colon, and a valid homeserver
/// hostname. /// hostname.
fn try_from(event_id: &str) -> Result<Self, Self::Error> { fn try_from(event_id: Cow<'_, str>) -> Result<Self, Self::Error> {
if event_id.contains(':') { if event_id.contains(':') {
let (localpart, host, port) = parse_id('$', event_id)?; let colon_idx = parse_id(&event_id, &['$'])?;
Ok(Self(Format::Original(Original { Ok(Self {
hostname: host, full_id: event_id.into_owned(),
localpart: localpart.to_owned(), colon_idx: Some(colon_idx),
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 { } else {
Ok(Self(Format::UrlSafeBase64(event_id[1..].to_string()))) validate_id(&event_id, &['$'])?;
Ok(Self {
full_id: event_id.into_owned(),
colon_idx: None,
})
} }
} }
} }
common_impls!(EventId, "a Matrix event ID");
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -197,7 +128,7 @@ mod tests {
assert_eq!( assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com") EventId::try_from("$39hvsi03hlne:example.com")
.expect("Failed to create EventId.") .expect("Failed to create EventId.")
.to_string(), .as_ref(),
"$39hvsi03hlne:example.com" "$39hvsi03hlne:example.com"
); );
} }
@ -207,7 +138,7 @@ mod tests {
assert_eq!( assert_eq!(
EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk") EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk")
.expect("Failed to create EventId.") .expect("Failed to create EventId.")
.to_string(), .as_ref(),
"$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk" "$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk"
) )
} }
@ -217,25 +148,24 @@ mod tests {
assert_eq!( assert_eq!(
EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg") EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg")
.expect("Failed to create EventId.") .expect("Failed to create EventId.")
.to_string(), .as_ref(),
"$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg" "$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg"
) )
} }
#[test] #[test]
fn generate_random_valid_event_id() { fn generate_random_valid_event_id() {
let event_id = EventId::new("example.com") let event_id = EventId::new("example.com").expect("Failed to generate EventId.");
.expect("Failed to generate EventId.") let id_str: &str = event_id.as_ref();
.to_string();
assert!(event_id.to_string().starts_with('$')); assert!(id_str.starts_with('$'));
assert_eq!(event_id.len(), 31); assert_eq!(id_str.len(), 31);
} }
#[test] /*#[test]
fn generate_random_invalid_event_id() { fn generate_random_invalid_event_id() {
assert!(EventId::new("").is_err()); assert!(EventId::new("").is_err());
} }*/
#[test] #[test]
fn serialize_valid_original_event_id() { fn serialize_valid_original_event_id() {
@ -306,8 +236,8 @@ mod tests {
assert_eq!( assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com:443") EventId::try_from("$39hvsi03hlne:example.com:443")
.expect("Failed to create EventId.") .expect("Failed to create EventId.")
.to_string(), .as_ref(),
"$39hvsi03hlne:example.com" "$39hvsi03hlne:example.com:443"
); );
} }
@ -316,7 +246,7 @@ mod tests {
assert_eq!( assert_eq!(
EventId::try_from("$39hvsi03hlne:example.com:5000") EventId::try_from("$39hvsi03hlne:example.com:5000")
.expect("Failed to create EventId.") .expect("Failed to create EventId.")
.to_string(), .as_ref(),
"$39hvsi03hlne:example.com:5000" "$39hvsi03hlne:example.com:5000"
); );
} }
@ -345,7 +275,7 @@ mod tests {
); );
} }
#[test] /*#[test]
fn invalid_event_id_host() { fn invalid_event_id_host() {
assert_eq!( assert_eq!(
EventId::try_from("$39hvsi03hlne:/").unwrap_err(), EventId::try_from("$39hvsi03hlne:/").unwrap_err(),
@ -359,5 +289,5 @@ mod tests {
EventId::try_from("$39hvsi03hlne:example.com:notaport").unwrap_err(), EventId::try_from("$39hvsi03hlne:example.com:notaport").unwrap_err(),
Error::InvalidHost Error::InvalidHost
); );
} }*/
} }

View File

@ -14,17 +14,10 @@
#[cfg_attr(feature = "diesel", macro_use)] #[cfg_attr(feature = "diesel", macro_use)]
extern crate diesel; extern crate diesel;
use std::{ use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
borrow::Cow,
convert::TryFrom,
fmt::{Formatter, Result as FmtResult},
};
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::de::{self, Deserialize as _, Deserializer, Unexpected}; use serde::de::{self, Deserialize as _, Deserializer, Unexpected};
use url::Url;
pub use url::Host;
#[doc(inline)] #[doc(inline)]
pub use crate::device_id::DeviceId; pub use crate::device_id::DeviceId;
@ -33,6 +26,9 @@ pub use crate::{
room_id_or_room_alias_id::RoomIdOrAliasId, room_version_id::RoomVersionId, user_id::UserId, room_id_or_room_alias_id::RoomIdOrAliasId, room_version_id::RoomVersionId, user_id::UserId,
}; };
#[macro_use]
mod macros;
pub mod device_id; pub mod device_id;
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
mod diesel_integration; mod diesel_integration;
@ -51,23 +47,6 @@ const MAX_BYTES: usize = 255;
/// This is an optimization and not required by the spec. The shortest possible valid ID is a sigil /// This is an optimization and not required by the spec. The shortest possible valid ID is a sigil
/// + a single character local ID + a colon + a single character hostname. /// + a single character local ID + a colon + a single character hostname.
const MIN_CHARS: usize = 4; const MIN_CHARS: usize = 4;
/// The number of bytes in a valid sigil.
const SIGIL_BYTES: usize = 1;
/// `Display` implementation shared by identifier types.
fn display(
f: &mut Formatter<'_>,
sigil: char,
localpart: &str,
hostname: &Host,
port: u16,
) -> FmtResult {
if port == 443 {
write!(f, "{}{}:{}", sigil, localpart, hostname)
} else {
write!(f, "{}{}:{}:{}", sigil, localpart, hostname, port)
}
}
/// Generates a random identifier localpart. /// Generates a random identifier localpart.
fn generate_localpart(length: usize) -> String { fn generate_localpart(length: usize) -> String {
@ -77,8 +56,8 @@ fn generate_localpart(length: usize) -> String {
.collect() .collect()
} }
/// Checks if an identifier is within the acceptable byte lengths. /// Checks if an identifier is valid.
fn validate_id(id: &str) -> Result<(), Error> { fn validate_id(id: &str, valid_sigils: &[char]) -> Result<(), Error> {
if id.len() > MAX_BYTES { if id.len() > MAX_BYTES {
return Err(Error::MaximumLengthExceeded); return Err(Error::MaximumLengthExceeded);
} }
@ -87,35 +66,27 @@ fn validate_id(id: &str) -> Result<(), Error> {
return Err(Error::MinimumLengthNotSatisfied); return Err(Error::MinimumLengthNotSatisfied);
} }
Ok(()) if !valid_sigils.contains(&id.chars().next().unwrap()) {
}
/// Parses the localpart, host, and port from a string identifier.
fn parse_id(required_sigil: char, id: &str) -> Result<(&str, Host, u16), Error> {
validate_id(id)?;
if !id.starts_with(required_sigil) {
return Err(Error::MissingSigil); return Err(Error::MissingSigil);
} }
let delimiter_index = match id.find(':') { Ok(())
Some(index) => index, }
None => return Err(Error::MissingDelimiter),
};
let localpart = &id[1..delimiter_index]; /// Checks an identifier that contains a localpart and hostname for validity,
let raw_host = &id[delimiter_index + SIGIL_BYTES..]; /// and returns the index of the colon that separates the two.
let url_string = format!("https://{}", raw_host); fn parse_id(id: &str, valid_sigils: &[char]) -> Result<NonZeroU8, Error> {
let url = Url::parse(&url_string)?; validate_id(id, valid_sigils)?;
let host = match url.host() { let colon_idx = id.find(':').ok_or(Error::MissingDelimiter)?;
Some(host) => host.to_owned(), if colon_idx == id.len() - 1 {
None => return Err(Error::InvalidHost), return Err(Error::InvalidHost);
}; }
let port = url.port().unwrap_or(443); match NonZeroU8::new(colon_idx as u8) {
Some(idx) => Ok(idx),
Ok((localpart, host, port)) None => Err(Error::InvalidLocalPart),
}
} }
/// Deserializes any type of id using the provided TryFrom implementation. /// Deserializes any type of id using the provided TryFrom implementation.

84
src/macros.rs Normal file
View File

@ -0,0 +1,84 @@
macro_rules! common_impls {
($id:ident, $desc:literal) => {
impl ::std::convert::From<$id> for ::std::string::String {
fn from(id: $id) -> Self {
id.full_id
}
}
impl ::std::convert::TryFrom<&str> for $id {
type Error = crate::error::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::try_from(::std::borrow::Cow::Borrowed(s))
}
}
impl ::std::convert::TryFrom<String> for $id {
type Error = crate::error::Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::try_from(::std::borrow::Cow::Owned(s))
}
}
impl ::std::convert::AsRef<str> for $id {
fn as_ref(&self) -> &str {
&self.full_id
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}", self.full_id)
}
}
impl ::std::cmp::PartialEq for $id {
fn eq(&self, other: &Self) -> bool {
self.full_id == other.full_id
}
}
impl ::std::cmp::Eq for $id {}
impl ::std::cmp::PartialOrd for $id {
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
<::std::string::String as ::std::cmp::PartialOrd>::partial_cmp(
&self.full_id,
&other.full_id,
)
}
}
impl ::std::cmp::Ord for $id {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
<::std::string::String as ::std::cmp::Ord>::cmp(&self.full_id, &other.full_id)
}
}
impl ::std::hash::Hash for $id {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.full_id.hash(state);
}
}
impl ::serde::Serialize for $id {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
serializer.serialize_str(&self.full_id)
}
}
impl<'de> ::serde::Deserialize<'de> for $id {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
crate::deserialize_id(deserializer, $desc)
}
}
};
}

View File

@ -1,16 +1,11 @@
//! Matrix room alias identifiers. //! Matrix room alias identifiers.
use std::{ use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
use diesel::sql_types::Text; use diesel::sql_types::Text;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use url::Host;
use crate::{deserialize_id, display, error::Error, parse_id}; use crate::{error::Error, parse_id};
/// A Matrix room alias ID. /// A Matrix room alias ID.
/// ///
@ -21,84 +16,49 @@ use crate::{deserialize_id, display, error::Error, parse_id};
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use ruma_identifiers::RoomAliasId; /// # use ruma_identifiers::RoomAliasId;
/// assert_eq!( /// assert_eq!(
/// RoomAliasId::try_from("#ruma:example.com").unwrap().to_string(), /// RoomAliasId::try_from("#ruma:example.com").unwrap().as_ref(),
/// "#ruma:example.com" /// "#ruma:example.com"
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")] #[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct RoomAliasId { pub struct RoomAliasId {
/// The alias for the room. full_id: String,
alias: String, colon_idx: NonZeroU8,
/// The hostname of the homeserver.
hostname: Host,
/// The network port of the homeserver.
port: u16,
} }
impl RoomAliasId { impl RoomAliasId {
/// Returns a `Host` for the room alias ID, containing the server name (minus the port) of /// Returns the host of the room alias ID, containing the server name (including the port) of
/// the originating homeserver. /// the originating homeserver.
/// pub fn hostname(&self) -> &str {
/// The host can be either a domain name, an IPv4 address, or an IPv6 address. &self.full_id[self.colon_idx.get() as usize + 1..]
pub fn hostname(&self) -> &Host {
&self.hostname
} }
/// Returns the room's alias. /// Returns the room's alias.
pub fn alias(&self) -> &str { pub fn alias(&self) -> &str {
&self.alias &self.full_id[1..self.colon_idx.get() as usize]
}
/// Returns the port the originating homeserver can be accessed on.
pub fn port(&self) -> u16 {
self.port
} }
} }
impl Display for RoomAliasId { impl TryFrom<Cow<'_, str>> 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>,
{
deserialize_id(deserializer, "a Matrix room alias ID as a string")
}
}
impl TryFrom<&str> for RoomAliasId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix room alias ID from a string representation. /// Attempts to create a new Matrix room alias ID from a string representation.
/// ///
/// The string must include the leading # sigil, the alias, a literal colon, and a valid /// The string must include the leading # sigil, the alias, a literal colon, and a server name.
/// server name. fn try_from(room_id: Cow<'_, str>) -> Result<Self, Error> {
fn try_from(room_id: &str) -> Result<Self, Error> { let colon_idx = parse_id(&room_id, &['#'])?;
let (alias, host, port) = parse_id('#', room_id)?;
Ok(Self { Ok(Self {
alias: alias.to_owned(), full_id: room_id.into_owned(),
hostname: host, colon_idx,
port,
}) })
} }
} }
common_impls!(RoomAliasId, "a Matrix room alias ID");
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -113,7 +73,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomAliasId::try_from("#ruma:example.com") RoomAliasId::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.") .expect("Failed to create RoomAliasId.")
.to_string(), .as_ref(),
"#ruma:example.com" "#ruma:example.com"
); );
} }
@ -143,8 +103,8 @@ mod tests {
assert_eq!( assert_eq!(
RoomAliasId::try_from("#ruma:example.com:443") RoomAliasId::try_from("#ruma:example.com:443")
.expect("Failed to create RoomAliasId.") .expect("Failed to create RoomAliasId.")
.to_string(), .as_ref(),
"#ruma:example.com" "#ruma:example.com:443"
); );
} }
@ -153,7 +113,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomAliasId::try_from("#ruma:example.com:5000") RoomAliasId::try_from("#ruma:example.com:5000")
.expect("Failed to create RoomAliasId.") .expect("Failed to create RoomAliasId.")
.to_string(), .as_ref(),
"#ruma:example.com:5000" "#ruma:example.com:5000"
); );
} }
@ -163,7 +123,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomAliasId::try_from("#老虎£я:example.com") RoomAliasId::try_from("#老虎£я:example.com")
.expect("Failed to create RoomAliasId.") .expect("Failed to create RoomAliasId.")
.to_string(), .as_ref(),
"#老虎£я:example.com" "#老虎£я:example.com"
); );
} }
@ -184,7 +144,7 @@ mod tests {
); );
} }
#[test] /*#[test]
fn invalid_room_alias_id_host() { fn invalid_room_alias_id_host() {
assert_eq!( assert_eq!(
RoomAliasId::try_from("#ruma:/").unwrap_err(), RoomAliasId::try_from("#ruma:/").unwrap_err(),
@ -198,5 +158,5 @@ mod tests {
RoomAliasId::try_from("#ruma:example.com:notaport").unwrap_err(), RoomAliasId::try_from("#ruma:example.com:notaport").unwrap_err(),
Error::InvalidHost Error::InvalidHost
); );
} }*/
} }

View File

@ -1,16 +1,11 @@
//! Matrix room identifiers. //! Matrix room identifiers.
use std::{ use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
use diesel::sql_types::Text; 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}; use crate::{error::Error, generate_localpart, parse_id};
/// A Matrix room ID. /// A Matrix room ID.
/// ///
@ -21,20 +16,16 @@ use crate::{deserialize_id, display, error::Error, generate_localpart, parse_id}
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use ruma_identifiers::RoomId; /// # use ruma_identifiers::RoomId;
/// assert_eq!( /// assert_eq!(
/// RoomId::try_from("!n8f893n9:example.com").unwrap().to_string(), /// RoomId::try_from("!n8f893n9:example.com").unwrap().as_ref(),
/// "!n8f893n9:example.com" /// "!n8f893n9:example.com"
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")] #[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct RoomId { pub struct RoomId {
/// The hostname of the homeserver. full_id: String,
hostname: Host, colon_idx: NonZeroU8,
/// The room's unique ID.
localpart: String,
/// The network port of the homeserver.
port: u16,
} }
impl RoomId { impl RoomId {
@ -43,77 +34,45 @@ impl RoomId {
/// ///
/// Fails if the given homeserver cannot be parsed as a valid host. /// Fails if the given homeserver cannot be parsed as a valid host.
pub fn new(homeserver_host: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let room_id = format!("!{}:{}", generate_localpart(18), homeserver_host); let full_id = format!("!{}:{}", generate_localpart(18), homeserver_host);
let (localpart, host, port) = parse_id('!', &room_id)?;
Ok(Self { Ok(Self {
hostname: host, full_id,
localpart: localpart.to_string(), colon_idx: NonZeroU8::new(19).unwrap(),
port,
}) })
} }
/// Returns a `Host` for the room ID, containing the server name (minus the port) of the /// Returns the host of the room ID, containing the server name (including the port) of the
/// originating homeserver. /// originating homeserver.
/// pub fn hostname(&self) -> &str {
/// The host can be either a domain name, an IPv4 address, or an IPv6 address. &self.full_id[self.colon_idx.get() as usize + 1..]
pub fn hostname(&self) -> &Host {
&self.hostname
} }
/// Returns the rooms's unique ID. /// Returns the rooms's unique ID.
pub fn localpart(&self) -> &str { pub fn localpart(&self) -> &str {
&self.localpart &self.full_id[1..self.colon_idx.get() as usize]
}
/// Returns the port the originating homeserver can be accessed on.
pub fn port(&self) -> u16 {
self.port
} }
} }
impl Display for RoomId { impl TryFrom<Cow<'_, str>> for RoomId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
display(f, '!', &self.localpart, &self.hostname, self.port)
}
}
impl Serialize for RoomId {
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 RoomId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_id(deserializer, "a Matrix room ID as a string")
}
}
impl TryFrom<&str> for RoomId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix room ID from a string representation. /// Attempts to create a new Matrix room ID from a string representation.
/// ///
/// The string must include the leading ! sigil, the localpart, a literal colon, and a valid /// The string must include the leading ! sigil, the localpart, a literal colon, and a server
/// server name. /// name.
fn try_from(room_id: &str) -> Result<Self, Error> { fn try_from(room_id: Cow<'_, str>) -> Result<Self, Error> {
let (localpart, host, port) = parse_id('!', room_id)?; let colon_idx = parse_id(&room_id, &['!'])?;
Ok(Self { Ok(Self {
hostname: host, full_id: room_id.into_owned(),
localpart: localpart.to_owned(), colon_idx,
port,
}) })
} }
} }
common_impls!(RoomId, "a Matrix room ID");
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -128,25 +87,24 @@ mod tests {
assert_eq!( assert_eq!(
RoomId::try_from("!29fhd83h92h0:example.com") RoomId::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomId.") .expect("Failed to create RoomId.")
.to_string(), .as_ref(),
"!29fhd83h92h0:example.com" "!29fhd83h92h0:example.com"
); );
} }
#[test] #[test]
fn generate_random_valid_room_id() { fn generate_random_valid_room_id() {
let room_id = RoomId::new("example.com") let room_id = RoomId::new("example.com").expect("Failed to generate RoomId.");
.expect("Failed to generate RoomId.") let id_str: &str = room_id.as_ref();
.to_string();
assert!(room_id.to_string().starts_with('!')); assert!(id_str.starts_with('!'));
assert_eq!(room_id.len(), 31); assert_eq!(id_str.len(), 31);
} }
#[test] /*#[test]
fn generate_random_invalid_room_id() { fn generate_random_invalid_room_id() {
assert!(RoomId::new("").is_err()); assert!(RoomId::new("").is_err());
} }*/
#[test] #[test]
fn serialize_valid_room_id() { fn serialize_valid_room_id() {
@ -173,8 +131,8 @@ mod tests {
assert_eq!( assert_eq!(
RoomId::try_from("!29fhd83h92h0:example.com:443") RoomId::try_from("!29fhd83h92h0:example.com:443")
.expect("Failed to create RoomId.") .expect("Failed to create RoomId.")
.to_string(), .as_ref(),
"!29fhd83h92h0:example.com" "!29fhd83h92h0:example.com:443"
); );
} }
@ -183,7 +141,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomId::try_from("!29fhd83h92h0:example.com:5000") RoomId::try_from("!29fhd83h92h0:example.com:5000")
.expect("Failed to create RoomId.") .expect("Failed to create RoomId.")
.to_string(), .as_ref(),
"!29fhd83h92h0:example.com:5000" "!29fhd83h92h0:example.com:5000"
); );
} }
@ -204,7 +162,7 @@ mod tests {
); );
} }
#[test] /*#[test]
fn invalid_room_id_host() { fn invalid_room_id_host() {
assert_eq!( assert_eq!(
RoomId::try_from("!29fhd83h92h0:/").unwrap_err(), RoomId::try_from("!29fhd83h92h0:/").unwrap_err(),
@ -218,5 +176,5 @@ mod tests {
RoomId::try_from("!29fhd83h92h0:example.com:notaport").unwrap_err(), RoomId::try_from("!29fhd83h92h0:example.com:notaport").unwrap_err(),
Error::InvalidHost Error::InvalidHost
); );
} }*/
} }

View File

@ -1,17 +1,11 @@
//! Matrix identifiers for places where a room ID or room alias ID are used interchangeably. //! Matrix identifiers for places where a room ID or room alias ID are used interchangeably.
use std::{ use std::{borrow::Cow, convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8};
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
use diesel::sql_types::Text; use diesel::sql_types::Text;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::{ use crate::{error::Error, parse_id};
deserialize_id, display, error::Error, room_alias_id::RoomAliasId, room_id::RoomId, validate_id,
};
/// A Matrix room ID or a Matrix room alias ID. /// A Matrix room ID or a Matrix room alias ID.
/// ///
@ -23,73 +17,61 @@ use crate::{
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use ruma_identifiers::RoomIdOrAliasId; /// # use ruma_identifiers::RoomIdOrAliasId;
/// assert_eq!( /// assert_eq!(
/// RoomIdOrAliasId::try_from("#ruma:example.com").unwrap().to_string(), /// RoomIdOrAliasId::try_from("#ruma:example.com").unwrap().as_ref(),
/// "#ruma:example.com" /// "#ruma:example.com"
/// ); /// );
/// ///
/// assert_eq!( /// assert_eq!(
/// RoomIdOrAliasId::try_from("!n8f893n9:example.com").unwrap().to_string(), /// RoomIdOrAliasId::try_from("!n8f893n9:example.com").unwrap().as_ref(),
/// "!n8f893n9:example.com" /// "!n8f893n9:example.com"
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")] #[cfg_attr(feature = "diesel", sql_type = "Text")]
pub enum RoomIdOrAliasId { pub struct RoomIdOrAliasId {
/// A Matrix room alias ID. full_id: String,
RoomAliasId(RoomAliasId), colon_idx: NonZeroU8,
/// A Matrix room ID.
RoomId(RoomId),
} }
impl Display for RoomIdOrAliasId { impl RoomIdOrAliasId {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { /// Returns the host of the room (alias) ID, containing the server name (including the port) of
match *self { /// the originating homeserver.
RoomIdOrAliasId::RoomAliasId(ref room_alias_id) => display( pub fn hostname(&self) -> &str {
f, &self.full_id[self.colon_idx.get() as usize + 1..]
'#', }
room_alias_id.alias(),
room_alias_id.hostname(), /// Returns the local part (everything after the `!` or `#` and before the first colon).
room_alias_id.port(), pub fn localpart(&self) -> &str {
), &self.full_id[1..self.colon_idx.get() as usize]
RoomIdOrAliasId::RoomId(ref room_id) => display( }
f,
'!', /// Whether this is a room id (starts with `'!'`)
room_id.localpart(), pub fn is_room_id(&self) -> bool {
room_id.hostname(), self.variant() == Variant::RoomId
room_id.port(), }
),
/// Whether this is a room alias id (starts with `'#'`)
pub fn is_room_alias_id(&self) -> bool {
self.variant() == Variant::RoomAliasId
}
fn variant(&self) -> Variant {
match self.full_id.bytes().next() {
Some(b'!') => Variant::RoomId,
Some(b'#') => Variant::RoomAliasId,
_ => unsafe { unreachable_unchecked() },
} }
} }
} }
impl Serialize for RoomIdOrAliasId { #[derive(PartialEq)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> enum Variant {
where RoomId,
S: Serializer, RoomAliasId,
{
match *self {
RoomIdOrAliasId::RoomAliasId(ref room_alias_id) => {
serializer.serialize_str(&room_alias_id.to_string())
}
RoomIdOrAliasId::RoomId(ref room_id) => serializer.serialize_str(&room_id.to_string()),
}
}
} }
impl<'de> Deserialize<'de> for RoomIdOrAliasId { impl TryFrom<Cow<'_, str>> for RoomIdOrAliasId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_id(
deserializer,
"a Matrix room ID or room alias ID as a string",
)
}
}
impl TryFrom<&str> for RoomIdOrAliasId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix room ID or a room alias ID from a string representation. /// Attempts to create a new Matrix room ID or a room alias ID from a string representation.
@ -97,27 +79,17 @@ impl TryFrom<&str> for RoomIdOrAliasId {
/// The string must either include the leading ! sigil, the localpart, a literal colon, and a /// The string must either include the leading ! sigil, the localpart, a literal colon, and a
/// valid homeserver host or include the leading # sigil, the alias, a literal colon, and a /// valid homeserver host or include the leading # sigil, the alias, a literal colon, and a
/// valid homeserver host. /// valid homeserver host.
fn try_from(room_id_or_alias_id: &str) -> Result<Self, Error> { fn try_from(room_id_or_alias_id: Cow<'_, str>) -> Result<Self, Error> {
validate_id(room_id_or_alias_id)?; let colon_idx = parse_id(&room_id_or_alias_id, &['#', '!'])?;
Ok(Self {
let mut chars = room_id_or_alias_id.chars(); full_id: room_id_or_alias_id.into_owned(),
colon_idx,
let sigil = chars.next().expect("ID missing first character."); })
match sigil {
'#' => {
let room_alias_id = RoomAliasId::try_from(room_id_or_alias_id)?;
Ok(RoomIdOrAliasId::RoomAliasId(room_alias_id))
}
'!' => {
let room_id = RoomId::try_from(room_id_or_alias_id)?;
Ok(RoomIdOrAliasId::RoomId(room_id))
}
_ => Err(Error::MissingSigil),
}
} }
} }
common_impls!(RoomIdOrAliasId, "a Matrix room ID or room alias ID");
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -132,7 +104,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomIdOrAliasId::try_from("#ruma:example.com") RoomIdOrAliasId::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.") .expect("Failed to create RoomAliasId.")
.to_string(), .as_ref(),
"#ruma:example.com" "#ruma:example.com"
); );
} }
@ -142,7 +114,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomIdOrAliasId::try_from("!29fhd83h92h0:example.com") RoomIdOrAliasId::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomId.") .expect("Failed to create RoomId.")
.to_string(), .as_ref(),
"!29fhd83h92h0:example.com" "!29fhd83h92h0:example.com"
); );
} }

View File

@ -1,8 +1,9 @@
//! Matrix room version identifiers. //! Matrix room version identifiers.
use std::{ use std::{
borrow::Cow,
convert::TryFrom, convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult}, fmt::{self, Display, Formatter},
}; };
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
@ -22,7 +23,7 @@ const MAX_CODE_POINTS: usize = 32;
/// ``` /// ```
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use ruma_identifiers::RoomVersionId; /// # use ruma_identifiers::RoomVersionId;
/// assert_eq!(RoomVersionId::try_from("1").unwrap().to_string(), "1"); /// assert_eq!(RoomVersionId::try_from("1").unwrap().as_ref(), "1");
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
@ -31,7 +32,7 @@ pub struct RoomVersionId(InnerRoomVersionId);
/// Possibile values for room version, distinguishing between official Matrix versions and custom /// Possibile values for room version, distinguishing between official Matrix versions and custom
/// versions. /// versions.
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum InnerRoomVersionId { enum InnerRoomVersionId {
/// A version 1 room. /// A version 1 room.
Version1, Version1,
@ -79,8 +80,8 @@ impl RoomVersionId {
} }
/// Creates a custom room version ID from the given string slice. /// Creates a custom room version ID from the given string slice.
pub fn custom(id: &str) -> Self { pub fn custom(id: String) -> Self {
Self(InnerRoomVersionId::Custom(id.to_string())) Self(InnerRoomVersionId::Custom(id))
} }
/// Whether or not this room version is an official one specified by the Matrix protocol. /// Whether or not this room version is an official one specified by the Matrix protocol.
@ -122,18 +123,35 @@ impl RoomVersionId {
} }
} }
impl Display for RoomVersionId { impl From<RoomVersionId> for String {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn from(id: RoomVersionId) -> Self {
let message = match self.0 { match id.0 {
InnerRoomVersionId::Version1 => "1".to_owned(),
InnerRoomVersionId::Version2 => "2".to_owned(),
InnerRoomVersionId::Version3 => "3".to_owned(),
InnerRoomVersionId::Version4 => "4".to_owned(),
InnerRoomVersionId::Version5 => "5".to_owned(),
InnerRoomVersionId::Custom(version) => version,
}
}
}
impl AsRef<str> for RoomVersionId {
fn as_ref(&self) -> &str {
match &self.0 {
InnerRoomVersionId::Version1 => "1", InnerRoomVersionId::Version1 => "1",
InnerRoomVersionId::Version2 => "2", InnerRoomVersionId::Version2 => "2",
InnerRoomVersionId::Version3 => "3", InnerRoomVersionId::Version3 => "3",
InnerRoomVersionId::Version4 => "4", InnerRoomVersionId::Version4 => "4",
InnerRoomVersionId::Version5 => "5", InnerRoomVersionId::Version5 => "5",
InnerRoomVersionId::Custom(ref version) => version, InnerRoomVersionId::Custom(version) => version,
}; }
}
}
write!(f, "{}", message) impl Display for RoomVersionId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_ref())
} }
} }
@ -142,7 +160,7 @@ impl Serialize for RoomVersionId {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_str(&self.to_string()) serializer.serialize_str(self.as_ref())
} }
} }
@ -155,12 +173,12 @@ impl<'de> Deserialize<'de> for RoomVersionId {
} }
} }
impl TryFrom<&str> for RoomVersionId { impl TryFrom<Cow<'_, str>> for RoomVersionId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix room version ID from a string representation. /// Attempts to create a new Matrix room version ID from a string representation.
fn try_from(room_version_id: &str) -> Result<Self, Error> { fn try_from(room_version_id: Cow<'_, str>) -> Result<Self, Error> {
let version = match room_version_id { let version = match &room_version_id as &str {
"1" => Self(InnerRoomVersionId::Version1), "1" => Self(InnerRoomVersionId::Version1),
"2" => Self(InnerRoomVersionId::Version2), "2" => Self(InnerRoomVersionId::Version2),
"3" => Self(InnerRoomVersionId::Version3), "3" => Self(InnerRoomVersionId::Version3),
@ -172,7 +190,7 @@ impl TryFrom<&str> for RoomVersionId {
} else if custom.chars().count() > MAX_CODE_POINTS { } else if custom.chars().count() > MAX_CODE_POINTS {
return Err(Error::MaximumLengthExceeded); return Err(Error::MaximumLengthExceeded);
} else { } else {
Self(InnerRoomVersionId::Custom(custom.to_string())) Self(InnerRoomVersionId::Custom(room_version_id.into_owned()))
} }
} }
}; };
@ -181,6 +199,22 @@ impl TryFrom<&str> for RoomVersionId {
} }
} }
impl TryFrom<&str> for RoomVersionId {
type Error = crate::error::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::try_from(Cow::Borrowed(s))
}
}
impl TryFrom<String> for RoomVersionId {
type Error = crate::error::Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::try_from(Cow::Owned(s))
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -195,7 +229,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("1") RoomVersionId::try_from("1")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"1" "1"
); );
} }
@ -205,7 +239,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("2") RoomVersionId::try_from("2")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"2" "2"
); );
} }
@ -215,7 +249,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("3") RoomVersionId::try_from("3")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"3" "3"
); );
} }
@ -225,7 +259,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("4") RoomVersionId::try_from("4")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"4" "4"
); );
} }
@ -235,7 +269,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("5") RoomVersionId::try_from("5")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"5" "5"
); );
} }
@ -245,7 +279,7 @@ mod tests {
assert_eq!( assert_eq!(
RoomVersionId::try_from("io.ruma.1") RoomVersionId::try_from("io.ruma.1")
.expect("Failed to create RoomVersionId.") .expect("Failed to create RoomVersionId.")
.to_string(), .as_ref(),
"io.ruma.1" "io.ruma.1"
); );
} }
@ -320,7 +354,7 @@ mod tests {
assert!(RoomVersionId::version_3().is_version_3()); assert!(RoomVersionId::version_3().is_version_3());
assert!(RoomVersionId::version_4().is_version_4()); assert!(RoomVersionId::version_4().is_version_4());
assert!(RoomVersionId::version_5().is_version_5()); assert!(RoomVersionId::version_5().is_version_5());
assert!(RoomVersionId::custom("foo").is_custom()); assert!(RoomVersionId::custom("foo".into()).is_custom());
} }
#[test] #[test]

View File

@ -1,16 +1,11 @@
//! Matrix user identifiers. //! Matrix user identifiers.
use std::{ use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
convert::TryFrom,
fmt::{Display, Formatter, Result as FmtResult},
};
#[cfg(feature = "diesel")] #[cfg(feature = "diesel")]
use diesel::sql_types::Text; 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}; use crate::{error::Error, generate_localpart, parse_id};
/// A Matrix user ID. /// A Matrix user ID.
/// ///
@ -21,20 +16,16 @@ use crate::{deserialize_id, display, error::Error, generate_localpart, parse_id}
/// # use std::convert::TryFrom; /// # use std::convert::TryFrom;
/// # use ruma_identifiers::UserId; /// # use ruma_identifiers::UserId;
/// assert_eq!( /// assert_eq!(
/// UserId::try_from("@carl:example.com").unwrap().to_string(), /// UserId::try_from("@carl:example.com").unwrap().as_ref(),
/// "@carl:example.com" /// "@carl:example.com"
/// ); /// );
/// ``` /// ```
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug)]
#[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))]
#[cfg_attr(feature = "diesel", sql_type = "Text")] #[cfg_attr(feature = "diesel", sql_type = "Text")]
pub struct UserId { pub struct UserId {
/// The hostname of the homeserver. full_id: String,
hostname: Host, colon_idx: NonZeroU8,
/// The user's unique ID.
localpart: String,
/// The network port of the homeserver.
port: u16,
/// Whether this user id is a historical one. /// Whether this user id is a historical one.
/// ///
/// A historical user id is one that is not legal per the regular user id rules, but was /// A historical user id is one that is not legal per the regular user id rules, but was
@ -49,37 +40,29 @@ impl UserId {
/// ///
/// Fails if the given homeserver cannot be parsed as a valid host. /// Fails if the given homeserver cannot be parsed as a valid host.
pub fn new(homeserver_host: &str) -> Result<Self, Error> { pub fn new(homeserver_host: &str) -> Result<Self, Error> {
let user_id = format!( let full_id = format!(
"@{}:{}", "@{}:{}",
generate_localpart(12).to_lowercase(), generate_localpart(12).to_lowercase(),
homeserver_host homeserver_host
); );
let (localpart, host, port) = parse_id('@', &user_id)?; let colon_idx = parse_id(&full_id, &['@'])?;
Ok(Self { Ok(Self {
hostname: host, full_id,
localpart: localpart.to_string(), colon_idx,
port,
is_historical: false, is_historical: false,
}) })
} }
/// Returns a `Host` for the user ID, containing the server name (minus the port) of the /// Returns the host of the user ID, containing the server name (including the port) of the
/// originating homeserver. /// originating homeserver.
/// pub fn hostname(&self) -> &str {
/// The host can be either a domain name, an IPv4 address, or an IPv6 address. &self.full_id[self.colon_idx.get() as usize + 1..]
pub fn hostname(&self) -> &Host {
&self.hostname
} }
/// Returns the user's localpart. /// Returns the user's localpart.
pub fn localpart(&self) -> &str { pub fn localpart(&self) -> &str {
&self.localpart &self.full_id[1..self.colon_idx.get() as usize]
}
/// Returns the port the originating homeserver can be accessed on.
pub fn port(&self) -> u16 {
self.port
} }
/// Whether this user ID is a historical one, i.e. one that doesn't conform to the latest /// Whether this user ID is a historical one, i.e. one that doesn't conform to the latest
@ -90,39 +73,16 @@ impl UserId {
} }
} }
impl Display for UserId { impl TryFrom<Cow<'_, str>> 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 TryFrom<&str> for UserId {
type Error = Error; type Error = Error;
/// Attempts to create a new Matrix user ID from a string representation. /// Attempts to create a new Matrix user ID from a string representation.
/// ///
/// The string must include the leading @ sigil, the localpart, a literal colon, and a valid /// The string must include the leading @ sigil, the localpart, a literal colon, and a server
/// server name. /// name.
fn try_from(user_id: &str) -> Result<Self, Error> { fn try_from(user_id: Cow<'_, str>) -> Result<Self, Error> {
let (localpart, host, port) = parse_id('@', user_id)?; let colon_idx = parse_id(&user_id, &['@'])?;
let localpart = &user_id[1..colon_idx.get() as usize];
// See https://matrix.org/docs/spec/appendices#user-identifiers // See https://matrix.org/docs/spec/appendices#user-identifiers
let is_fully_conforming = localpart.bytes().all(|b| match b { let is_fully_conforming = localpart.bytes().all(|b| match b {
@ -138,14 +98,15 @@ impl TryFrom<&str> for UserId {
} }
Ok(Self { Ok(Self {
hostname: host, full_id: user_id.into_owned(),
port, colon_idx,
localpart: localpart.to_owned(),
is_historical: !is_fully_conforming, is_historical: !is_fully_conforming,
}) })
} }
} }
common_impls!(UserId, "a Matrix user ID");
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;
@ -158,32 +119,31 @@ mod tests {
#[test] #[test]
fn valid_user_id() { fn valid_user_id() {
let user_id = UserId::try_from("@carl:example.com").expect("Failed to create UserId."); let user_id = UserId::try_from("@carl:example.com").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@carl:example.com"); assert_eq!(user_id.as_ref(), "@carl:example.com");
assert!(!user_id.is_historical()); assert!(!user_id.is_historical());
} }
#[test] #[test]
fn valid_historical_user_id() { fn valid_historical_user_id() {
let user_id = UserId::try_from("@a%b[irc]:example.com").expect("Failed to create UserId."); 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_eq!(user_id.as_ref(), "@a%b[irc]:example.com");
assert!(user_id.is_historical()); assert!(user_id.is_historical());
} }
#[test] #[test]
fn downcase_user_id() { fn downcase_user_id() {
let user_id = UserId::try_from("@CARL:example.com").expect("Failed to create UserId."); let user_id = UserId::try_from("@CARL:example.com").expect("Failed to create UserId.");
assert_eq!(user_id.to_string(), "@CARL:example.com"); assert_eq!(user_id.as_ref(), "@CARL:example.com");
assert!(user_id.is_historical()); assert!(user_id.is_historical());
} }
#[test] #[test]
fn generate_random_valid_user_id() { fn generate_random_valid_user_id() {
let user_id = UserId::new("example.com") let user_id = UserId::new("example.com").expect("Failed to generate UserId.");
.expect("Failed to generate UserId.") let id_str: &str = user_id.as_ref();
.to_string();
assert!(user_id.to_string().starts_with('@')); assert!(id_str.starts_with('@'));
assert_eq!(user_id.len(), 25); assert_eq!(id_str.len(), 25);
} }
#[test] #[test]
@ -213,15 +173,15 @@ mod tests {
assert_eq!( assert_eq!(
UserId::try_from("@carl:example.com:443") UserId::try_from("@carl:example.com:443")
.expect("Failed to create UserId.") .expect("Failed to create UserId.")
.to_string(), .as_ref(),
"@carl:example.com" "@carl:example.com:443"
); );
} }
#[test] #[test]
fn valid_user_id_with_non_standard_port() { fn valid_user_id_with_non_standard_port() {
let user_id = UserId::try_from("@carl:example.com:5000").expect("Failed to create UserId."); 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_eq!(user_id.as_ref(), "@carl:example.com:5000");
assert!(!user_id.is_historical()); assert!(!user_id.is_historical());
} }
@ -249,7 +209,7 @@ mod tests {
); );
} }
#[test] /*#[test]
fn invalid_user_id_host() { fn invalid_user_id_host() {
assert_eq!(UserId::try_from("@carl:/").unwrap_err(), Error::InvalidHost); assert_eq!(UserId::try_from("@carl:/").unwrap_err(), Error::InvalidHost);
} }
@ -260,5 +220,5 @@ mod tests {
UserId::try_from("@carl:example.com:notaport").unwrap_err(), UserId::try_from("@carl:example.com:notaport").unwrap_err(),
Error::InvalidHost Error::InvalidHost
); );
} }*/
} }