diff --git a/CHANGELOG.md b/CHANGELOG.md index f4210fb4..0fa4de8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Improvements: * `impl From for RoomIdOrAliasId` * `impl TryFrom for RoomId` * `impl TryFrom for RoomAliasId` + * `RoomIdOrAliasId::into_either` (if the optional dependency `either` is activated with the + identically named feature) # 0.15.1 diff --git a/Cargo.toml b/Cargo.toml index 39decbca..b46c2dd1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ default = ["serde"] [dependencies] diesel = { version = "1.4.4", optional = true } +either = { version = "1.5.3", optional = true } rand = { version = "0.7.3", optional = true } serde = { version = "1.0.106", optional = true } diff --git a/src/room_id_or_room_alias_id.rs b/src/room_id_or_room_alias_id.rs index fd02007f..f0be719e 100644 --- a/src/room_id_or_room_alias_id.rs +++ b/src/room_id_or_room_alias_id.rs @@ -55,6 +55,22 @@ impl RoomIdOrAliasId { self.variant() == Variant::RoomAliasId } + /// Turn this `RoomIdOrAliasId` into `Either` + #[cfg(feature = "either")] + #[cfg_attr(docsrs, doc(cfg(feature = "either")))] + pub fn into_either(self) -> either::Either { + match self.variant() { + Variant::RoomId => either::Either::Left(RoomId { + full_id: self.full_id, + colon_idx: self.colon_idx, + }), + Variant::RoomAliasId => either::Either::Right(RoomAliasId { + full_id: self.full_id, + colon_idx: self.colon_idx, + }), + } + } + fn variant(&self) -> Variant { match self.full_id.bytes().next() { Some(b'!') => Variant::RoomId,