From 6e6a51e11a5bec7e82191f53dee58a3a22fce1db Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 20 Apr 2020 15:38:19 +0200 Subject: [PATCH] Implement conversion functinos for RoomIdOrAliasId --- CHANGELOG.md | 14 ++++++++++ src/room_alias_id.rs | 4 +-- src/room_id.rs | 4 +-- src/room_id_or_room_alias_id.rs | 48 ++++++++++++++++++++++++++++++++- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd0e901..f4210fb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # [unreleased] +Breaking changes: + +* Update `RoomId::parse_with_server_name`s bounds from `Into>` to + `AsRef + Into`. While this is a breaking change, it is not expected to actually + require code changes. + +Improvements: + +* Add conversion functions for `RoomIdOrAliasId` + * `impl From for RoomIdOrAliasId` + * `impl From for RoomIdOrAliasId` + * `impl TryFrom for RoomId` + * `impl TryFrom for RoomAliasId` + # 0.15.1 Bug fixes: diff --git a/src/room_alias_id.rs b/src/room_alias_id.rs index e612d655..0293bfb5 100644 --- a/src/room_alias_id.rs +++ b/src/room_alias_id.rs @@ -24,8 +24,8 @@ use crate::{error::Error, parse_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct RoomAliasId { - full_id: String, - colon_idx: NonZeroU8, + pub(crate) full_id: String, + pub(crate) colon_idx: NonZeroU8, } impl RoomAliasId { diff --git a/src/room_id.rs b/src/room_id.rs index 88fb82fe..fa5e8841 100644 --- a/src/room_id.rs +++ b/src/room_id.rs @@ -24,8 +24,8 @@ use crate::{error::Error, parse_id}; #[cfg_attr(feature = "diesel", derive(FromSqlRow, QueryId, AsExpression, SqlType))] #[cfg_attr(feature = "diesel", sql_type = "Text")] pub struct RoomId { - full_id: String, - colon_idx: NonZeroU8, + pub(crate) full_id: String, + pub(crate) colon_idx: NonZeroU8, } impl RoomId { diff --git a/src/room_id_or_room_alias_id.rs b/src/room_id_or_room_alias_id.rs index fe7ab364..fd02007f 100644 --- a/src/room_id_or_room_alias_id.rs +++ b/src/room_id_or_room_alias_id.rs @@ -5,7 +5,7 @@ use std::{borrow::Cow, convert::TryFrom, hint::unreachable_unchecked, num::NonZe #[cfg(feature = "diesel")] use diesel::sql_types::Text; -use crate::{error::Error, parse_id}; +use crate::{error::Error, parse_id, RoomAliasId, RoomId}; /// A Matrix room ID or a Matrix room alias ID. /// @@ -89,6 +89,52 @@ impl TryFrom> for RoomIdOrAliasId { common_impls!(RoomIdOrAliasId, "a Matrix room ID or room alias ID"); +impl From for RoomIdOrAliasId { + fn from(RoomId { full_id, colon_idx }: RoomId) -> Self { + Self { full_id, colon_idx } + } +} + +impl From for RoomIdOrAliasId { + fn from(RoomAliasId { full_id, colon_idx }: RoomAliasId) -> Self { + Self { full_id, colon_idx } + } +} + +impl TryFrom for RoomId { + type Error = RoomAliasId; + + fn try_from(id: RoomIdOrAliasId) -> Result { + match id.variant() { + Variant::RoomId => Ok(RoomId { + full_id: id.full_id, + colon_idx: id.colon_idx, + }), + Variant::RoomAliasId => Err(RoomAliasId { + full_id: id.full_id, + colon_idx: id.colon_idx, + }), + } + } +} + +impl TryFrom for RoomAliasId { + type Error = RoomId; + + fn try_from(id: RoomIdOrAliasId) -> Result { + match id.variant() { + Variant::RoomAliasId => Ok(RoomAliasId { + full_id: id.full_id, + colon_idx: id.colon_idx, + }), + Variant::RoomId => Err(RoomId { + full_id: id.full_id, + colon_idx: id.colon_idx, + }), + } + } +} + #[cfg(test)] mod tests { use std::convert::TryFrom;