Skip to content

Commit

Permalink
remove proc-macro-error dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Aug 8, 2023
1 parent 6731e5b commit b34c42d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[workspace.package]
authors = ["Antonio Yang <[email protected]>"]
version = "0.3.1"
version = "0.3.2"
edition = "2021"
categories = ["development-tools"]
keywords = ["struct", "patch", "macro", "derive", "overlay"]
Expand Down
1 change: 0 additions & 1 deletion struct-patch-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
proc-macro-error = "1.0"
syn = { version = "2.0", features = ["parsing"] }

[features]
Expand Down
55 changes: 23 additions & 32 deletions struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use proc_macro_error::{abort, proc_macro_error};
use quote::quote;
use syn::{Expr, Lit, Meta, MetaList, MetaNameValue, PathSegment};

#[proc_macro_derive(Patch, attributes(patch_derive, patch_name, patch_skip))]
#[proc_macro_error]
pub fn derive_patch(item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::DeriveInput);
let struct_name = &input.ident;
Expand All @@ -26,50 +24,43 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
}) => {
let mut path_clone = path.clone();
let mut segments = path.segments.clone();
match path
if let Some("patch_derive") = path
.segments
.first()
.map(|s| s.ident.to_string())
.as_deref()
{
Some("patch_derive") => {
if let Some(seg) = segments.first_mut() {
*seg = PathSegment {
ident: Ident::new("derive", Span::call_site()),
arguments: seg.arguments.clone(),
};
}
path_clone.segments = segments;
attr_clone.meta = Meta::List(MetaList {
path: path_clone,
tokens,
delimiter,
});
patch_derive = Some(attr_clone);
.as_deref() {
if let Some(seg) = segments.first_mut() {
*seg = PathSegment {
ident: Ident::new("derive", Span::call_site()),
arguments: seg.arguments.clone(),
};
}
_ => {}
path_clone.segments = segments;
attr_clone.meta = Meta::List(MetaList {
path: path_clone,
tokens,
delimiter,
});
patch_derive = Some(attr_clone);
}

}
Meta::NameValue(MetaNameValue {
path,
value: Expr::Lit(lit, ..),
..
}) => {
match path
if let Some("patch_name") = path
.segments
.first()
.map(|s| s.ident.to_string())
.as_deref()
{
Some("patch_name") => {
if let Lit::Str(l) = lit.lit {
patch_struct_name = Some(Ident::new(
format!("{}", l.value()).trim_matches('"'),
Span::call_site(),
));
}
.as_deref() {
if let Lit::Str(l) = lit.lit {
patch_struct_name = Some(Ident::new(
l.value().to_string().trim_matches('"'),
Span::call_site(),
));
}
_ => {}
}
}
_ => (),
Expand All @@ -79,7 +70,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
let fields = if let syn::Data::Struct(syn::DataStruct { fields, .. }) = &input.data {
fields
} else {
abort!(&input.ident, "Patch derive only use for struct")
return syn::Error::new(input.ident.span(), "Patch derive only use for struct").to_compile_error().into()
};
let fields_with_type = match fields {
syn::Fields::Named(f) => f
Expand Down
2 changes: 1 addition & 1 deletion struct-patch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
readme.workspace = true

[dependencies]
struct-patch-derive = { version = "=0.3.1", path = "../struct-patch-derive" }
struct-patch-derive = { version = "=0.3.2", path = "../struct-patch-derive" }

[dev-dependencies]
serde_json = "1.0"
Expand Down

0 comments on commit b34c42d

Please sign in to comment.