Skip to content

Commit

Permalink
Enforce private fields (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 6, 2023
1 parent 8155b5e commit b88163a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/strong_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ use crate::detail::{
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput};
use syn::{Data, DeriveInput, Fields, Visibility};

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;
if let Fields::Unnamed(fields_unnamed) = &data_struct.fields {
return (fields_unnamed.unnamed.len() == 1)
&& matches!(
fields_unnamed.unnamed.first().unwrap().vis,
Visibility::Inherited
);
}
}
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.");
panic!("Strong type must be a tuple struct with one private field.");
}

let name = &input.ident;
Expand Down

0 comments on commit b88163a

Please sign in to comment.