identifiers: Add session_id macro
This commit is contained in:
parent
bf670fb814
commit
cb96caa064
@ -161,6 +161,19 @@ macro_rules! server_name {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compile-time checked `SessionId` construction.
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! session_id {
|
||||||
|
($s:literal) => {{
|
||||||
|
const SESSION_ID: &$crate::SessionId = match $crate::SessionId::_priv_const_new($s) {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(_) => panic!("Invalid session ID"),
|
||||||
|
};
|
||||||
|
|
||||||
|
SESSION_ID
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
/// Compile-time checked `MxcUri` construction.
|
/// Compile-time checked `MxcUri` construction.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! mxc_uri {
|
macro_rules! mxc_uri {
|
||||||
|
@ -13,6 +13,16 @@ use super::IdParseError;
|
|||||||
#[ruma_id(validate = validate_session_id)]
|
#[ruma_id(validate = validate_session_id)]
|
||||||
pub struct SessionId(str);
|
pub struct SessionId(str);
|
||||||
|
|
||||||
|
impl SessionId {
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub const fn _priv_const_new(s: &str) -> Result<&Self, IdParseError> {
|
||||||
|
match validate_session_id(s) {
|
||||||
|
Ok(()) => Ok(Self::from_borrowed(s)),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const fn validate_session_id(s: &str) -> Result<(), IdParseError> {
|
const fn validate_session_id(s: &str) -> Result<(), IdParseError> {
|
||||||
if s.len() > 255 {
|
if s.len() > 255 {
|
||||||
return Err(IdParseError::MaximumLengthExceeded);
|
return Err(IdParseError::MaximumLengthExceeded);
|
||||||
|
@ -3,4 +3,5 @@ fn ui() {
|
|||||||
let t = trybuild::TestCases::new();
|
let t = trybuild::TestCases::new();
|
||||||
t.pass("tests/identifiers/ui/01-valid-id-macros.rs");
|
t.pass("tests/identifiers/ui/01-valid-id-macros.rs");
|
||||||
t.compile_fail("tests/identifiers/ui/02-invalid-id-macros.rs");
|
t.compile_fail("tests/identifiers/ui/02-invalid-id-macros.rs");
|
||||||
|
t.compile_fail("tests/identifiers/ui/03-invalid-new-id-macros.rs");
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
let _ = ruma_common::session_id!("invalid~");
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
error[E0080]: evaluation of constant value failed
|
||||||
|
--> tests/identifiers/ui/03-invalid-new-id-macros.rs:2:13
|
||||||
|
|
|
||||||
|
2 | let _ = ruma_common::session_id!("invalid~");
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'Invalid session ID', $DIR/tests/identifiers/ui/03-invalid-new-id-macros.rs:2:13
|
||||||
|
|
|
||||||
|
= note: this error originates in the macro `$crate::panic::panic_2021` (in Nightly builds, run with -Z macro-backtrace for more info)
|
@ -78,7 +78,7 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result<TokenStream> {
|
|||||||
#owned_decl
|
#owned_decl
|
||||||
|
|
||||||
impl #impl_generics #id_ty {
|
impl #impl_generics #id_ty {
|
||||||
pub(super) fn from_borrowed(s: &str) -> &Self {
|
pub(super) const fn from_borrowed(s: &str) -> &Self {
|
||||||
unsafe { std::mem::transmute(s) }
|
unsafe { std::mem::transmute(s) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user