Implement conversion functinos for RoomIdOrAliasId
This commit is contained in:
parent
05c9eac6ab
commit
6e6a51e11a
14
CHANGELOG.md
14
CHANGELOG.md
@ -1,5 +1,19 @@
|
||||
# [unreleased]
|
||||
|
||||
Breaking changes:
|
||||
|
||||
* Update `RoomId::parse_with_server_name`s bounds from `Into<Cow<'_, str>>` to
|
||||
`AsRef<str> + Into<String>`. While this is a breaking change, it is not expected to actually
|
||||
require code changes.
|
||||
|
||||
Improvements:
|
||||
|
||||
* Add conversion functions for `RoomIdOrAliasId`
|
||||
* `impl From<RoomId> for RoomIdOrAliasId`
|
||||
* `impl From<RoomAliasId> for RoomIdOrAliasId`
|
||||
* `impl TryFrom<RoomIdOrAliasId> for RoomId`
|
||||
* `impl TryFrom<RoomIdOrAliasId> for RoomAliasId`
|
||||
|
||||
# 0.15.1
|
||||
|
||||
Bug fixes:
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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<Cow<'_, str>> for RoomIdOrAliasId {
|
||||
|
||||
common_impls!(RoomIdOrAliasId, "a Matrix room ID or room alias ID");
|
||||
|
||||
impl From<RoomId> for RoomIdOrAliasId {
|
||||
fn from(RoomId { full_id, colon_idx }: RoomId) -> Self {
|
||||
Self { full_id, colon_idx }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RoomAliasId> for RoomIdOrAliasId {
|
||||
fn from(RoomAliasId { full_id, colon_idx }: RoomAliasId) -> Self {
|
||||
Self { full_id, colon_idx }
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<RoomIdOrAliasId> for RoomId {
|
||||
type Error = RoomAliasId;
|
||||
|
||||
fn try_from(id: RoomIdOrAliasId) -> Result<RoomId, RoomAliasId> {
|
||||
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<RoomIdOrAliasId> for RoomAliasId {
|
||||
type Error = RoomId;
|
||||
|
||||
fn try_from(id: RoomIdOrAliasId) -> Result<RoomAliasId, RoomId> {
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user