New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Impl UUID macro #543
Merged
Merged
Impl UUID macro #543
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6290874
Implemented uuid!("")
QnnOkabayashi 622806c
Fixed undetected `crate::std` errors
QnnOkabayashi 860818a
Improved testing
QnnOkabayashi 8868cd5
Improved diagnostics
QnnOkabayashi 733cf38
Made `macros` module in `uuid`
QnnOkabayashi 96ef40c
Micro refactor
QnnOkabayashi d6aed32
Merge branch 'main' into impl-uuid-macro
QnnOkabayashi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ edition = "2018" | |
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "1.0.80" | ||
quote = "1.0.10" | ||
proc-macro2 = "1.0.29" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,106 @@ | ||
use std; | ||
use proc_macro::TokenStream; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::{quote, quote_spanned}; | ||
use std::fmt; | ||
use syn::spanned::Spanned; | ||
|
||
#[cfg(any(feature = "std", test))] | ||
#[macro_use] | ||
extern crate std; | ||
|
||
#[cfg(all(not(feature = "std"), not(test)))] | ||
#[macro_use] | ||
extern crate core as std; | ||
|
||
#[path = "../../shared/error.rs"] | ||
#[allow(dead_code)] | ||
mod error; | ||
|
||
#[path = "../../shared/parser.rs"] | ||
#[allow(dead_code)] | ||
mod parser; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
assert_eq!(2 + 2, 4); | ||
/// 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 | ||
#[proc_macro] | ||
pub fn uuid(input: TokenStream) -> TokenStream { | ||
build_uuid(input.clone()).unwrap_or_else(|e| { | ||
let msg = e.to_string(); | ||
TokenStream::from(quote_spanned! { | ||
TokenStream2::from(input).span() => | ||
compile_error!(#msg) | ||
}) | ||
}) | ||
} | ||
|
||
enum Error { | ||
NonStringLiteral, | ||
UuidParse(error::Error), | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match *self { | ||
Error::NonStringLiteral => f.write_str("expected string literal"), | ||
Error::UuidParse(ref e) => write!(f, "{}", e), | ||
} | ||
} | ||
} | ||
|
||
fn build_uuid(input: TokenStream) -> Result<TokenStream, Error> { | ||
let uuid_str = match syn::parse::<syn::Lit>(input) { | ||
Ok(syn::Lit::Str(ref literal)) => literal.value(), | ||
_ => return Err(Error::NonStringLiteral), | ||
}; | ||
|
||
let bytes = parser::parse_str(&uuid_str).map_err(Error::UuidParse)?; | ||
|
||
let tokens = bytes | ||
.iter() | ||
.map(|byte| quote! { #byte, }) | ||
.collect::<TokenStream2>(); | ||
|
||
Ok(quote! {::uuid::Uuid::from_bytes([#tokens])}.into()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Required to stop
shared/error.rs
andshared/parser.rs
from complaining. I just copy-pasted it fromuuid
.