From 733cf3838f311d8628722bb18a89339f78a65aeb Mon Sep 17 00:00:00 2001 From: Quinn Okabayashi Date: Sun, 31 Oct 2021 22:13:42 -0400 Subject: [PATCH] Made `macros` module in `uuid` --- src/lib.rs | 6 ++++-- src/macros.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/macros.rs diff --git a/src/lib.rs b/src/lib.rs index ff41f71..ae6ca6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -233,8 +233,10 @@ mod v5; mod winapi_support; #[cfg(feature = "macros")] -#[doc(inline)] -pub use uuid_macros::*; +#[macro_use] +mod macros; +#[cfg(feature = "macros")] +pub extern crate uuid_macros; use crate::std::convert; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..f4380d4 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,53 @@ +/// Parse [`Uuid`][uuid::Uuid]s from string literals at compile time. +/// ## Usage +/// This macro transforms the string literal representation of a +/// [`Uuid`][uuid::Uuid] into the bytes representation, raising a compilation +/// error if it cannot properly be parsed. +/// +/// ## Examples +/// Setting a global constant: +/// ``` +/// # use uuid::{uuid, Uuid}; +/// pub const SCHEMA_ATTR_CLASS: Uuid = uuid!("00000000-0000-0000-0000-ffff00000000"); +/// pub const SCHEMA_ATTR_UUID: Uuid = uuid!("00000000-0000-0000-0000-ffff00000001"); +/// pub const SCHEMA_ATTR_NAME: Uuid = uuid!("00000000-0000-0000-0000-ffff00000002"); +/// ``` +/// Defining a local variable: +/// ``` +/// # use uuid::{uuid, Uuid}; +/// let uuid: Uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"); +/// ``` +/// ## Compilation Failures +/// Invalid UUIDs are rejected: +/// ```ignore +/// # use uuid::{uuid, Uuid}; +/// let uuid: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); +/// ``` +/// Provides the following compilation error: +/// ```txt +/// error: invalid character: expected an optional prefix of `urn:uuid:` followed by 0123456789abcdefABCDEF-, found Z at 9 +/// | +/// | let id: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); +/// | ^ +/// ``` +/// Tokens that aren't string literals are also rejected: +/// ```ignore +/// # use uuid::{uuid, Uuid}; +/// let uuid_str: &str = "550e8400e29b41d4a716446655440000"; +/// let uuid: Uuid = uuid!(uuid_str); +/// ``` +/// Provides the following compilation error: +/// ```txt +/// error: expected string literal +/// | +/// | let uuid: Uuid = uuid!(uuid_str); +/// | ^^^^^^^^ +/// ``` +/// +/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html +#[macro_export] +macro_rules! uuid { + ($uuid:tt) => {{ + $crate::Uuid::from_bytes($crate::uuid_macros::parse_lit!($uuid)) + }}; +}