Skip to content

Commit

Permalink
prost-validate-derive-core: strip enum super:: prefix when module i…
Browse files Browse the repository at this point in the history
…s specified

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
  • Loading branch information
Adphi committed Oct 15, 2024
1 parent f27cae5 commit cfab2bf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions prost-validate-derive-core/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@ pub fn derive_with_module(
let implementation = match opts.data {
Data::Enum(e) => e
.iter()
.map(|v| Field {
enum_strip_super: module.is_some(),
..v.clone()
})
.map(|v| v.to_token_stream())
.collect::<proc_macro2::TokenStream>(),
Data::Struct(s) => s
.fields
.iter()
.map(|v| Field {
enum_strip_super: module.is_some(),
..v.clone()
})
.map(|field| field.into_token_stream())
.collect::<proc_macro2::TokenStream>(),
};

let allow = quote! {
#[allow(irrefutable_let_patterns)]
#[allow(unused_variables)]
};

let path = if let Some(module) = module {
quote! { #module::#ident }
} else {
Expand All @@ -43,6 +56,7 @@ pub fn derive_with_module(
if !implementation.is_empty() {
quote! {
impl ::prost_validate::Validator for #path {
#allow
fn validate(&self) -> prost_validate::Result<()> {
#implementation
Ok(())
Expand Down
7 changes: 6 additions & 1 deletion prost-validate-derive-core/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ impl ToValidationTokens for EnumRules {
}
});
let defined_only = rules.defined_only.is_true_and(|| {
let enum_type: syn::Path = syn::parse_str(ctx.to_owned().enumeration.expect("missing enum type").as_str())
let enumeration = if ctx.enum_strip_super {
ctx.enumeration.to_owned().expect("missing enum type").strip_prefix("super::").unwrap().to_string()
} else {
ctx.enumeration.to_owned().expect("missing enum type")
};
let enum_type: syn::Path = syn::parse_str(enumeration.as_str())
.expect("Invalid enum path");
let field = &ctx.name;
quote! {
Expand Down
4 changes: 4 additions & 0 deletions prost-validate-derive-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Context<'a> {
pub oneof: bool,
pub prost_types: bool,
pub wrapper: bool,
pub enum_strip_super: bool,
}

pub trait ToValidationTokens {
Expand All @@ -41,6 +42,7 @@ pub struct Field {
pub prost: ProstField,
pub oneof: bool,
pub map: bool,
pub enum_strip_super: bool,
}

impl Field {
Expand Down Expand Up @@ -72,6 +74,7 @@ impl Field {
validation,
oneof,
map: map.is_some(),
enum_strip_super: false,
}
}
pub fn is_wkt(&self) -> bool {
Expand Down Expand Up @@ -555,6 +558,7 @@ impl ToTokens for Field {
map: self.map,
oneof: self.oneof,
prost_types: self.is_prost_types(),
enum_strip_super: self.enum_strip_super,
};
let name = if self.oneof {
let name = to_snake(ident.to_string());
Expand Down

0 comments on commit cfab2bf

Please # to comment.