From 6edf3e8cd59351589622daf1f2634870d90896e3 Mon Sep 17 00:00:00 2001 From: Vraj Shah Date: Sat, 29 Jun 2024 08:05:39 -0400 Subject: [PATCH] Use const identifer in uuid macro --- src/macros.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index eb957256..016666dd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -20,6 +20,13 @@ macro_rules! define_uuid_macro { }; OUTPUT }}; + ($uuid:ident) => {{ + const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) { + $crate::__macro_support::Ok(u) => u, + $crate::__macro_support::Err(_) => panic!("invalid UUID"), + }; + OUTPUT + }}; } } } @@ -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 /// @@ -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 }