Update how TryFrom implementation are generated
… and remove TryFrom<Cow<'_, str>> implementations
This commit is contained in:
parent
5861457f3b
commit
7b30c2bb3e
@ -4,6 +4,10 @@ Breaking changes
|
|||||||
|
|
||||||
* Removed diesel integration. If you were using it, please comment on the corresponding issue:
|
* Removed diesel integration. If you were using it, please comment on the corresponding issue:
|
||||||
https://github.com/ruma/ruma-identifiers/issues/22
|
https://github.com/ruma/ruma-identifiers/issues/22
|
||||||
|
* Remove `TryFrom<Cow<'_, str>>` implementations for identifier types
|
||||||
|
* Update `parse_with_server_name`s signature (instead of `Into<String>` it now requires
|
||||||
|
`Into<Box<str>>` of the id type). This is technically a breaking change, but extremely unlikely
|
||||||
|
to affect any existing code.
|
||||||
|
|
||||||
# 0.16.1
|
# 0.16.1
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Matrix event identifiers.
|
//! Matrix event identifiers.
|
||||||
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{error::Error, parse_id, validate_id};
|
use crate::{error::Error, parse_id, validate_id};
|
||||||
|
|
||||||
@ -87,34 +87,32 @@ impl EventId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Cow<'_, str>> for EventId {
|
/// Attempts to create a new Matrix event ID from a string representation.
|
||||||
type Error = Error;
|
///
|
||||||
|
/// If using the original event format as used by Matrix room versions 1 and 2, the string must
|
||||||
|
/// include the leading $ sigil, the localpart, a literal colon, and a valid homeserver hostname.
|
||||||
|
fn try_from<S>(event_id: S) -> Result<EventId, Error>
|
||||||
|
where
|
||||||
|
S: AsRef<str> + Into<Box<str>>,
|
||||||
|
{
|
||||||
|
if event_id.as_ref().contains(':') {
|
||||||
|
let colon_idx = parse_id(event_id.as_ref(), &['$'])?;
|
||||||
|
|
||||||
/// Attempts to create a new Matrix event ID from a string representation.
|
Ok(EventId {
|
||||||
///
|
full_id: event_id.into(),
|
||||||
/// If using the original event format as used by Matrix room versions 1 and 2, the string must
|
colon_idx: Some(colon_idx),
|
||||||
/// include the leading $ sigil, the localpart, a literal colon, and a valid homeserver
|
})
|
||||||
/// hostname.
|
} else {
|
||||||
fn try_from(event_id: Cow<'_, str>) -> Result<Self, Self::Error> {
|
validate_id(event_id.as_ref(), &['$'])?;
|
||||||
if event_id.contains(':') {
|
|
||||||
let colon_idx = parse_id(&event_id, &['$'])?;
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(EventId {
|
||||||
full_id: event_id.into_owned().into(),
|
full_id: event_id.into(),
|
||||||
colon_idx: Some(colon_idx),
|
colon_idx: None,
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
validate_id(&event_id, &['$'])?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
full_id: event_id.into_owned().into(),
|
|
||||||
colon_idx: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
common_impls!(EventId, "a Matrix event ID");
|
common_impls!(EventId, try_from, "a Matrix event ID");
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
macro_rules! common_impls {
|
macro_rules! common_impls {
|
||||||
($id:ident, $desc:literal) => {
|
($id:ident, $try_from:ident, $desc:literal) => {
|
||||||
impl ::std::convert::From<$id> for ::std::string::String {
|
impl ::std::convert::From<$id> for ::std::string::String {
|
||||||
fn from(id: $id) -> Self {
|
fn from(id: $id) -> Self {
|
||||||
id.full_id.into()
|
id.full_id.into()
|
||||||
@ -10,7 +10,7 @@ macro_rules! common_impls {
|
|||||||
type Error = crate::error::Error;
|
type Error = crate::error::Error;
|
||||||
|
|
||||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||||
Self::try_from(::std::borrow::Cow::Borrowed(s))
|
$try_from(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ macro_rules! common_impls {
|
|||||||
type Error = crate::error::Error;
|
type Error = crate::error::Error;
|
||||||
|
|
||||||
fn try_from(s: String) -> Result<Self, Self::Error> {
|
fn try_from(s: String) -> Result<Self, Self::Error> {
|
||||||
Self::try_from(::std::borrow::Cow::Owned(s))
|
$try_from(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Matrix room alias identifiers.
|
//! Matrix room alias identifiers.
|
||||||
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{error::Error, parse_id};
|
use crate::{error::Error, parse_id};
|
||||||
|
|
||||||
@ -35,23 +35,22 @@ impl RoomAliasId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Cow<'_, str>> for RoomAliasId {
|
/// Attempts to create a new Matrix room alias ID from a string representation.
|
||||||
type Error = Error;
|
///
|
||||||
|
/// The string must include the leading # sigil, the alias, a literal colon, and a server name.
|
||||||
|
fn try_from<S>(room_id: S) -> Result<RoomAliasId, Error>
|
||||||
|
where
|
||||||
|
S: AsRef<str> + Into<Box<str>>,
|
||||||
|
{
|
||||||
|
let colon_idx = parse_id(room_id.as_ref(), &['#'])?;
|
||||||
|
|
||||||
/// Attempts to create a new Matrix room alias ID from a string representation.
|
Ok(RoomAliasId {
|
||||||
///
|
full_id: room_id.into(),
|
||||||
/// The string must include the leading # sigil, the alias, a literal colon, and a server name.
|
colon_idx,
|
||||||
fn try_from(room_id: Cow<'_, str>) -> Result<Self, Error> {
|
})
|
||||||
let colon_idx = parse_id(&room_id, &['#'])?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
full_id: room_id.into_owned().into(),
|
|
||||||
colon_idx,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
common_impls!(RoomAliasId, "a Matrix room alias ID");
|
common_impls!(RoomAliasId, try_from, "a Matrix room alias ID");
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Matrix room identifiers.
|
//! Matrix room identifiers.
|
||||||
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{error::Error, parse_id};
|
use crate::{error::Error, parse_id};
|
||||||
|
|
||||||
@ -55,24 +55,22 @@ impl RoomId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Cow<'_, str>> for RoomId {
|
/// Attempts to create a new Matrix room ID from a string representation.
|
||||||
type Error = Error;
|
///
|
||||||
|
/// The string must include the leading ! sigil, the localpart, a literal colon, and a server name.
|
||||||
|
fn try_from<S>(room_id: S) -> Result<RoomId, Error>
|
||||||
|
where
|
||||||
|
S: AsRef<str> + Into<Box<str>>,
|
||||||
|
{
|
||||||
|
let colon_idx = parse_id(room_id.as_ref(), &['!'])?;
|
||||||
|
|
||||||
/// Attempts to create a new Matrix room ID from a string representation.
|
Ok(RoomId {
|
||||||
///
|
full_id: room_id.into(),
|
||||||
/// The string must include the leading ! sigil, the localpart, a literal colon, and a server
|
colon_idx,
|
||||||
/// name.
|
})
|
||||||
fn try_from(room_id: Cow<'_, str>) -> Result<Self, Error> {
|
|
||||||
let colon_idx = parse_id(&room_id, &['!'])?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
full_id: room_id.into_owned().into(),
|
|
||||||
colon_idx,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
common_impls!(RoomId, "a Matrix room ID");
|
common_impls!(RoomId, try_from, "a Matrix room ID");
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Matrix identifiers for places where a room ID or room alias ID are used interchangeably.
|
//! Matrix identifiers for places where a room ID or room alias ID are used interchangeably.
|
||||||
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8};
|
use std::{convert::TryFrom, hint::unreachable_unchecked, num::NonZeroU8};
|
||||||
|
|
||||||
use crate::{error::Error, parse_id, RoomAliasId, RoomId};
|
use crate::{error::Error, parse_id, RoomAliasId, RoomId};
|
||||||
|
|
||||||
@ -81,24 +81,27 @@ enum Variant {
|
|||||||
RoomAliasId,
|
RoomAliasId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Cow<'_, str>> for RoomIdOrAliasId {
|
/// Attempts to create a new Matrix room ID or a room alias ID from a string representation.
|
||||||
type Error = Error;
|
///
|
||||||
|
/// The string must either include the leading ! sigil, the localpart, a literal colon, and a
|
||||||
/// Attempts to create a new Matrix room ID or a room alias ID from a string representation.
|
/// valid homeserver host or include the leading # sigil, the alias, a literal colon, and a
|
||||||
///
|
/// valid homeserver host.
|
||||||
/// The string must either include the leading ! sigil, the localpart, a literal colon, and a
|
fn try_from<S>(room_id_or_alias_id: S) -> Result<RoomIdOrAliasId, Error>
|
||||||
/// valid homeserver host or include the leading # sigil, the alias, a literal colon, and a
|
where
|
||||||
/// valid homeserver host.
|
S: AsRef<str> + Into<Box<str>>,
|
||||||
fn try_from(room_id_or_alias_id: Cow<'_, str>) -> Result<Self, Error> {
|
{
|
||||||
let colon_idx = parse_id(&room_id_or_alias_id, &['#', '!'])?;
|
let colon_idx = parse_id(room_id_or_alias_id.as_ref(), &['#', '!'])?;
|
||||||
Ok(Self {
|
Ok(RoomIdOrAliasId {
|
||||||
full_id: room_id_or_alias_id.into_owned().into(),
|
full_id: room_id_or_alias_id.into(),
|
||||||
colon_idx,
|
colon_idx,
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
common_impls!(RoomIdOrAliasId, "a Matrix room ID or room alias ID");
|
common_impls!(
|
||||||
|
RoomIdOrAliasId,
|
||||||
|
try_from,
|
||||||
|
"a Matrix room ID or room alias ID"
|
||||||
|
);
|
||||||
|
|
||||||
impl From<RoomId> for RoomIdOrAliasId {
|
impl From<RoomId> for RoomIdOrAliasId {
|
||||||
fn from(RoomId { full_id, colon_idx }: RoomId) -> Self {
|
fn from(RoomId { full_id, colon_idx }: RoomId) -> Self {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Matrix user identifiers.
|
//! Matrix user identifiers.
|
||||||
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, num::NonZeroU8};
|
use std::num::NonZeroU8;
|
||||||
|
|
||||||
use crate::{error::Error, is_valid_server_name, parse_id};
|
use crate::{error::Error, is_valid_server_name, parse_id};
|
||||||
|
|
||||||
@ -59,13 +59,13 @@ impl UserId {
|
|||||||
/// localpart, not the localpart plus the `@` prefix, or the localpart plus server name without
|
/// localpart, not the localpart plus the `@` prefix, or the localpart plus server name without
|
||||||
/// the `@` prefix.
|
/// the `@` prefix.
|
||||||
pub fn parse_with_server_name(
|
pub fn parse_with_server_name(
|
||||||
id: impl AsRef<str> + Into<String>,
|
id: impl AsRef<str> + Into<Box<str>>,
|
||||||
server_name: &str,
|
server_name: &str,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
let id_str = id.as_ref();
|
let id_str = id.as_ref();
|
||||||
|
|
||||||
if id_str.starts_with('@') {
|
if id_str.starts_with('@') {
|
||||||
Self::try_from(id.into())
|
try_from(id.into())
|
||||||
} else {
|
} else {
|
||||||
let is_fully_conforming = localpart_is_fully_comforming(id_str)?;
|
let is_fully_conforming = localpart_is_fully_comforming(id_str)?;
|
||||||
if !is_valid_server_name(server_name) {
|
if !is_valid_server_name(server_name) {
|
||||||
@ -98,28 +98,26 @@ impl UserId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Cow<'_, str>> for UserId {
|
/// Attempts to create a new Matrix user ID from a string representation.
|
||||||
type Error = Error;
|
///
|
||||||
|
/// The string must include the leading @ sigil, the localpart, a literal colon, and a server name.
|
||||||
|
fn try_from<S>(user_id: S) -> Result<UserId, Error>
|
||||||
|
where
|
||||||
|
S: AsRef<str> + Into<Box<str>>,
|
||||||
|
{
|
||||||
|
let colon_idx = parse_id(user_id.as_ref(), &['@'])?;
|
||||||
|
let localpart = &user_id.as_ref()[1..colon_idx.get() as usize];
|
||||||
|
|
||||||
/// Attempts to create a new Matrix user ID from a string representation.
|
let is_historical = localpart_is_fully_comforming(localpart)?;
|
||||||
///
|
|
||||||
/// The string must include the leading @ sigil, the localpart, a literal colon, and a server
|
|
||||||
/// name.
|
|
||||||
fn try_from(user_id: Cow<'_, str>) -> Result<Self, Error> {
|
|
||||||
let colon_idx = parse_id(&user_id, &['@'])?;
|
|
||||||
let localpart = &user_id[1..colon_idx.get() as usize];
|
|
||||||
|
|
||||||
let is_historical = localpart_is_fully_comforming(localpart)?;
|
Ok(UserId {
|
||||||
|
full_id: user_id.into(),
|
||||||
Ok(Self {
|
colon_idx,
|
||||||
full_id: user_id.into_owned().into(),
|
is_historical: !is_historical,
|
||||||
colon_idx,
|
})
|
||||||
is_historical: !is_historical,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
common_impls!(UserId, "a Matrix user ID");
|
common_impls!(UserId, try_from, "a Matrix user ID");
|
||||||
|
|
||||||
/// Check whether the given user id localpart is valid and fully conforming
|
/// Check whether the given user id localpart is valid and fully conforming
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user