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 | Ok(u) => u, |
19 | Err(_) => { |
20 | // here triggers const_err |
21 | // const_panic requires 1.57 |
22 | #[allow(unconditional_panic)] |
23 | let _ = ["invalid uuid representation" ][1]; |
24 | |
25 | loop {} // -> never type |
26 | } |
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 | /// |
61 | /// ## Compilation Failures |
62 | /// |
63 | /// Invalid UUIDs are rejected: |
64 | /// |
65 | /// ```compile_fail |
66 | /// # use uuid::uuid; |
67 | /// let uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); |
68 | /// ``` |
69 | /// |
70 | /// Enable the feature `macro-diagnostics` to see the error messages below. |
71 | /// |
72 | /// Provides the following compilation error: |
73 | /// |
74 | /// ```txt |
75 | /// error: invalid character: expected an optional prefix of `urn:uuid:` followed by [0-9a-fA-F-], found Z at 9 |
76 | /// | |
77 | /// | let id = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); |
78 | /// | ^ |
79 | /// ``` |
80 | /// |
81 | /// Tokens that aren't string literals are also rejected: |
82 | /// |
83 | /// ```compile_fail |
84 | /// # use uuid::uuid; |
85 | /// let uuid_str: &str = "550e8400e29b41d4a716446655440000"; |
86 | /// let uuid = uuid!(uuid_str); |
87 | /// ``` |
88 | /// |
89 | /// Provides the following compilation error: |
90 | /// |
91 | /// ```txt |
92 | /// error: expected string literal |
93 | /// | |
94 | /// | let uuid = uuid!(uuid_str); |
95 | /// | ^^^^^^^^ |
96 | /// ``` |
97 | /// |
98 | /// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html |
99 | } |
100 | |