Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ jobs:
run: |
nix develop .#ci -c cargo run --example status
nix develop .#ci -c cargo run --example op
nix develop .#ci -c cargo run --example clap
nix develop .#ci -c cargo run --features=nesting --example nesting
nix develop .#ci -c cargo run --features=nesting --example clap
nix develop .#ci -c cargo test
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [

[workspace.package]
authors = ["Antonio Yang <yanganto@gmail.com>"]
version = "0.10.4"
version = "0.10.5"
edition = "2021"
categories = ["development-tools"]
keywords = ["struct", "patch", "macro", "derive", "overlay"]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ Field attributes:
- `#[patch(name = "...")]`: change the type of the field in the generated patch struct.
- `#[patch(attribute(...))]`: add attributes to the field in the generated patch struct.
- `#[patch(attribute(derive(...)))]`: add derives to the field in the generated patch struct.
- `#[patch(empty_value = ...)]`: define a value as empty, so the corresponding field of patch will not wrapped by Option, and apply patch when the field is empty.
- `#[filler(extendable)]`: use the struct of field for filler, the struct needs implement `Default`, `Extend`, `IntoIterator` and `is_empty`.
- `#[filler(empty_value)]`: define a value as empty, so the corresponding field of Filler will be applied, even the field is not `Option` or `Extendable`.
- `#[filler(empty_value = ...)]`: define a value as empty, so the corresponding field of Filler will be applied, even the field is not `Option` or `Extendable`.

Please check the [traits][doc-traits] of document to learn more.

Expand Down
26 changes: 3 additions & 23 deletions derive/src/filler.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use std::str::FromStr;
use syn::meta::ParseNestedMeta;
use syn::spanned::Spanned;
use syn::{parenthesized, DeriveInput, Error, Lit, LitStr, Result, Type};
use syn::{parenthesized, DeriveInput, Lit, LitStr, Result, Type};

#[cfg(feature = "op")]
use crate::Addable;
Expand Down Expand Up @@ -415,7 +413,7 @@ impl Field {
return Err(meta
.error("The field is already the field of filler, we can't defined more than once"));
}
if let Some(lit) = get_lit(path, &meta)? {
if let Some(lit) = crate::get_lit(path, &meta)? {
fty = Some(FillerType::NativeValue(lit));
} else {
return Err(meta
Expand All @@ -429,6 +427,7 @@ impl Field {
}
#[cfg(not(feature = "op"))]
ADDABLE => {
use syn::spanned::Spanned;
return Err(syn::Error::new(
ident.span(),
"`addable` needs `op` feature",
Expand Down Expand Up @@ -501,22 +500,3 @@ fn none_option_filler_type(ty: &Type) -> Ident {
panic!("#[filler(extendable)] should use on a type")
}
}

fn get_lit(attr_name: String, meta: &ParseNestedMeta) -> syn::Result<Option<syn::Lit>> {
let expr: syn::Expr = meta.value()?.parse()?;
let mut value = &expr;
while let syn::Expr::Group(e) = value {
value = &e.expr;
}
if let syn::Expr::Lit(syn::ExprLit { lit, .. }) = value {
Ok(Some(lit.clone()))
} else {
Err(Error::new(
expr.span(),
format!(
"expected serde {} attribute to be lit: `{} = \"...\"`",
attr_name, attr_name
),
))
}
}
24 changes: 24 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ mod patch;
use filler::Filler;
use patch::Patch;

use syn::meta::ParseNestedMeta;
use syn::spanned::Spanned;
use syn::Error;

#[cfg(feature = "op")]
pub(crate) enum Addable {
Disable,
Expand All @@ -29,3 +33,23 @@ pub fn derive_filler(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
.unwrap()
.into()
}

fn get_lit(attr_name: String, meta: &ParseNestedMeta) -> syn::Result<Option<syn::Lit>> {
let expr: syn::Expr = meta.value()?.parse()?;
let mut value = &expr;
while let syn::Expr::Group(e) = value {
value = &e.expr;
}
if let syn::Expr::Lit(syn::ExprLit { lit, .. }) = value {
Ok(Some(lit.clone()))
} else {
Err(Error::new(
expr.span(),
format!(
"expected serde {} attribute to be lit: `{} = \"...\"`",
attr_name, attr_name
),
))
}
}

Loading