Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework string length rules #86

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions garde/src/rules/byte_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use super::AsStr;
use crate::error::Error;

#[deprecated = "the `byte_length` attribute is deprecated. Use `length` instead. (See https://github.com/jprochazk/garde/issues/84)"]
pub fn apply<T: ByteLength>(v: &T, (min, max): (usize, usize)) -> Result<(), Error> {
if let Err(e) = v.validate_byte_length(min, max) {
match e {
Expand Down
92 changes: 92 additions & 0 deletions garde_derive/src/check_deprecated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote_spanned, ToTokens};

use crate::model::{self, RawRule};

const BYTE_LENGTH_DEPRECATED: &str = "the `byte_length` attribute is deprecated. Use `length` instead. (See https://github.com/jprochazk/garde/issues/84)";

pub struct DeprecatedWarningSpans {
input_ident_name: String,
spans: Vec<Span>,
}

pub fn check(input: &model::Input) -> DeprecatedWarningSpans {
let model::Input { ident, kind, .. } = input;

let mut spans = Vec::new();
dpytaylo marked this conversation as resolved.
Show resolved Hide resolved
match kind {
model::InputKind::Struct(variant) => {
spans.append(&mut check_variant(variant));
}
model::InputKind::Enum(list) => {
for (_, variant) in list {
if let Some(variant) = variant {
spans.append(&mut check_variant(variant));
}
}
}
};

DeprecatedWarningSpans {
input_ident_name: ident.to_string(),
spans,
}
}

fn check_variant(variant: &model::Variant) -> Vec<Span> {
let mut spans = Vec::new();

match variant {
model::Variant::Struct(map) => {
for field in map.values() {
spans.append(&mut check_field(field));
}
}
model::Variant::Tuple(list) => {
for field in list {
spans.append(&mut check_field(field));
}
}
};

spans
}

fn check_field(field: &model::Field) -> Vec<Span> {
let model::Field {
rules: raw_rules, ..
} = field;

let mut spans = Vec::new();
for RawRule { span, kind } in raw_rules {
if let model::RawRuleKind::ByteLength(_) = kind {
spans.push(*span);
}
}

spans
}
dpytaylo marked this conversation as resolved.
Show resolved Hide resolved

impl ToTokens for DeprecatedWarningSpans {
fn to_tokens(&self, tokens: &mut TokenStream2) {
for (i, &span) in self.spans.iter().enumerate() {
let name = Ident::new(
&format!("__{}_byte_length_deprecated_{i}", self.input_ident_name),
Span::call_site(),
);

// Inspired by:
// https://github.com/ggwpez/proc-macro-warning/blob/970809f551eb78ea003006ef4da0c303ede8501d/proc-macro-warning/src/lib.rs#L260
quote_spanned! {span=>
#[allow(dead_code)]
fn #name() {
#[deprecated(note = #BYTE_LENGTH_DEPRECATED)]
#[allow(non_upper_case_globals)]
const _w: () = ();
let _ = _w;
}
}
.to_tokens(tokens);
}
}
}
11 changes: 9 additions & 2 deletions garde_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod check;
mod check_deprecated;
mod emit;
mod model;
mod syntax;
mod util;

use proc_macro::{Delimiter, Literal, Span, TokenStream, TokenTree};
use quote::quote;
use quote::{quote, ToTokens, TokenStreamExt};
use syn::DeriveInput;

#[proc_macro_derive(Validate, attributes(garde))]
Expand All @@ -15,11 +16,17 @@ pub fn derive_validate(input: TokenStream) -> TokenStream {
Ok(v) => v,
Err(e) => return e.into_compile_error().into(),
};

let deprecated = check_deprecated::check(&input);

let input = match check::check(input) {
Ok(v) => v,
Err(e) => return e.into_compile_error().into(),
};
emit::emit(input).into()

let mut stream = emit::emit(input);
stream.append_all(deprecated.into_token_stream());
stream.into()
}

#[proc_macro]
Expand Down