-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NestedStrongType with custom_underlying
- Loading branch information
1 parent
72e6c45
commit 7a5b2f6
Showing
7 changed files
with
170 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,101 @@ | ||
use syn::{Data, DeriveInput, Type}; | ||
|
||
#[repr(u8)] | ||
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] | ||
pub(crate) enum UnderlyingTypeGroup { | ||
Int, | ||
Float, | ||
UInt, | ||
Bool, | ||
Char, | ||
String, | ||
pub(crate) enum UnderlyingType { | ||
Primitive, | ||
Derived, | ||
} | ||
pub(crate) enum ValueTypeGroup { | ||
Int(UnderlyingType), | ||
Float(UnderlyingType), | ||
UInt(UnderlyingType), | ||
Bool(UnderlyingType), | ||
Char(UnderlyingType), | ||
String(UnderlyingType), | ||
} | ||
|
||
pub(crate) struct TypeInfo<'a> { | ||
pub value_type: &'a syn::Ident, | ||
pub type_group: ValueTypeGroup, | ||
} | ||
|
||
pub(crate) fn get_type_ident(input: &DeriveInput) -> &syn::Ident { | ||
fn get_type_ident(input: &DeriveInput) -> Option<&syn::Ident> { | ||
if let Data::Struct(ref data_struct) = input.data { | ||
if let Type::Path(ref path) = &data_struct.fields.iter().next().unwrap().ty { | ||
return &path.path.segments.last().unwrap().ident; | ||
return Some(&path.path.segments.last().unwrap().ident); | ||
} | ||
} | ||
None | ||
} | ||
|
||
fn get_group_from_custom_underlying(input: &DeriveInput) -> Option<ValueTypeGroup> { | ||
for attr in input.attrs.iter() { | ||
if attr.path().is_ident("custom_underlying") { | ||
let mut type_group = None; | ||
attr.parse_nested_meta(|meta| { | ||
if let Some(ident) = meta.path.get_ident() { | ||
type_group = Some(get_type_group(ident, UnderlyingType::Derived)); | ||
Ok(()) | ||
} else { | ||
Err(meta.error("Unsupported attribute")) | ||
} | ||
}) | ||
.ok()?; | ||
return type_group; | ||
} | ||
} | ||
panic!("Unsupported input") | ||
|
||
None | ||
} | ||
|
||
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" | ||
pub(crate) fn get_type(input: &DeriveInput) -> TypeInfo { | ||
if let Some(value_type) = get_type_ident(input) { | ||
let type_group = match get_group_from_custom_underlying(input) { | ||
Some(type_group) => type_group, | ||
None => get_type_group(value_type, UnderlyingType::Primitive), | ||
}; | ||
|
||
TypeInfo { | ||
value_type, | ||
type_group, | ||
} | ||
} else { | ||
panic!("Unsupported input") | ||
} | ||
} | ||
|
||
pub(crate) fn get_type_group( | ||
underlying_type: &syn::Ident, | ||
value_type: UnderlyingType, | ||
) -> ValueTypeGroup { | ||
if underlying_type == "i8" | ||
|| underlying_type == "i16" | ||
|| underlying_type == "i32" | ||
|| underlying_type == "i64" | ||
|| underlying_type == "i128" | ||
|| underlying_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 ValueTypeGroup::Int(value_type); | ||
} | ||
if underlying_type == "u8" | ||
|| underlying_type == "u16" | ||
|| underlying_type == "u32" | ||
|| underlying_type == "u64" | ||
|| underlying_type == "u128" | ||
|| underlying_type == "usize" | ||
{ | ||
return UnderlyingTypeGroup::UInt; | ||
return ValueTypeGroup::UInt(value_type); | ||
} | ||
if value_type == "f32" || value_type == "f64" { | ||
return UnderlyingTypeGroup::Float; | ||
if underlying_type == "f32" || underlying_type == "f64" { | ||
return ValueTypeGroup::Float(value_type); | ||
} | ||
if value_type == "bool" { | ||
return UnderlyingTypeGroup::Bool; | ||
if underlying_type == "bool" { | ||
return ValueTypeGroup::Bool(value_type); | ||
} | ||
if value_type == "char" { | ||
return UnderlyingTypeGroup::Char; | ||
if underlying_type == "char" { | ||
return ValueTypeGroup::Char(value_type); | ||
} | ||
if value_type == "String" { | ||
return UnderlyingTypeGroup::String; | ||
if underlying_type == "String" { | ||
return ValueTypeGroup::String(value_type); | ||
} | ||
panic!("Unsupported type: {}", value_type); | ||
panic!("Unsupported type: {}", underlying_type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use std::mem; | ||
use strong_type::StrongType; | ||
|
||
fn test_type<T: std::fmt::Debug + Clone + Send + Sync + Default + PartialEq>() {} | ||
|
||
#[test] | ||
fn test_custom_underlying() { | ||
#[derive(StrongType)] | ||
#[strong_type(auto_operators)] | ||
struct Dollar(i32); | ||
|
||
#[derive(StrongType)] | ||
#[strong_type(auto_operators)] | ||
#[custom_underlying(i32)] | ||
struct Cash(Dollar); | ||
test_type::<Cash>(); | ||
assert_eq!(mem::size_of::<Cash>(), mem::size_of::<i32>()); | ||
|
||
assert_eq!( | ||
Cash::new(Dollar::new(10)), | ||
Cash::new(Dollar::new(2)) + Cash::new(Dollar::new(8)) | ||
); | ||
|
||
assert_eq!( | ||
format!("{}", Cash::new(Dollar::new(10))), | ||
"Cash(Dollar(10))" | ||
); | ||
|
||
#[derive(StrongType)] | ||
#[custom_underlying(i32)] | ||
struct Coin(Cash); | ||
test_type::<Coin>(); | ||
assert_eq!(mem::size_of::<Coin>(), mem::size_of::<i32>()); | ||
assert_eq!( | ||
format!("{}", Coin::new(Cash::new(Dollar::new(10)))), | ||
"Coin(Cash(Dollar(10)))" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod auto_operators; | ||
mod custom_underlying; | ||
mod display; | ||
mod strong_type; |