Macro uuid::uuid

source ·
macro_rules! uuid {
    ($uuid:literal) => { ... };
}
Expand description

Parse Uuids from string literals at compile time.

§Usage

This macro transforms the string literal representation of a Uuid into the bytes representation, raising a compilation error if it cannot properly be parsed.

§Examples

Setting a global constant:

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:

let uuid = uuid!("urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4");

§Compilation Failures

Invalid UUIDs are rejected:

let uuid = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");

Enable the feature macro-diagnostics to see the error messages below.

Provides the following compilation error:

error: invalid character: expected an optional prefix of `urn:uuid:` followed by 0123456789abcdefABCDEF-, found Z at 9
    |
    |     let id = uuid!("F9168C5E-ZEB2-4FAA-B6BF-329BF39FA1E4");
    |                              ^

Tokens that aren’t string literals are also rejected:

let uuid_str: &str = "550e8400e29b41d4a716446655440000";
let uuid = uuid!(uuid_str);

Provides the following compilation error:

error: expected string literal
  |
  |     let uuid = uuid!(uuid_str);
  |                      ^^^^^^^^