identifiers: Add Rc
/ Arc
parsing constructors
This commit is contained in:
parent
6324034f03
commit
bba7d62442
@ -217,6 +217,26 @@ macro_rules! opaque_identifier_validated {
|
|||||||
Ok($id::from_owned(s.into()))
|
Ok($id::from_owned(s.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc_concat! {
|
||||||
|
#[doc = concat!("Try parsing a `&str` into an `Rc<", stringify!($id), ">`.")]
|
||||||
|
pub fn parse_rc(
|
||||||
|
s: impl AsRef<str> + Into<std::rc::Rc<str>>,
|
||||||
|
) -> Result<std::rc::Rc<Self>, crate::Error> {
|
||||||
|
$validate_id(s.as_ref())?;
|
||||||
|
Ok($id::from_rc(s.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
doc_concat! {
|
||||||
|
#[doc = concat!("Try parsing a `&str` into an `Arc<", stringify!($id), ">`.")]
|
||||||
|
pub fn parse_arc(
|
||||||
|
s: impl AsRef<str> + Into<std::sync::Arc<str>>,
|
||||||
|
) -> Result<std::sync::Arc<Self>, crate::Error> {
|
||||||
|
$validate_id(s.as_ref())?;
|
||||||
|
Ok($id::from_arc(s.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opaque_identifier_common_impls!($id);
|
opaque_identifier_common_impls!($id);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
//! Matrix user identifiers.
|
//! Matrix user identifiers.
|
||||||
|
|
||||||
|
use std::{rc::Rc, sync::Arc};
|
||||||
|
|
||||||
use crate::{MatrixToRef, ServerName};
|
use crate::{MatrixToRef, ServerName};
|
||||||
|
|
||||||
/// A Matrix user ID.
|
/// A Matrix user ID.
|
||||||
@ -51,6 +53,38 @@ impl UserId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Variation of [`parse_with_server_name`] that returns `Rc<Self>`.
|
||||||
|
///
|
||||||
|
/// [`parse_with_server_name`]: Self::parse_with_server_name
|
||||||
|
pub fn parse_with_server_name_rc(
|
||||||
|
id: impl AsRef<str> + Into<Rc<str>>,
|
||||||
|
server_name: &ServerName,
|
||||||
|
) -> Result<Rc<Self>, crate::Error> {
|
||||||
|
let id_str = id.as_ref();
|
||||||
|
|
||||||
|
if id_str.starts_with('@') {
|
||||||
|
Self::parse_rc(id)
|
||||||
|
} else {
|
||||||
|
Ok(Self::from_rc(format!("@{}:{}", id_str, server_name).into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Variation of [`parse_with_server_name`] that returns `Arc<Self>`.
|
||||||
|
///
|
||||||
|
/// [`parse_with_server_name`]: Self::parse_with_server_name
|
||||||
|
pub fn parse_with_server_name_arc(
|
||||||
|
id: impl AsRef<str> + Into<Arc<str>>,
|
||||||
|
server_name: &ServerName,
|
||||||
|
) -> Result<Arc<Self>, crate::Error> {
|
||||||
|
let id_str = id.as_ref();
|
||||||
|
|
||||||
|
if id_str.starts_with('@') {
|
||||||
|
Self::parse_arc(id)
|
||||||
|
} else {
|
||||||
|
Ok(Self::from_arc(format!("@{}:{}", id_str, server_name).into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the user's localpart.
|
/// Returns the user's localpart.
|
||||||
pub fn localpart(&self) -> &str {
|
pub fn localpart(&self) -> &str {
|
||||||
&self.as_str()[1..self.colon_idx() as usize]
|
&self.as_str()[1..self.colon_idx() as usize]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user