Skip to content

Commit

Permalink
Merge pull request #764 from Vrajs16/main
Browse files Browse the repository at this point in the history
Use const identifier in uuid macro
  • Loading branch information
KodrAus committed Jul 8, 2024
2 parents 36e6f57 + 8896e26 commit 66b4fce
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ macro_rules! define_uuid_macro {
#[cfg(feature = "macro-diagnostics")]
#[macro_export]
macro_rules! uuid {
($uuid:expr) => {{
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
$crate::__macro_support::Ok(u) => u,
$crate::__macro_support::Err(_) => panic!("invalid UUID"),
};
OUTPUT
}};
($uuid:literal) => {{
$crate::Uuid::from_bytes($crate::uuid_macro_internal::parse_lit!($uuid))
}};
Expand All @@ -13,7 +20,7 @@ macro_rules! define_uuid_macro {
#[cfg(not(feature = "macro-diagnostics"))]
#[macro_export]
macro_rules! uuid {
($uuid:literal) => {{
($uuid:expr) => {{
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
$crate::__macro_support::Ok(u) => u,
$crate::__macro_support::Err(_) => panic!("invalid UUID"),
Expand Down Expand Up @@ -50,6 +57,12 @@ define_uuid_macro! {
/// # use uuid::uuid;
/// let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
/// ```
/// Using a const variable:
/// ```
/// # use uuid::uuid;
/// const UUID_STR: &str = "12345678-1234-5678-1234-567812345678";
/// let UUID = uuid!(UUID_STR);
/// ```
///
/// ## Compilation Failures
///
Expand All @@ -71,22 +84,5 @@ define_uuid_macro! {
/// | ^
/// ```
///
/// Tokens that aren't string literals are also rejected:
///
/// ```compile_fail
/// # use uuid::uuid;
/// let uuid_str: &str = "550e8400e29b41d4a716446655440000";
/// let uuid = uuid!(uuid_str);
/// ```
///
/// Provides the following compilation error:
///
/// ```txt
/// error: expected string literal
/// |
/// | let uuid = uuid!(uuid_str);
/// | ^^^^^^^^
/// ```
///
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
}

0 comments on commit 66b4fce

Please sign in to comment.