diff --git a/Cargo.toml b/Cargo.toml index 709d803e..f17c9012 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ repository = "uuid-rs/uuid" [features] default = ["std"] std = [] -macros = ["uuid_macro"] +macro-diagnostics = ["uuid_macro"] v1 = ["atomic"] v3 = ["md-5"] diff --git a/README.md b/README.md index 3d0f52a3..834c8bec 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,10 @@ let my_uuid = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8")?; assert_eq!(Some(Version::Random), my_uuid.get_version()); ``` -If you add the `macros` feature then you can parse UUIDs at compile time -instead of at runtime: +You can parse UUIDs at compile time instead of at runtime. + +If you add the `macro-diagnostics` feature then you can see much better +error messages. ```rust #[macro_use] @@ -78,7 +80,7 @@ various pieces of functionality: generate a `Uuid`. * `v5` - adds the `Uuid::new_v5` function and the ability to create a V5 UUID based on the SHA1 hash of some data. -* `macros` - adds the `uuid!` macro that can parse UUIDs at compile time. +* `macro-diagnostics` - enhances the diagnostics of `uuid!` macro. * `serde` - adds the ability to serialize and deserialize a `Uuid` using the `serde` crate. * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid`. diff --git a/examples/uuid_macro.rs b/examples/uuid_macro.rs index 9336a09d..c4ff0484 100644 --- a/examples/uuid_macro.rs +++ b/examples/uuid_macro.rs @@ -1,12 +1,13 @@ //! Using the `uuid!` macro. //! -//! If you enable the `macros` feature you can use the `uuid!` macro. //! `uuid!` will parse encoded UUIDs at compile time instead of at runtime. //! If you've got a fixed UUID string handy then consider using `uuid!` instead //! of `Uuid::parse_str` or `str::parse`. +//! +//! If you enable the `macro-diagnostics` feature, you can see much better +//! error messages. #[test] -#[cfg(feature = "macros")] fn parse_uuid_at_compile_time() { use uuid::uuid; diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 22c6cea9..13237cde 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,11 +1,11 @@ //! Implementation details for the `uuid!` macro. //! //! This crate is not meant to be used directly. Instead, -//! you can use the `macros` feature of `uuid`: +//! you can use the `macro-diagnostics` feature of `uuid`: //! //! ```toml //! [dependencies.uuid] -//! features = ["macros"] +//! features = ["macro-diagnostics"] //! ``` use proc_macro::TokenStream; diff --git a/src/lib.rs b/src/lib.rs index bc7fc883..f5bd2472 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ //! //! Other crate features can also be useful beyond the version support: //! -//! * `macros` - adds the `uuid!` macro that can parse UUIDs from string literals at compile time. +//! * `macro-diagnostics` - enhances the diagnostics of `uuid!` macro. //! * `serde` - adds the ability to serialize and deserialize a UUID using //! `serde`. //! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for @@ -241,11 +241,11 @@ mod rng; mod external; -#[cfg(feature = "macros")] #[macro_use] mod macros; + #[doc(hidden)] -#[cfg(feature = "macros")] +#[cfg(feature = "macro-diagnostics")] pub extern crate uuid_macro; use crate::std::convert; diff --git a/src/macros.rs b/src/macros.rs index 4f7d1c41..8be62d8a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,3 +1,37 @@ +macro_rules! define_uuid_macro { + {$(#[$doc:meta])*} => { + $(#[$doc])* + #[cfg(feature = "macro-diagnostics")] + #[macro_export] + macro_rules! uuid { + ($uuid:literal) => {{ + $crate::Uuid::from_bytes($crate::uuid_macro::parse_lit!($uuid)) + }}; + } + + $(#[$doc])* + #[cfg(not(feature = "macro-diagnostics"))] + #[macro_export] + macro_rules! uuid { + ($uuid:literal) => {{ + const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) { + Ok(u) => u, + Err(_) => { + // here triggers const_err + // const_panic requires 1.57 + #[allow(unconditional_panic)] + let _ = ["invalid uuid representation"][1]; + + loop {} // -> never type + } + }; + OUTPUT + }}; + } + } +} + +define_uuid_macro! { /// Parse [`Uuid`][uuid::Uuid]s from string literals at compile time. /// /// ## Usage @@ -33,6 +67,8 @@ /// let uuid: Uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4"); /// ``` /// +/// Enable the feature `macro-diagnostics` to see the error messages below. +/// /// Provides the following compilation error: /// /// ```txt @@ -60,9 +96,4 @@ /// ``` /// /// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html -#[macro_export] -macro_rules! uuid { - ($uuid:tt) => {{ - $crate::Uuid::from_bytes($crate::uuid_macro::parse_lit!($uuid)) - }}; } diff --git a/tests/macros.rs b/tests/macros.rs index cdb8ee57..8748d96b 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "macros")] +#[cfg(feature = "macro-diagnostics")] #[test] fn ui() { let t = trybuild::TestCases::new();