Skip to content

Commit

Permalink
Error message for invalid structs (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 6, 2023
1 parent 0a1247e commit 8155b5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ use crate::detail::{
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
use syn::{Data, DeriveInput};

fn is_struct_valid(input: &DeriveInput) -> bool {
if let Data::Struct(data_struct) = &input.data {
if let syn::Fields::Unnamed(fields_unnamed) = &data_struct.fields {
return fields_unnamed.unnamed.len() == 1;
}
}
false
}

pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> TokenStream {
if !is_struct_valid(&input) {
panic!("Strong type must be a tuple struct with one field.");
}

let name = &input.ident;
let value_type = get_type_ident(&input);
let group = get_type_group(value_type);
Expand Down
2 changes: 1 addition & 1 deletion tests/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod tests {
struct NamedF64(f64);
let _ = NamedF64::new(1.0);

#[derive(StrongType)]
#[derive(StrongNumericType)]
struct NamedBool(bool);
let _ = NamedBool::new(true);

Expand Down

0 comments on commit 8155b5e

Please sign in to comment.