Skip to content

Commit

Permalink
update display derive
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurZucker committed Jun 12, 2024
1 parent f67af9c commit 4c3f37a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
1 change: 1 addition & 0 deletions tokenizers/display_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
syn = "1.0"
quote = "1.0"
proc-macro2 = "1.0"
unicode-xid = "0.2.4"

[lib]
proc-macro = true
25 changes: 20 additions & 5 deletions tokenizers/display_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::{format_ident,quote};
use syn::{parse_macro_input, Data, DeriveInput, Fields, Lit, Meta, MetaList, NestedMeta};
use syn::{parse_macro_input, DeriveInput};
mod vendored;
mod parsing;
use vendored::FmtAttribute;

#[proc_macro_derive(Display)]
pub fn display_derive(input: TokenStream) -> syn::Result<TokenStream> {
pub fn display_derive(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = parse_macro_input!(input as DeriveInput);

Expand Down Expand Up @@ -89,12 +90,26 @@ fn expand_enum(
}

let match_arms = e.variants.iter().try_fold(
TokenStream::new, |variant| {
let attrs = FmtAttribute::parse_attrs(&variant.attrs, attr_name)?
(Vec::new(), TokenStream::new()),
|mut arms, variant| {
let attrs = ContainerAttributes::parse_attrs(&variant.attrs, attr_name)?

Check failure on line 95 in tokenizers/display_derive/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check it builds for Windows 32-bit (3.9)

failed to resolve: use of undeclared type `ContainerAttributes`

Check failure on line 95 in tokenizers/display_derive/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check it builds for Windows 32-bit (3.7)

failed to resolve: use of undeclared type `ContainerAttributes`
.map(Spanning::into_inner)

Check failure on line 96 in tokenizers/display_derive/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check it builds for Windows 32-bit (3.9)

failed to resolve: use of undeclared type `Spanning`

Check failure on line 96 in tokenizers/display_derive/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check it builds for Windows 32-bit (3.7)

failed to resolve: use of undeclared type `Spanning`
.unwrap_or_default();
let ident = &variant.ident;

if attrs.fmt.is_none()
&& variant.fields.is_empty()
&& attr_name != "display"
{
return Err(syn::Error::new(
e.variants.span(),
format!(
"implicit formatting of unit enum variant is supported only for `Display` \
macro, use `#[{attr_name}(\"...\")]` to explicitly specify the formatting",
),
));
}

let v = Expansion {
attrs: &attrs,
fields: &variant.fields,
Expand All @@ -121,7 +136,7 @@ fn expand_enum(

Ok::<_, syn::Error>(arms)
},
)?;
)?;

let body = match_arms
.is_empty()
Expand Down
34 changes: 13 additions & 21 deletions tokenizers/display_derive/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ pub(crate) fn format_string(input: &str) -> Option<FormatString<'_>> {
let formats = iter::repeat(())
.scan(&mut input, |input, _| {
let (curr, format) =
alt(&mut [&mut maybe_format, &mut map(text, |(i, _)| (i, None))])(
input,
)?;
alt(&mut [&mut maybe_format, &mut map(text, |(i, _)| (i, None))])(input)?;
**input = curr;
Some(format)
})
Expand Down Expand Up @@ -600,9 +598,7 @@ fn lookahead(
fn optional_result<'i, T: 'i>(
mut parser: impl FnMut(&'i str) -> Option<(&'i str, T)>,
) -> impl FnMut(&'i str) -> (LeftToParse<'i>, Option<T>) {
move |input: &str| {
map_or_else(&mut parser, |i| (i, None), |(i, c)| (i, Some(c)))(input)
}
move |input: &str| map_or_else(&mut parser, |i| (i, None), |(i, c)| (i, Some(c)))(input)
}

/// Parses while `parser` is successful. Never fails.
Expand Down Expand Up @@ -682,9 +678,7 @@ fn char(c: char) -> impl FnMut(&str) -> Option<LeftToParse<'_>> {
/// Checks whether first [`char`] suits `check`.
///
/// [`char`]: fn@char
fn check_char(
mut check: impl FnMut(char) -> bool,
) -> impl FnMut(&str) -> Option<LeftToParse<'_>> {
fn check_char(mut check: impl FnMut(char) -> bool) -> impl FnMut(&str) -> Option<LeftToParse<'_>> {
move |input| {
input
.chars()
Expand Down Expand Up @@ -1159,9 +1153,7 @@ mod tests {
alternate: None,
zero_padding: None,
width: None,
precision: Some(Precision::Count(Count::Parameter(
Argument::Integer(0),
))),
precision: Some(Precision::Count(Count::Parameter(Argument::Integer(0),))),
ty: Type::Display,
}),
}],
Expand All @@ -1178,9 +1170,9 @@ mod tests {
alternate: None,
zero_padding: None,
width: None,
precision: Some(Precision::Count(Count::Parameter(
Argument::Identifier("par"),
))),
precision: Some(Precision::Count(Count::Parameter(Argument::Identifier(
"par"
),))),
ty: Type::Display,
}),
}],
Expand All @@ -1197,9 +1189,9 @@ mod tests {
alternate: Some(Alternate),
zero_padding: None,
width: Some(Count::Parameter(Argument::Integer(2))),
precision: Some(Precision::Count(Count::Parameter(
Argument::Identifier("par"),
))),
precision: Some(Precision::Count(Count::Parameter(Argument::Identifier(
"par"
),))),
ty: Type::Display,
}),
}],
Expand Down Expand Up @@ -1250,9 +1242,9 @@ mod tests {
alternate: Some(Alternate),
zero_padding: None,
width: Some(Count::Parameter(Argument::Identifier("par"))),
precision: Some(Precision::Count(Count::Parameter(
Argument::Identifier("par"),
))),
precision: Some(Precision::Count(Count::Parameter(Argument::Identifier(
"par"
),))),
ty: Type::UpperDebug,
}),
}],
Expand Down
15 changes: 6 additions & 9 deletions tokenizers/display_derive/src/vendored.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use syn::LitStr;
use proc_macro2::TokenStream;
use crate::parsing;
use proc_macro2::TokenStream;
use quote::{format_ident, ToTokens};
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned as _,
token,
token,
Expr,
};

/// Representation of a [`fmt`]-like attribute.
///
/// ```rust,ignore
Expand All @@ -17,7 +16,7 @@ use syn::{
///
/// [`fmt`]: std::fmt
#[derive(Debug)]
struct FmtAttribute {
pub struct FmtAttribute {
/// Interpolation [`syn::LitStr`].
///
/// [`syn::LitStr`]: struct@syn::LitStr
Expand All @@ -42,7 +41,7 @@ impl Parse for FmtAttribute {
.peek(token::Comma)
.then(|| input.parse())
.transpose()?,
args: input.parse_terminated(FmtArgument::parse, token::Comma)?,
args: input.parse_terminated(FmtArgument::parse)?,
})
}
}
Expand Down Expand Up @@ -70,8 +69,7 @@ impl FmtAttribute {

// (1) There is exactly one formatting parameter.
let lit = self.lit.value();
let param =
parsing::format(&lit).and_then(|(more, p)| more.is_empty().then_some(p))?;
let param = parsing::format(&lit).and_then(|(more, p)| more.is_empty().then_some(p))?;

// (2) And the formatting parameter doesn't contain any modifiers.
if param
Expand Down Expand Up @@ -164,4 +162,3 @@ impl ToTokens for FmtArgument {
self.expr.to_tokens(tokens);
}
}

0 comments on commit 4c3f37a

Please sign in to comment.