Skip to content
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

feat(DeriveAttribute) Implement derive macro attribute for format #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,29 @@ fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}

#[test]
fn test_format_not_valuable() {
#[derive(Debug)]
struct NotValuable;

#[derive(Valuable)]
struct S {
f1: (),
#[valuable(format = "{:?}")]
f2: NotValuable,
f3: (),
}

#[derive(Valuable)]
struct T((), #[valuable(format = "{:?}")] NotValuable, ());

let s = Structable::definition(&S {
f1: (),
f2: NotValuable,
f3: (),
});
assert!(matches!(s.fields(), Fields::Named(f) if f.len() == 3));
let s = Structable::definition(&T((), NotValuable, ()));
assert!(matches!(s.fields(), Fields::Unnamed(f) if *f == 3));
}
17 changes: 16 additions & 1 deletion valuable-derive/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ static ATTRS: &[AttrDef] = &[
],
style: &[MetaStyle::Ident],
},
// #[valuable(format)]
AttrDef {
name: "format",
conflicts_with: &["skip", "rename"],
position: &[Position::NamedField, Position::UnnamedField],
style: &[MetaStyle::NameValue],
},
];

pub(crate) struct Attrs {
rename: Option<(syn::MetaNameValue, syn::LitStr)>,
transparent: Option<Span>,
skip: Option<Span>,
format: Option<(syn::MetaNameValue, syn::LitStr)>,
}

impl Attrs {
Expand All @@ -65,12 +73,17 @@ impl Attrs {
pub(crate) fn skip(&self) -> bool {
self.skip.is_some()
}

pub(crate) fn format(&self) -> Option<syn::LitStr> {
self.format.as_ref().map(|(_, format)| format).cloned()
}
}

pub(crate) fn parse_attrs(cx: &Context, attrs: &[syn::Attribute], pos: Position) -> Attrs {
let mut rename = None;
let mut transparent = None;
let mut skip = None;
let mut format = None;

let attrs = filter_attrs(cx, attrs, pos);
for (def, meta) in &attrs {
Expand Down Expand Up @@ -104,7 +117,8 @@ pub(crate) fn parse_attrs(cx: &Context, attrs: &[syn::Attribute], pos: Position)
"transparent" => transparent = Some(meta.span()),
// #[valuable(skip)]
"skip" => skip = Some(meta.span()),

// #[valuable(format = "{}")]
"format" => lit_str!(format),
_ => unreachable!("{}", def.name),
}
}
Expand All @@ -113,6 +127,7 @@ pub(crate) fn parse_attrs(cx: &Context, attrs: &[syn::Attribute], pos: Position)
rename,
transparent,
skip,
format,
}
}

Expand Down
23 changes: 18 additions & 5 deletions valuable-derive/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,18 @@ fn derive_struct(
.iter()
.enumerate()
.filter(|(i, _)| !field_attrs[*i].skip())
.map(|(_, field)| {
.map(|(i, field)| {
let f = field.ident.as_ref();
let tokens = quote! {
&self.#f
let tokens = if let Some(format_str) = field_attrs[i].format() {
quote! {
&format!(#format_str, self.#f)
}
} else {
quote! {
&self.#f
}
};

respan(tokens, &field.ty)
});
visit_fields = quote! {
Expand All @@ -125,8 +132,14 @@ fn derive_struct(
.filter(|(i, _)| !field_attrs[*i].skip())
.map(|(i, field)| {
let index = syn::Index::from(i);
let tokens = quote! {
&self.#index
let tokens = if let Some(format_str) = field_attrs[i].format() {
quote! {
&format!(#format_str, self.#index)
}
} else {
quote! {
&self.#index
}
};
respan(tokens, &field.ty)
})
Expand Down