Skip to content

Commit

Permalink
Use const identifer in uuid macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrajs16 committed Jun 29, 2024
1 parent 36e6f57 commit 6edf3e8
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ macro_rules! define_uuid_macro {
};
OUTPUT
}};
($uuid:ident) => {{
const OUTPUT: $crate::Uuid = match $crate::Uuid::try_parse($uuid) {
$crate::__macro_support::Ok(u) => u,
$crate::__macro_support::Err(_) => panic!("invalid UUID"),
};
OUTPUT
}};
}
}
}
Expand Down Expand Up @@ -50,6 +57,12 @@ define_uuid_macro! {
/// # use uuid::uuid;
/// let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");
/// ```
/// Using a const variable:
/// ```
/// # use uuid::uuid;
/// const UUID_STR: &str = "12345678-1234-5678-1234-567812345678";
/// let UUID = uuid!(UUID_STR);
/// ```
///
/// ## Compilation Failures
///
Expand All @@ -71,22 +84,5 @@ define_uuid_macro! {
/// | ^
/// ```
///
/// Tokens that aren't string literals are also rejected:
///
/// ```compile_fail
/// # use uuid::uuid;
/// let uuid_str: &str = "550e8400e29b41d4a716446655440000";
/// let uuid = uuid!(uuid_str);
/// ```
///
/// Provides the following compilation error:
///
/// ```txt
/// error: expected string literal
/// |
/// | let uuid = uuid!(uuid_str);
/// | ^^^^^^^^
/// ```
///
/// [uuid::Uuid]: https://docs.rs/uuid/*/uuid/struct.Uuid.html
}

0 comments on commit 6edf3e8

Please sign in to comment.