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

simplify proc macro validation #85

Merged
merged 1 commit into from
Jul 8, 2023
Merged
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
27 changes: 5 additions & 22 deletions ssz-rs-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,21 +463,12 @@ fn is_valid_none_identifier(ident: &Ident) -> bool {
*ident == format_ident!("None")
}

// Refers to the validation state of proc macro's input
enum ValidationState<'a> {
Unvalidated(&'a Data),
Validated(&'a Data),
}

// Validates the incoming data follows the rules
// for mapping the Rust term to something that can
// implement the `SimpleSerialize` trait.
fn validate_derive_data(data: ValidationState) -> ValidationState {
let data = match data {
ValidationState::Unvalidated(data) => data,
data @ ValidationState::Validated(..) => return data,
};

//
// Panics if validation fails which aborts the macro derivation.
fn validate_derive_data(data: &Data) {
match data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields) => {
Expand Down Expand Up @@ -535,22 +526,14 @@ fn validate_derive_data(data: ValidationState) -> ValidationState {
}
Data::Union(..) => panic!("Rust unions cannot produce valid SSZ types"),
}

ValidationState::Validated(data)
}

#[proc_macro_derive(SimpleSerialize)]
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);

let data = ValidationState::Unvalidated(&input.data);

let data = &validate_derive_data(data);

let data = match *data {
ValidationState::Validated(data) => data,
ValidationState::Unvalidated(..) => panic!("do not process unvalidated input"),
};
let data = &input.data;
validate_derive_data(data);

let name = &input.ident;
let generics = &input.generics;
Expand Down