1 macro_rules! define_uuid_macro { 2 {$(#[$doc:meta])*} => { 3 $(#[$doc])* 4 #[cfg(feature = "macro-diagnostics")] 5 #[macro_export] 6 macro_rules! uuid { 7 ($uuid:expr) => {{ 8 const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) { 9 $crate::__macro_support::Ok(u) => u, 10 $crate::__macro_support::Err(_) => panic!("invalid UUID"), 11 }; 12 OUTPUT 13 }}; 14 ($uuid:literal) => {{ 15 $crate::Uuid::from_bytes($crate::uuid_macro_internal::parse_lit!($uuid)) 16 }}; 17 } 18 19 $(#[$doc])* 20 #[cfg(not(feature = "macro-diagnostics"))] 21 #[macro_export] 22 macro_rules! uuid { 23 ($uuid:expr) => {{ 24 const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) { 25 $crate::__macro_support::Ok(u) => u, 26 $crate::__macro_support::Err(_) => panic!("invalid UUID"), 27 }; 28 OUTPUT 29 }}; 30 } 31 } 32 } 33 34 define_uuid_macro! { 35 /// Parse [`Uuid`][uuid::Uuid]s from string literals at compile time. 36 /// 37 /// ## Usage 38 /// 39 /// This macro transforms the string literal representation of a 40 /// [`Uuid`][uuid::Uuid] into the bytes representation, raising a compilation 41 /// error if it cannot properly be parsed. 42 /// 43 /// ## Examples 44 /// 45 /// Setting a global constant: 46 /// 47 /// ``` 48 /// # use uuid::{uuid, Uuid}; 49 /// pub const SCHEMA_ATTR_CLASS: Uuid = uuid!("00000000-0000-0000-0000-ffff00000000"); 50 /// pub const SCHEMA_ATTR_UUID: Uuid = uuid!("00000000-0000-0000-0000-ffff00000001"); 51 /// pub const SCHEMA_ATTR_NAME: Uuid = uuid!("00000000-0000-0000-0000-ffff00000002"); 52 /// ``` 53 /// 54 /// Defining a local variable: 55 /// 56 /// ``` 57 /// # use uuid::uuid; 58 /// let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"); 59 /// ``` 60 /// Using a const variable: 61 /// ``` 62 /// # use uuid::uuid; 63 /// const UUID_STR: &str = "12345678-1234-5678-1234-567812345678"; 64 /// let UUID = uuid!(UUID_STR); 65 /// ``` 66 /// 67 /// ## Compilation Failures 68 /// 69 /// Invalid UUIDs are rejected: 70 /// 71 /// ```compile_fail 72 /// # use uuid::uuid; 73 /// let uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); 74 /// ``` 75 /// 76 /// Enable the feature `macro-diagnostics` to see the error messages below. 77 /// 78 /// Provides the following compilation error: 79 /// 80 /// ```txt 81 /// error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found Z at 9 82 /// | 83 /// | let id = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); 84 /// | ^ 85 /// ``` 86 /// 87 /// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html 88 } 89