From 7b30c2bb3ee93834387a4d60b8c086f0579d3805 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 30 May 2020 19:28:06 +0200 Subject: [PATCH] Update how TryFrom implementation are generated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and remove TryFrom> implementations --- CHANGELOG.md | 4 +++ src/event_id.rs | 46 ++++++++++++++++----------------- src/macros.rs | 6 ++--- src/room_alias_id.rs | 29 ++++++++++----------- src/room_id.rs | 30 ++++++++++----------- src/room_id_or_room_alias_id.rs | 37 ++++++++++++++------------ src/user_id.rs | 40 ++++++++++++++-------------- 7 files changed, 96 insertions(+), 96 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eb3c139..8ae312cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Breaking changes * Removed diesel integration. If you were using it, please comment on the corresponding issue: https://github.com/ruma/ruma-identifiers/issues/22 +* Remove `TryFrom>` implementations for identifier types +* Update `parse_with_server_name`s signature (instead of `Into` it now requires + `Into>` of the id type). This is technically a breaking change, but extremely unlikely + to affect any existing code. # 0.16.1 diff --git a/src/event_id.rs b/src/event_id.rs index 9cbfb259..c0c9c1f1 100644 --- a/src/event_id.rs +++ b/src/event_id.rs @@ -1,6 +1,6 @@ //! Matrix event identifiers. -use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8}; +use std::num::NonZeroU8; use crate::{error::Error, parse_id, validate_id}; @@ -87,34 +87,32 @@ impl EventId { } } -impl TryFrom> for EventId { - type Error = Error; +/// Attempts to create a new Matrix event ID from a string representation. +/// +/// 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 hostname. +fn try_from(event_id: S) -> Result +where + S: AsRef + Into>, +{ + if event_id.as_ref().contains(':') { + let colon_idx = parse_id(event_id.as_ref(), &['$'])?; - /// Attempts to create a new Matrix event ID from a string representation. - /// - /// 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 - /// hostname. - fn try_from(event_id: Cow<'_, str>) -> Result { - if event_id.contains(':') { - let colon_idx = parse_id(&event_id, &['$'])?; + Ok(EventId { + full_id: event_id.into(), + colon_idx: Some(colon_idx), + }) + } else { + validate_id(event_id.as_ref(), &['$'])?; - Ok(Self { - full_id: event_id.into_owned().into(), - colon_idx: Some(colon_idx), - }) - } else { - validate_id(&event_id, &['$'])?; - - Ok(Self { - full_id: event_id.into_owned().into(), - colon_idx: None, - }) - } + Ok(EventId { + full_id: event_id.into(), + colon_idx: None, + }) } } -common_impls!(EventId, "a Matrix event ID"); +common_impls!(EventId, try_from, "a Matrix event ID"); #[cfg(test)] mod tests { diff --git a/src/macros.rs b/src/macros.rs index 09cb7e8a..5ea40a4e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,5 @@ macro_rules! common_impls { - ($id:ident, $desc:literal) => { + ($id:ident, $try_from:ident, $desc:literal) => { impl ::std::convert::From<$id> for ::std::string::String { fn from(id: $id) -> Self { id.full_id.into() @@ -10,7 +10,7 @@ macro_rules! common_impls { type Error = crate::error::Error; fn try_from(s: &str) -> Result { - Self::try_from(::std::borrow::Cow::Borrowed(s)) + $try_from(s) } } @@ -18,7 +18,7 @@ macro_rules! common_impls { type Error = crate::error::Error; fn try_from(s: String) -> Result { - Self::try_from(::std::borrow::Cow::Owned(s)) + $try_from(s) } } diff --git a/src/room_alias_id.rs b/src/room_alias_id.rs index a6e873eb..b8ca6684 100644 --- a/src/room_alias_id.rs +++ b/src/room_alias_id.rs @@ -1,6 +1,6 @@ //! Matrix room alias identifiers. -use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8}; +use std::num::NonZeroU8; use crate::{error::Error, parse_id}; @@ -35,23 +35,22 @@ impl RoomAliasId { } } -impl TryFrom> for RoomAliasId { - type Error = Error; +/// 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 server name. +fn try_from(room_id: S) -> Result +where + S: AsRef + Into>, +{ + let colon_idx = parse_id(room_id.as_ref(), &['#'])?; - /// 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 server name. - fn try_from(room_id: Cow<'_, str>) -> Result { - let colon_idx = parse_id(&room_id, &['#'])?; - - Ok(Self { - full_id: room_id.into_owned().into(), - colon_idx, - }) - } + Ok(RoomAliasId { + full_id: room_id.into(), + colon_idx, + }) } -common_impls!(RoomAliasId, "a Matrix room alias ID"); +common_impls!(RoomAliasId, try_from, "a Matrix room alias ID"); #[cfg(test)] mod tests { diff --git a/src/room_id.rs b/src/room_id.rs index ed120543..4187cc9d 100644 --- a/src/room_id.rs +++ b/src/room_id.rs @@ -1,6 +1,6 @@ //! Matrix room identifiers. -use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8}; +use std::num::NonZeroU8; use crate::{error::Error, parse_id}; @@ -55,24 +55,22 @@ impl RoomId { } } -impl TryFrom> for RoomId { - type Error = Error; +/// 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 server name. +fn try_from(room_id: S) -> Result +where + S: AsRef + Into>, +{ + let colon_idx = parse_id(room_id.as_ref(), &['!'])?; - /// 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 server - /// name. - fn try_from(room_id: Cow<'_, str>) -> Result { - let colon_idx = parse_id(&room_id, &['!'])?; - - Ok(Self { - full_id: room_id.into_owned().into(), - colon_idx, - }) - } + Ok(RoomId { + full_id: room_id.into(), + colon_idx, + }) } -common_impls!(RoomId, "a Matrix room ID"); +common_impls!(RoomId, try_from, "a Matrix room ID"); #[cfg(test)] mod tests { diff --git a/src/room_id_or_room_alias_id.rs b/src/room_id_or_room_alias_id.rs index 82787e6b..00018c82 100644 --- a/src/room_id_or_room_alias_id.rs +++ b/src/room_id_or_room_alias_id.rs @@ -1,6 +1,6 @@ //! Matrix identifiers for places where a room ID or room alias ID are used interchangeably. -use std::{borrow::Cow, convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8}; +use std::{convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8}; use crate::{error::Error, parse_id, RoomAliasId, RoomId}; @@ -81,24 +81,27 @@ enum Variant { RoomAliasId, } -impl TryFrom> for RoomIdOrAliasId { - type Error = Error; - - /// Attempts to create a new Matrix room ID or a room alias ID from a string representation. - /// - /// 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. - fn try_from(room_id_or_alias_id: Cow<'_, str>) -> Result { - let colon_idx = parse_id(&room_id_or_alias_id, &['#', '!'])?; - Ok(Self { - full_id: room_id_or_alias_id.into_owned().into(), - colon_idx, - }) - } +/// Attempts to create a new Matrix room ID or a room alias ID from a string representation. +/// +/// 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. +fn try_from(room_id_or_alias_id: S) -> Result +where + S: AsRef + Into>, +{ + let colon_idx = parse_id(room_id_or_alias_id.as_ref(), &['#', '!'])?; + Ok(RoomIdOrAliasId { + full_id: room_id_or_alias_id.into(), + colon_idx, + }) } -common_impls!(RoomIdOrAliasId, "a Matrix room ID or room alias ID"); +common_impls!( + RoomIdOrAliasId, + try_from, + "a Matrix room ID or room alias ID" +); impl From for RoomIdOrAliasId { fn from(RoomId { full_id, colon_idx }: RoomId) -> Self { diff --git a/src/user_id.rs b/src/user_id.rs index e34e6627..5bb8047c 100644 --- a/src/user_id.rs +++ b/src/user_id.rs @@ -1,6 +1,6 @@ //! Matrix user identifiers. -use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8}; +use std::num::NonZeroU8; use crate::{error::Error, is_valid_server_name, parse_id}; @@ -59,13 +59,13 @@ impl UserId { /// localpart, not the localpart plus the `@` prefix, or the localpart plus server name without /// the `@` prefix. pub fn parse_with_server_name( - id: impl AsRef + Into, + id: impl AsRef + Into>, server_name: &str, ) -> Result { let id_str = id.as_ref(); if id_str.starts_with('@') { - Self::try_from(id.into()) + try_from(id.into()) } else { let is_fully_conforming = localpart_is_fully_comforming(id_str)?; if !is_valid_server_name(server_name) { @@ -98,28 +98,26 @@ impl UserId { } } -impl TryFrom> for UserId { - type Error = Error; +/// 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 server name. +fn try_from(user_id: S) -> Result +where + S: AsRef + Into>, +{ + let colon_idx = parse_id(user_id.as_ref(), &['@'])?; + let localpart = &user_id.as_ref()[1..colon_idx.get() as usize]; - /// 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 server - /// name. - fn try_from(user_id: Cow<'_, str>) -> Result { - let colon_idx = parse_id(&user_id, &['@'])?; - let localpart = &user_id[1..colon_idx.get() as usize]; + let is_historical = localpart_is_fully_comforming(localpart)?; - let is_historical = localpart_is_fully_comforming(localpart)?; - - Ok(Self { - full_id: user_id.into_owned().into(), - colon_idx, - is_historical: !is_historical, - }) - } + Ok(UserId { + full_id: user_id.into(), + colon_idx, + is_historical: !is_historical, + }) } -common_impls!(UserId, "a Matrix user ID"); +common_impls!(UserId, try_from, "a Matrix user ID"); /// Check whether the given user id localpart is valid and fully conforming ///