mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2025-10-21 19:00:34 +00:00
fix: clippy errors
This commit is contained in:
parent
b2cf463ff8
commit
5bc3aa055a
136
src/from_file.rs
136
src/from_file.rs
@ -1,8 +1,8 @@
|
|||||||
use proc_macro::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
use syn::{
|
use syn::{
|
||||||
Attribute, Data, DeriveInput, Error, Expr, Fields, GenericParam, Generics, Meta, Result,
|
Attribute, Data, DeriveInput, Error, Expr, Fields, FieldsNamed, GenericParam, Generics, Meta,
|
||||||
WherePredicate, parse_quote, parse2,
|
Result, WhereClause, WherePredicate, parse_quote, parse2,
|
||||||
};
|
};
|
||||||
|
|
||||||
const WITH_MERGE: bool = cfg!(feature = "merge");
|
const WITH_MERGE: bool = cfg!(feature = "merge");
|
||||||
@ -10,47 +10,72 @@ const WITH_MERGE: bool = cfg!(feature = "merge");
|
|||||||
pub fn impl_from_file(input: &DeriveInput) -> Result<TokenStream> {
|
pub fn impl_from_file(input: &DeriveInput) -> Result<TokenStream> {
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
let vis = &input.vis;
|
let vis = &input.vis;
|
||||||
let generics = add_trait_bouts(input.generics.clone());
|
let generics = add_trait_bouds(input.generics.clone());
|
||||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||||
|
|
||||||
let file_ident = format_ident!("{name}File");
|
let file_ident = format_ident!("{name}File");
|
||||||
|
|
||||||
let fields = match &input.data {
|
let fields = extract_named_fields(input)?;
|
||||||
|
let (field_assignments, file_fields, default_bounds) = process_fields(fields)?;
|
||||||
|
|
||||||
|
let where_clause = build_where_clause(where_clause.cloned(), default_bounds)?;
|
||||||
|
|
||||||
|
let derive_clause = build_derive_clause();
|
||||||
|
|
||||||
|
Ok(quote! {
|
||||||
|
#derive_clause
|
||||||
|
#vis struct #file_ident {
|
||||||
|
#(#file_fields),*
|
||||||
|
}
|
||||||
|
|
||||||
|
impl #impl_generics #name #ty_generics #where_clause {
|
||||||
|
pub fn from_file(file: Option<#file_ident #ty_generics>) -> Self {
|
||||||
|
let file = file.unwrap_or_default();
|
||||||
|
Self {
|
||||||
|
#(#field_assignments),*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl #impl_generics From<Option<#file_ident #ty_generics>> for #name #ty_generics #where_clause {
|
||||||
|
fn from(value: Option<#file_ident #ty_generics>) -> Self {
|
||||||
|
Self::from_file(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_named_fields(input: &DeriveInput) -> Result<&FieldsNamed> {
|
||||||
|
match &input.data {
|
||||||
Data::Struct(ds) => match &ds.fields {
|
Data::Struct(ds) => match &ds.fields {
|
||||||
Fields::Named(fields) => &fields.named,
|
Fields::Named(fields) => Ok(fields),
|
||||||
_ => {
|
_ => Err(Error::new_spanned(
|
||||||
return Err(Error::new_spanned(
|
|
||||||
&input.ident,
|
&input.ident,
|
||||||
"FromFile can only be derived for structs with named fields",
|
"FromFile can only be derived for structs with named fields",
|
||||||
));
|
)),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => {
|
_ => Err(Error::new_spanned(
|
||||||
return Err(Error::new_spanned(
|
|
||||||
&input.ident,
|
&input.ident,
|
||||||
"FromFile can only be derived for structs",
|
"FromFile can only be derived for structs",
|
||||||
));
|
)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
fn process_fields(
|
||||||
|
fields: &FieldsNamed,
|
||||||
|
) -> Result<(Vec<TokenStream>, Vec<TokenStream>, Vec<TokenStream>)> {
|
||||||
let mut field_assignments = Vec::new();
|
let mut field_assignments = Vec::new();
|
||||||
let mut file_fields = Vec::new();
|
let mut file_fields = Vec::new();
|
||||||
let mut default_bounds = Vec::new();
|
let mut default_bounds = Vec::new();
|
||||||
|
|
||||||
for field in fields {
|
for field in &fields.named {
|
||||||
let ident = field
|
let ident = field
|
||||||
.ident
|
.ident
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.ok_or_else(|| Error::new_spanned(field, "Expected named fields"))?;
|
.ok_or_else(|| Error::new_spanned(field, "Expected named fields"))?;
|
||||||
let ty = &field.ty;
|
let ty = &field.ty;
|
||||||
|
|
||||||
let mut default_expr = None;
|
let default_expr = parse_default_attrs(&field.attrs)?;
|
||||||
for attr in &field.attrs {
|
|
||||||
if let Some(expr) = parse_default_attr(attr)? {
|
|
||||||
default_expr = Some(expr);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let field_attrs = if WITH_MERGE {
|
let field_attrs = if WITH_MERGE {
|
||||||
quote! {
|
quote! {
|
||||||
@ -76,10 +101,18 @@ pub fn impl_from_file(input: &DeriveInput) -> Result<TokenStream> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let where_clause = if default_bounds.is_empty() {
|
Ok((field_assignments, file_fields, default_bounds))
|
||||||
where_clause.cloned()
|
}
|
||||||
} else {
|
|
||||||
let mut where_clause = where_clause.cloned();
|
fn build_where_clause(
|
||||||
|
where_clause: Option<WhereClause>,
|
||||||
|
default_bounds: Vec<TokenStream>,
|
||||||
|
) -> Result<Option<WhereClause>> {
|
||||||
|
if default_bounds.is_empty() {
|
||||||
|
return Ok(where_clause);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut where_clause = where_clause;
|
||||||
if let Some(wc) = &mut where_clause {
|
if let Some(wc) = &mut where_clause {
|
||||||
for bound in default_bounds {
|
for bound in default_bounds {
|
||||||
let predicate = parse2::<WherePredicate>(bound.clone())
|
let predicate = parse2::<WherePredicate>(bound.clone())
|
||||||
@ -89,44 +122,22 @@ pub fn impl_from_file(input: &DeriveInput) -> Result<TokenStream> {
|
|||||||
} else {
|
} else {
|
||||||
where_clause = Some(parse_quote!(where #(#default_bounds),*));
|
where_clause = Some(parse_quote!(where #(#default_bounds),*));
|
||||||
}
|
}
|
||||||
where_clause
|
Ok(where_clause)
|
||||||
};
|
|
||||||
|
|
||||||
// Conditionally include Merge derive based on feature
|
|
||||||
let derive_clause = if WITH_MERGE {
|
|
||||||
quote! {
|
|
||||||
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize, merge::Merge)]
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
fn build_derive_clause() -> TokenStream {
|
||||||
|
if WITH_MERGE {
|
||||||
|
return quote! {
|
||||||
|
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize, merge::Merge)]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize)]
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
Ok(quote! {
|
|
||||||
#derive_clause
|
|
||||||
#vis struct #file_ident {
|
|
||||||
#(#file_fields),*
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl #impl_generics #name #ty_generics #where_clause {
|
fn add_trait_bouds(mut generics: Generics) -> Generics {
|
||||||
pub fn from_file(file: Option<#file_ident #ty_generics>) -> Self {
|
|
||||||
let file = file.unwrap_or_default();
|
|
||||||
Self {
|
|
||||||
#(#field_assignments),*
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl #impl_generics From<Option<#file_ident #ty_generics>> for #name #ty_generics #where_clause {
|
|
||||||
fn from(value: Option<#file_ident #ty_generics>) -> Self {
|
|
||||||
Self::from_file(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_trait_bouts(mut generics: Generics) -> Generics {
|
|
||||||
for param in &mut generics.params {
|
for param in &mut generics.params {
|
||||||
if let GenericParam::Type(type_param) = param {
|
if let GenericParam::Type(type_param) = param {
|
||||||
type_param.bounds.push(parse_quote!(Default));
|
type_param.bounds.push(parse_quote!(Default));
|
||||||
@ -135,6 +146,15 @@ fn add_trait_bouts(mut generics: Generics) -> Generics {
|
|||||||
generics
|
generics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_default_attrs(attrs: &[Attribute]) -> Result<Option<Expr>> {
|
||||||
|
for attr in attrs {
|
||||||
|
if let Some(expr) = parse_default_attr(attr)? {
|
||||||
|
return Ok(Some(expr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_default_attr(attr: &Attribute) -> Result<Option<Expr>> {
|
fn parse_default_attr(attr: &Attribute) -> Result<Option<Expr>> {
|
||||||
if !attr.path().is_ident("default") {
|
if !attr.path().is_ident("default") {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
|||||||
@ -9,5 +9,7 @@ use syn::{DeriveInput, parse_macro_input};
|
|||||||
#[proc_macro_derive(FromFile, attributes(from_file))]
|
#[proc_macro_derive(FromFile, attributes(from_file))]
|
||||||
pub fn derive_from_file(input: TokenStream) -> TokenStream {
|
pub fn derive_from_file(input: TokenStream) -> TokenStream {
|
||||||
let inp = parse_macro_input!(input as DeriveInput);
|
let inp = parse_macro_input!(input as DeriveInput);
|
||||||
impl_from_file(&inp).unwrap_or_else(|e| e.to_compile_error().into())
|
impl_from_file(&inp)
|
||||||
|
.unwrap_or_else(|e| e.to_compile_error())
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user