identifiers: Rename RoomIdOrAliasId to RoomOrAliasId

This commit is contained in:
Jonas Platte 2021-11-26 01:07:34 +01:00
parent 6b76d7813e
commit 8923881678
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
7 changed files with 36 additions and 37 deletions

View File

@ -1,7 +1,7 @@
//! [POST /_matrix/client/r0/knock/{roomIdOrAlias}](https://spec.matrix.org/unstable/client-server-api/#post_matrixclientr0knockroomidoralias)
use ruma_api::ruma_api;
use ruma_identifiers::{RoomId, RoomIdOrAliasId, ServerName};
use ruma_identifiers::{RoomId, RoomOrAliasId, ServerName};
ruma_api! {
metadata: {
@ -16,7 +16,7 @@ ruma_api! {
request: {
/// The room the user should knock on.
#[ruma_api(path)]
pub room_id_or_alias: &'a RoomIdOrAliasId,
pub room_id_or_alias: &'a RoomOrAliasId,
/// The reason for joining a room.
#[serde(skip_serializing_if = "Option::is_none")]
@ -38,7 +38,7 @@ ruma_api! {
impl<'a> Request<'a> {
/// Creates a new `Request` with the given room ID or alias.
pub fn new(room_id_or_alias: &'a RoomIdOrAliasId) -> Self {
pub fn new(room_id_or_alias: &'a RoomOrAliasId) -> Self {
Self { room_id_or_alias, reason: None, server_name: &[] }
}
}

View File

@ -1,7 +1,7 @@
//! [POST /_matrix/client/r0/join/{roomIdOrAlias}](https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-join-roomidoralias)
use ruma_api::ruma_api;
use ruma_identifiers::{RoomId, RoomIdOrAliasId, ServerName};
use ruma_identifiers::{RoomId, RoomOrAliasId, ServerName};
use super::{IncomingThirdPartySigned, ThirdPartySigned};
@ -18,7 +18,7 @@ ruma_api! {
request: {
/// The room where the user should be invited.
#[ruma_api(path)]
pub room_id_or_alias: &'a RoomIdOrAliasId,
pub room_id_or_alias: &'a RoomOrAliasId,
/// The servers to attempt to join the room through.
///
@ -48,7 +48,7 @@ ruma_api! {
impl<'a> Request<'a> {
/// Creates a new `Request` with the given room ID or alias ID.
pub fn new(room_id_or_alias: &'a RoomIdOrAliasId) -> Self {
pub fn new(room_id_or_alias: &'a RoomOrAliasId) -> Self {
Self {
room_id_or_alias,
server_name: &[],

View File

@ -10,6 +10,7 @@ Breaking changes:
* The corresponding macros (and also `server_name!`) now return `'static` references instead of
owned values now use `.to_owned()` to get an owned copy
* Rename `RoomVersionId::Version{X}` variants to `RoomVersionId::V{X}`
* Rename `RoomIdOrAliasId` to `RoomOrAliasId`
# 0.20.0

View File

@ -32,8 +32,8 @@ pub use crate::{
mxc_uri::MxcUri,
room_alias_id::RoomAliasId,
room_id::RoomId,
room_id_or_room_alias_id::RoomIdOrAliasId,
room_name::RoomName,
room_or_room_alias_id::RoomOrAliasId,
room_version_id::RoomVersionId,
server_name::ServerName,
session_id::SessionId,
@ -59,8 +59,8 @@ mod matrix_to;
mod mxc_uri;
mod room_alias_id;
mod room_id;
mod room_id_or_room_alias_id;
mod room_name;
mod room_or_room_alias_id;
mod room_version_id;
mod server_name;
mod session_id;

View File

@ -9,33 +9,33 @@ use crate::{server_name::ServerName, RoomAliasId, RoomId};
/// A Matrix room ID or a Matrix room alias ID.
///
/// `RoomIdOrAliasId` is useful for APIs that accept either kind of room identifier. It is converted
/// `RoomOrAliasId` is useful for APIs that accept either kind of room identifier. It is converted
/// from a string slice, and can be converted back into a string as needed. When converted from a
/// string slice, the variant is determined by the leading sigil character.
///
/// ```
/// # use std::convert::TryFrom;
/// # use ruma_identifiers::RoomIdOrAliasId;
/// # use ruma_identifiers::RoomOrAliasId;
/// assert_eq!(
/// <&RoomIdOrAliasId>::try_from("#ruma:example.com").unwrap(),
/// <&RoomOrAliasId>::try_from("#ruma:example.com").unwrap(),
/// "#ruma:example.com"
/// );
///
/// assert_eq!(
/// <&RoomIdOrAliasId>::try_from("!n8f893n9:example.com").unwrap(),
/// <&RoomOrAliasId>::try_from("!n8f893n9:example.com").unwrap(),
/// "!n8f893n9:example.com"
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoomIdOrAliasId(str);
pub struct RoomOrAliasId(str);
opaque_identifier_validated!(
RoomIdOrAliasId,
RoomOrAliasId,
ruma_identifiers_validation::room_id_or_alias_id::validate
);
impl RoomIdOrAliasId {
impl RoomOrAliasId {
/// Returns the local part (everything after the `!` or `#` and before the first colon).
pub fn localpart(&self) -> &str {
&self.as_str()[1..self.colon_idx()]
@ -56,7 +56,7 @@ impl RoomIdOrAliasId {
self.variant() == Variant::RoomAliasId
}
/// Turn this `RoomIdOrAliasId` into `Either<RoomId, RoomAliasId>`
/// Turn this `RoomOrAliasId` into `Either<RoomId, RoomAliasId>`
#[cfg(feature = "either")]
pub fn into_either(self: Box<Self>) -> either::Either<Box<RoomId>, Box<RoomAliasId>> {
let variant = self.variant();
@ -87,22 +87,22 @@ enum Variant {
RoomAliasId,
}
impl From<Box<RoomId>> for Box<RoomIdOrAliasId> {
impl From<Box<RoomId>> for Box<RoomOrAliasId> {
fn from(room_id: Box<RoomId>) -> Self {
Self::try_from(room_id.as_str()).unwrap()
}
}
impl From<Box<RoomAliasId>> for Box<RoomIdOrAliasId> {
impl From<Box<RoomAliasId>> for Box<RoomOrAliasId> {
fn from(room_alias_id: Box<RoomAliasId>) -> Self {
Self::try_from(room_alias_id.as_str()).unwrap()
}
}
impl TryFrom<Box<RoomIdOrAliasId>> for Box<RoomId> {
impl TryFrom<Box<RoomOrAliasId>> for Box<RoomId> {
type Error = Box<RoomAliasId>;
fn try_from(id: Box<RoomIdOrAliasId>) -> Result<Box<RoomId>, Box<RoomAliasId>> {
fn try_from(id: Box<RoomOrAliasId>) -> Result<Box<RoomId>, Box<RoomAliasId>> {
match id.variant() {
Variant::RoomId => Ok(id.as_str().try_into().unwrap()),
Variant::RoomAliasId => Err(id.as_str().try_into().unwrap()),
@ -110,10 +110,10 @@ impl TryFrom<Box<RoomIdOrAliasId>> for Box<RoomId> {
}
}
impl TryFrom<Box<RoomIdOrAliasId>> for Box<RoomAliasId> {
impl TryFrom<Box<RoomOrAliasId>> for Box<RoomAliasId> {
type Error = Box<RoomId>;
fn try_from(id: Box<RoomIdOrAliasId>) -> Result<Box<RoomAliasId>, Box<RoomId>> {
fn try_from(id: Box<RoomOrAliasId>) -> Result<Box<RoomAliasId>, Box<RoomId>> {
match id.variant() {
Variant::RoomAliasId => Ok(id.as_str().try_into().unwrap()),
Variant::RoomId => Err(id.as_str().try_into().unwrap()),
@ -125,13 +125,13 @@ impl TryFrom<Box<RoomIdOrAliasId>> for Box<RoomAliasId> {
mod tests {
use std::convert::TryFrom;
use super::RoomIdOrAliasId;
use super::RoomOrAliasId;
use crate::Error;
#[test]
fn valid_room_id_or_alias_id_with_a_room_alias_id() {
assert_eq!(
<&RoomIdOrAliasId>::try_from("#ruma:example.com")
<&RoomOrAliasId>::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.")
.as_ref(),
"#ruma:example.com"
@ -141,7 +141,7 @@ mod tests {
#[test]
fn valid_room_id_or_alias_id_with_a_room_id() {
assert_eq!(
<&RoomIdOrAliasId>::try_from("!29fhd83h92h0:example.com")
<&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomId.")
.as_ref(),
"!29fhd83h92h0:example.com"
@ -151,7 +151,7 @@ mod tests {
#[test]
fn missing_sigil_for_room_id_or_alias_id() {
assert_eq!(
<&RoomIdOrAliasId>::try_from("ruma:example.com").unwrap_err(),
<&RoomOrAliasId>::try_from("ruma:example.com").unwrap_err(),
Error::MissingLeadingSigil
);
}
@ -161,7 +161,7 @@ mod tests {
fn serialize_valid_room_id_or_alias_id_with_a_room_alias_id() {
assert_eq!(
serde_json::to_string(
<&RoomIdOrAliasId>::try_from("#ruma:example.com")
<&RoomOrAliasId>::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.")
)
.expect("Failed to convert RoomAliasId to JSON."),
@ -174,7 +174,7 @@ mod tests {
fn serialize_valid_room_id_or_alias_id_with_a_room_id() {
assert_eq!(
serde_json::to_string(
<&RoomIdOrAliasId>::try_from("!29fhd83h92h0:example.com")
<&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomId.")
)
.expect("Failed to convert RoomId to JSON."),
@ -186,10 +186,9 @@ mod tests {
#[test]
fn deserialize_valid_room_id_or_alias_id_with_a_room_alias_id() {
assert_eq!(
serde_json::from_str::<Box<RoomIdOrAliasId>>(r##""#ruma:example.com""##)
serde_json::from_str::<Box<RoomOrAliasId>>(r##""#ruma:example.com""##)
.expect("Failed to convert JSON to RoomAliasId"),
<&RoomIdOrAliasId>::try_from("#ruma:example.com")
.expect("Failed to create RoomAliasId.")
<&RoomOrAliasId>::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.")
);
}
@ -197,9 +196,9 @@ mod tests {
#[test]
fn deserialize_valid_room_id_or_alias_id_with_a_room_id() {
assert_eq!(
serde_json::from_str::<Box<RoomIdOrAliasId>>(r##""!29fhd83h92h0:example.com""##)
serde_json::from_str::<Box<RoomOrAliasId>>(r##""!29fhd83h92h0:example.com""##)
.expect("Failed to convert JSON to RoomId"),
<&RoomIdOrAliasId>::try_from("!29fhd83h92h0:example.com")
<&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomAliasId.")
);
}

View File

@ -271,7 +271,7 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
|| last_seg.ident == "RawJsonValue"
|| last_seg.ident == "RoomAliasId"
|| last_seg.ident == "RoomId"
|| last_seg.ident == "RoomIdOrAliasId"
|| last_seg.ident == "RoomOrAliasId"
|| last_seg.ident == "RoomName"
|| last_seg.ident == "ServerSigningKeyId"
|| last_seg.ident == "SigningKeyId"

View File

@ -85,9 +85,8 @@ pub use ruma_identifiers::{
device_id, device_key_id, event_id, mxc_uri, room_alias_id, room_id, room_version_id,
server_name, server_signing_key_id, user_id, ClientSecret, DeviceId, DeviceKeyAlgorithm,
DeviceKeyId, DeviceSignatures, DeviceSigningKeyId, EntitySignatures, EventEncryptionAlgorithm,
EventId, KeyId, KeyName, MxcUri, RoomAliasId, RoomId, RoomIdOrAliasId, RoomVersionId,
ServerName, ServerSignatures, ServerSigningKeyId, SessionId, Signatures, SigningKeyAlgorithm,
UserId,
EventId, KeyId, KeyName, MxcUri, RoomAliasId, RoomId, RoomOrAliasId, RoomVersionId, ServerName,
ServerSignatures, ServerSigningKeyId, SessionId, Signatures, SigningKeyAlgorithm, UserId,
};
#[cfg(feature = "client")]