Skip to content

Commit

Permalink
Replace to_string matching with eq in get_type_group (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 5, 2023
1 parent 4e4fc29 commit c4fbb7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let y = Second::new(3);
let z = Minute::new(3);

assert_eq!(x.type_id(), y.type_id()); // Same type: Second
assert_ne!(y.type_id(), z.type_id()); // Different type: Second versus Minute
assert_ne!(y.type_id(), z.type_id()); // Different types: Second versus Minute
```

#### Named type with arithmetic operations:
Expand Down
2 changes: 1 addition & 1 deletion src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ pub(crate) use min_max::implement_min_max;
pub(crate) use nan::implement_nan;
pub(crate) use negate::implement_negate;
pub(crate) use not::implement_not;
pub(crate) use underlying_type::{get_type, get_type_ident, UnderlyingType};
pub(crate) use underlying_type::{get_type_group, get_type_ident, UnderlyingTypeGroup};
42 changes: 32 additions & 10 deletions src/detail/underlying_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use syn::{Data, DeriveInput, Type};

#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)]
pub(crate) enum UnderlyingType {
pub(crate) enum UnderlyingTypeGroup {
Int,
Float,
UInt,
Expand All @@ -20,14 +20,36 @@ pub(crate) fn get_type_ident(input: &DeriveInput) -> &syn::Ident {
panic!("Unsupported input")
}

pub(crate) fn get_type(value_type: &syn::Ident) -> UnderlyingType {
match value_type.to_string().as_str() {
"i8" | "i16" | "i32" | "i64" | "i128" | "isize" => UnderlyingType::Int,
"u8" | "u16" | "u32" | "u64" | "u128" | "usize" => UnderlyingType::UInt,
"f32" | "f64" => UnderlyingType::Float,
"bool" => UnderlyingType::Bool,
"char" => UnderlyingType::Char,
"String" => UnderlyingType::String,
_ => panic!("Unsupported type"),
pub(crate) fn get_type_group(value_type: &syn::Ident) -> UnderlyingTypeGroup {
if value_type == "i8"
|| value_type == "i16"
|| value_type == "i32"
|| value_type == "i64"
|| value_type == "i128"
|| value_type == "isize"
{
return UnderlyingTypeGroup::Int;
}
if value_type == "u8"
|| value_type == "u16"
|| value_type == "u32"
|| value_type == "u64"
|| value_type == "u128"
|| value_type == "usize"
{
return UnderlyingTypeGroup::UInt;
}
if value_type == "f32" || value_type == "f64" {
return UnderlyingTypeGroup::Float;
}
if value_type == "bool" {
return UnderlyingTypeGroup::Bool;
}
if value_type == "char" {
return UnderlyingTypeGroup::Char;
}
if value_type == "String" {
return UnderlyingTypeGroup::String;
}
panic!("Unsupported type: {}", value_type);
}
20 changes: 10 additions & 10 deletions src/strong_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::detail::{
custom_display, get_type, get_type_ident, implement_arithmetic, implement_basic,
custom_display, get_type_group, get_type_ident, implement_arithmetic, implement_basic,
implement_basic_primitive, implement_basic_string, implement_display, implement_hash,
implement_min_max, implement_nan, implement_negate, implement_not, UnderlyingType,
implement_min_max, implement_nan, implement_negate, implement_not, UnderlyingTypeGroup,
};
use proc_macro2::TokenStream;
use quote::quote;
Expand All @@ -10,7 +10,7 @@ use syn::DeriveInput;
pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> TokenStream {
let name = &input.ident;
let value_type = get_type_ident(&input);
let group = get_type(value_type);
let group = get_type_group(value_type);

let mut ast = quote!();
ast.extend(implement_basic(name));
Expand All @@ -19,38 +19,38 @@ pub(super) fn expand_strong_type(input: DeriveInput, impl_arithmetic: bool) -> T
};

match &group {
UnderlyingType::Int | UnderlyingType::UInt => {
UnderlyingTypeGroup::Int | UnderlyingTypeGroup::UInt => {
ast.extend(implement_basic_primitive(name, value_type));
ast.extend(implement_min_max(name, value_type));
ast.extend(implement_hash(name));
}
UnderlyingType::Float => {
UnderlyingTypeGroup::Float => {
ast.extend(implement_basic_primitive(name, value_type));
ast.extend(implement_min_max(name, value_type));
ast.extend(implement_nan(name, value_type));
}
UnderlyingType::Bool => {
UnderlyingTypeGroup::Bool => {
ast.extend(implement_basic_primitive(name, value_type));
ast.extend(implement_not(name));
ast.extend(implement_hash(name));
}
UnderlyingType::Char => {
UnderlyingTypeGroup::Char => {
ast.extend(implement_basic_primitive(name, value_type));
ast.extend(implement_hash(name));
}
UnderlyingType::String => {
UnderlyingTypeGroup::String => {
ast.extend(implement_basic_string(name));
ast.extend(implement_hash(name));
}
}

if impl_arithmetic {
match &group {
UnderlyingType::Int | UnderlyingType::Float => {
UnderlyingTypeGroup::Int | UnderlyingTypeGroup::Float => {
ast.extend(implement_arithmetic(name));
ast.extend(implement_negate(name));
}
UnderlyingType::UInt => {
UnderlyingTypeGroup::UInt => {
ast.extend(implement_arithmetic(name));
}
_ => panic!("Non-arithmetic type {value_type}"),
Expand Down

0 comments on commit c4fbb7c

Please sign in to comment.