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