Add RoomIdOrAliasId::into_either

This commit is contained in:
Jonas Platte 2020-04-20 15:41:58 +02:00
parent 6e6a51e11a
commit e21633ef53
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 19 additions and 0 deletions

View File

@ -13,6 +13,8 @@ Improvements:
* `impl From<RoomAliasId> for RoomIdOrAliasId`
* `impl TryFrom<RoomIdOrAliasId> for RoomId`
* `impl TryFrom<RoomIdOrAliasId> for RoomAliasId`
* `RoomIdOrAliasId::into_either` (if the optional dependency `either` is activated with the
identically named feature)
# 0.15.1

View File

@ -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 }

View File

@ -55,6 +55,22 @@ impl RoomIdOrAliasId {
self.variant() == Variant::RoomAliasId
}
/// Turn this `RoomIdOrAliasId` into `Either<RoomId, RoomAliasId>`
#[cfg(feature = "either")]
#[cfg_attr(docsrs, doc(cfg(feature = "either")))]
pub fn into_either(self) -> either::Either<RoomId, RoomAliasId> {
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,