Skip to content

Commit

Permalink
Add more struct-level validations
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed Nov 26, 2024
1 parent 3a421b7 commit 6dfbd4a
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions fieldx_aux/src/builder_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::{
use darling::{util::Flag, FromMeta};
use fieldx_derive_support::fxhelper;
use getset::Getters;
use proc_macro2::Span;
use syn::{spanned::Spanned, Lit, Token};
use syn::{Lit, Token};

// TODO try to issue warnings with `diagnostics` for sub-arguments which are not supported at struct or field level.
#[fxhelper(validate = Self::validate)]
Expand Down Expand Up @@ -59,17 +58,25 @@ impl<const STRUCT: bool> FXBuilderHelper<STRUCT> {
}

pub fn validate(&self) -> darling::Result<()> {
if !STRUCT && self.error.is_some() {
return Err(
darling::Error::custom(format!("parameter 'error' is only supported at struct level")).with_span(
&self
.error
.as_ref()
.unwrap()
.orig()
.map_or_else(|| Span::call_site(), |o| o.span()),
),
);
if !STRUCT {
if self.error.is_some() {
return Err(
darling::Error::custom(format!("parameter 'error' is only supported at struct level"))
.with_span(&self.error.fx_span()),
);
}
if self.post_build.is_some() {
return Err(darling::Error::custom(format!(
"parameter 'post_build' is only supported at struct level"
))
.with_span(&self.post_build.fx_span()));
}
if self.opt_in.is_some() {
return Err(
darling::Error::custom(format!("parameter 'opt_in' is only supported at struct level"))
.with_span(&self.opt_in.fx_span()),
);
}
}
Ok(())
}
Expand Down

0 comments on commit 6dfbd4a

Please sign in to comment.