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
3 changes: 1 addition & 2 deletions prost-validate-derive-core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct AnyRules {

impl ToValidationTokens for AnyRules {
fn to_validation_tokens(&self, ctx: &Context, name: &Ident) -> TokenStream {
let field = &ctx.name;
let rules = prost_validate_types::AnyRules::from(self.to_owned());
let r#in = rules.r#in.is_empty().not().then(|| {
let v = rules.r#in;
let field = &ctx.name;
quote! {
let values = vec![#(#v),*];
if !values.contains(&#name.type_url.as_str()) {
Expand All @@ -28,7 +28,6 @@ impl ToValidationTokens for AnyRules {
});
let not_in = rules.not_in.is_empty().not().then(|| {
let v = rules.not_in;
let field = &ctx.name;
quote! {
let values = vec![#(#v),*];
if values.contains(&#name.type_url.as_str()) {
Expand Down
2 changes: 1 addition & 1 deletion prost-validate-derive-core/src/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct BoolRules {

impl ToValidationTokens for BoolRules {
fn to_validation_tokens(&self, ctx: &Context, name: &Ident) -> TokenStream {
let field = &ctx.name;
let r#const = self.r#const.map(|v| {
let field = &ctx.name;
quote! {
if *#name != #v {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::r#bool::Error::Const(#v)));
Expand Down
30 changes: 10 additions & 20 deletions prost-validate-derive-core/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub struct BytesRules {

impl ToValidationTokens for BytesRules {
fn to_validation_tokens(&self, ctx: &Context, name: &Ident) -> TokenStream {
let field = &ctx.name;
let rules = prost_validate_types::BytesRules::from(self.to_owned());
let r#const = rules.r#const.map(|v| {
let v = LitByteStr::new(v.as_slice(), Span::call_site());
let field = &ctx.name;
quote! {
if !#name.iter().eq(#v.iter()) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Const(#v.to_vec())));
Expand All @@ -37,7 +37,6 @@ impl ToValidationTokens for BytesRules {
});
let len = rules.len.map(|v| {
let v = v as usize;
let field = &ctx.name;
quote! {
if #name.len() != #v {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Len(#v)));
Expand All @@ -46,7 +45,6 @@ impl ToValidationTokens for BytesRules {
});
let min_len = rules.min_len.map(|v| {
let v = v as usize;
let field = &ctx.name;
quote! {
if #name.len() < #v {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::MinLen(#v)));
Expand All @@ -55,30 +53,29 @@ impl ToValidationTokens for BytesRules {
});
let max_len = rules.max_len.map(|v| {
let v = v as usize;
let field = &ctx.name;
quote! {
if #name.len() > #v {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::MaxLen(#v)));
}
}
});
let pattern = rules.pattern.map(|v| {
let field = &ctx.name;
if let Err(err ) = regex::bytes::Regex::new(&v) {
panic!("{field}: Invalid regex pattern: {}", err);
if let Err(err) = regex::bytes::Regex::new(&v) {
panic!("{field}: Invalid regex pattern: {err}");
}
quote! {
let regex = ::regex::bytes::Regex::new(#v).map_err(|err| {
::prost_validate::Error::new(#field, format!("Invalid regex pattern: {}", err))
})?;
if !regex.is_match(#name.iter().as_slice()) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Pattern(#v.to_string())));
match ::regex::bytes::Regex::new(#v) {
Err(e) => return Err(::prost_validate::Error::new(#field, format!("Invalid regex pattern: {e}"))),
Ok(regex) => {
if !regex.is_match(#name.iter().as_slice()) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Pattern(#v.to_string())));
}
}
}
}
});
let prefix = rules.prefix.map(|v| {
let v = LitByteStr::new(v.as_slice(), Span::call_site());
let field = &ctx.name;
quote! {
if !#name.starts_with(#v) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Prefix(#v.to_vec())));
Expand All @@ -87,7 +84,6 @@ impl ToValidationTokens for BytesRules {
});
let suffix = rules.suffix.map(|v| {
let v = LitByteStr::new(v.as_slice(), Span::call_site());
let field = &ctx.name;
quote! {
if !#name.ends_with(#v) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Suffix(#v.to_vec())));
Expand All @@ -96,7 +92,6 @@ impl ToValidationTokens for BytesRules {
});
let contains = rules.contains.map(|v| {
let v = LitByteStr::new(v.as_slice(), Span::call_site());
let field = &ctx.name;
quote! {
if !::prost_validate::ValidateBytesExt::contains(&#name, #v.as_slice()) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Contains(#v.to_vec())));
Expand All @@ -109,7 +104,6 @@ impl ToValidationTokens for BytesRules {
.iter()
.map(|v| LitByteStr::new(v.as_slice(), Span::call_site()))
.collect::<Vec<_>>();
let field = &ctx.name;
quote! {
let values = [#(#v.to_vec()),*];
if !values.contains(&#name) {
Expand All @@ -123,7 +117,6 @@ impl ToValidationTokens for BytesRules {
.iter()
.map(|v| LitByteStr::new(v.as_slice(), Span::call_site()))
.collect::<Vec<_>>();
let field = &ctx.name;
quote! {
let values = [#(#v.to_vec()),*];
if values.contains(&#name) {
Expand All @@ -133,23 +126,20 @@ impl ToValidationTokens for BytesRules {
});
let well_known = rules.well_known.map(|v| match v {
bytes_rules::WellKnown::Ip(true) => {
let field = &ctx.name;
quote! {
if #name.len() != 4 && #name.len() != 16 {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Ip));
}
}
}
bytes_rules::WellKnown::Ipv4(true) => {
let field = &ctx.name;
quote! {
if #name.len() != 4 {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Ipv4));
}
}
}
bytes_rules::WellKnown::Ipv6(true) => {
let field = &ctx.name;
quote! {
if #name.len() != 16 {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::bytes::Error::Ipv6));
Expand Down
16 changes: 1 addition & 15 deletions prost-validate-derive-core/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub fn duration_to_tokens(name: &Ident, want: &Duration) -> (TokenStream, TokenS

impl ToValidationTokens for DurationRules {
fn to_validation_tokens(&self, ctx: &Context, name: &Ident) -> TokenStream {
let field = &ctx.name;
let rules = prost_validate_types::DurationRules::from(self.clone());
let r#const = rules.r#const.map(|v| v.as_duration()).map(|v| {
let (got, want) = duration_to_tokens(name, &v);
let field = &ctx.name;
quote! {
if #got != #want {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::duration::Error::Const(#want)));
Expand All @@ -46,7 +46,6 @@ impl ToValidationTokens for DurationRules {
let gte_lte = if let Some(lt) = rules.lt.map(|v| v.as_duration()) {
if let Some(gt) = rules.gt.map(|v| v.as_duration()) {
if lt > gt {
let field = &ctx.name;
let (val, lt) = duration_to_tokens(name, &lt);
let (_, gt) = duration_to_tokens(name, &gt);
quote! {
Expand All @@ -55,7 +54,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lt) = duration_to_tokens(name, &lt);
let (_, gt) = duration_to_tokens(name, &gt);
quote! {
Expand All @@ -66,7 +64,6 @@ impl ToValidationTokens for DurationRules {
}
} else if let Some(gte) = rules.gte.map(|v| v.as_duration()) {
if lt > gte {
let field = &ctx.name;
let (val, lt) = duration_to_tokens(name, &lt);
let (_, gte) = duration_to_tokens(name, &gte);
quote! {
Expand All @@ -75,7 +72,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lt) = duration_to_tokens(name, &lt);
let (_, gte) = duration_to_tokens(name, &gte);
quote! {
Expand All @@ -85,7 +81,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lt) = duration_to_tokens(name, &lt);
quote! {
if #val >= #lt {
Expand All @@ -96,7 +91,6 @@ impl ToValidationTokens for DurationRules {
} else if let Some(lte) = rules.lte.map(|v| v.as_duration()) {
if let Some(gt) = rules.gt.map(|v| v.as_duration()) {
if lte > gt {
let field = &ctx.name;
let (val, lte) = duration_to_tokens(name, &lte);
let (_, gt) = duration_to_tokens(name, &gt);
quote! {
Expand All @@ -105,7 +99,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lte) = duration_to_tokens(name, &lte);
let (_, gt) = duration_to_tokens(name, &gt);
quote! {
Expand All @@ -116,7 +109,6 @@ impl ToValidationTokens for DurationRules {
}
} else if let Some(gte) = rules.gte.map(|v| v.as_duration()) {
if lte > gte {
let field = &ctx.name;
let (val, lte) = duration_to_tokens(name, &lte);
let (_, gte) = duration_to_tokens(name, &gte);
quote! {
Expand All @@ -125,7 +117,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lte) = duration_to_tokens(name, &lte);
let (_, gte) = duration_to_tokens(name, &gte);
quote! {
Expand All @@ -135,7 +126,6 @@ impl ToValidationTokens for DurationRules {
}
}
} else {
let field = &ctx.name;
let (val, lte) = duration_to_tokens(name, &lte);
quote! {
if #val > #lte {
Expand All @@ -144,15 +134,13 @@ impl ToValidationTokens for DurationRules {
}
}
} else if let Some(gt) = rules.gt.map(|v| v.as_duration()) {
let field = &ctx.name;
let (val, gt) = duration_to_tokens(name, &gt);
quote! {
if #val <= #gt {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::duration::Error::Gt(#gt)));
}
}
} else if let Some(gte) = rules.gte.map(|v| v.as_duration()) {
let field = &ctx.name;
let (val, gte) = duration_to_tokens(name, &gte);
quote! {
if #val < #gte {
Expand All @@ -168,7 +156,6 @@ impl ToValidationTokens for DurationRules {
.iter()
.map(|v| v.as_duration())
.collect::<Vec<Duration>>();
let field = &ctx.name;
let (val, _) = duration_to_tokens(name, &vals[0]);
let vals = rules
.r#in
Expand All @@ -188,7 +175,6 @@ impl ToValidationTokens for DurationRules {
.iter()
.map(|v| v.as_duration())
.collect::<Vec<Duration>>();
let field = &ctx.name;
let (val, _) = duration_to_tokens(name, &vals[0]);
let vals = rules
.not_in
Expand Down
5 changes: 1 addition & 4 deletions prost-validate-derive-core/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct EnumRules {

impl ToValidationTokens for EnumRules {
fn to_validation_tokens(&self, ctx: &Context, name: &Ident) -> TokenStream {
let field = &ctx.name;
let rules = prost_validate_types::EnumRules::from(self.to_owned());
let r#const = rules.r#const.map(|v| {
let field = &ctx.name;
quote! {
if (*#name as i32) != #v {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::r#enum::Error::Const(#v)));
Expand All @@ -45,7 +45,6 @@ impl ToValidationTokens for EnumRules {
};
let enum_type: syn::Path = syn::parse_str(enumeration.as_str())
.expect("Invalid enum path");
let field = &ctx.name;
quote! {
if !#enum_type::is_valid(*#name) {
return Err(::prost_validate::Error::new(#field, ::prost_validate::errors::r#enum::Error::DefinedOnly));
Expand All @@ -54,7 +53,6 @@ impl ToValidationTokens for EnumRules {
});
let r#in = rules.r#in.is_empty().not().then(|| {
let v = rules.r#in.to_owned();
let field = &ctx.name;
quote! {
let values = [#(#v),*];
if !values.contains(&#name) {
Expand All @@ -64,7 +62,6 @@ impl ToValidationTokens for EnumRules {
});
let not_in = rules.not_in.is_empty().not().then(|| {
let v = rules.not_in.to_owned();
let field = &ctx.name;
quote! {
let values = [#(#v),*];
if values.contains(#name) {
Expand Down
Loading
Loading