-
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.
Refactor to improve readability (#5)
- Loading branch information
1 parent
aa89e54
commit 00e5c4f
Showing
17 changed files
with
387 additions
and
298 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_arithmetic(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::ops::Add for #name { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: Self) -> Self::Output { | ||
Self(self.value() + rhs.value()) | ||
} | ||
} | ||
|
||
impl std::ops::AddAssign for #name { | ||
fn add_assign(&mut self, rhs: Self) { | ||
self.0 += rhs.value() | ||
} | ||
} | ||
|
||
impl std::ops::Sub for #name { | ||
type Output = Self; | ||
fn sub(self, rhs: Self) -> Self::Output { | ||
Self(self.value() - rhs.value()) | ||
} | ||
} | ||
|
||
impl std::ops::SubAssign for #name { | ||
fn sub_assign(&mut self, rhs: Self) { | ||
self.0 -= rhs.value() | ||
} | ||
} | ||
|
||
impl std::ops::Mul for #name { | ||
type Output = Self; | ||
fn mul(self, rhs: Self) -> Self::Output { | ||
Self(self.value() * rhs.value()) | ||
} | ||
} | ||
|
||
impl std::ops::MulAssign for #name { | ||
fn mul_assign(&mut self, rhs: Self) { | ||
self.0 *= rhs.value() | ||
} | ||
} | ||
|
||
impl std::ops::Div for #name { | ||
type Output = Self; | ||
fn div(self, rhs: Self) -> Self::Output { | ||
Self(self.value() / rhs.value()) | ||
} | ||
} | ||
|
||
impl std::ops::DivAssign for #name { | ||
fn div_assign(&mut self, rhs: Self) { | ||
self.0 /= rhs.value() | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_basic(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::fmt::Debug for #name { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
f.debug_struct(stringify!(#name)) | ||
.field("value", &self.0) | ||
.finish() | ||
} | ||
} | ||
|
||
impl std::cmp::PartialEq for #name { | ||
fn eq(&self, rhs: &Self) -> bool { | ||
self.value() == rhs.value() | ||
} | ||
} | ||
} | ||
} |
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,31 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_basic_primitive(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl #name { | ||
pub fn new(value : #value_type) -> Self { | ||
Self(value) | ||
} | ||
|
||
pub fn value(&self) -> #value_type { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Copy for #name {} | ||
|
||
impl Clone for #name { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
#[allow(clippy::incorrect_partial_ord_impl_on_ord_type)] | ||
impl std::cmp::PartialOrd for #name { | ||
fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> { | ||
self.value().partial_cmp(&rhs.value()) | ||
} | ||
} | ||
} | ||
} |
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,28 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_basic_string(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl #name { | ||
pub fn new(value : &str) -> Self { | ||
Self(String::from(value)) | ||
} | ||
|
||
pub fn value(&self) -> &str { | ||
self.0.as_str() | ||
} | ||
} | ||
|
||
impl Clone for #name { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone()) | ||
} | ||
} | ||
|
||
impl std::cmp::PartialOrd for #name { | ||
fn partial_cmp(&self, rhs: &Self) -> Option<std::cmp::Ordering> { | ||
Some(self.value().cmp(&rhs.value())) | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
|
||
pub(crate) fn implement_display(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::fmt::Display for #name { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "{}({})", stringify!(#name), &self.0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn custom_display(input: &DeriveInput) -> bool { | ||
input | ||
.attrs | ||
.iter() | ||
.any(|attr| attr.path().is_ident("custom_display")) | ||
} |
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,20 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_hash(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::cmp::Eq for #name {} | ||
|
||
impl std::cmp::Ord for #name { | ||
fn cmp(&self, rhs: &Self) -> std::cmp::Ordering { | ||
self.value().cmp(&rhs.value()) | ||
} | ||
} | ||
|
||
impl std::hash::Hash for #name { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.value().hash(state); | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_min_max(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl #name { | ||
fn min() -> Self { | ||
Self(#value_type::MIN) | ||
} | ||
|
||
fn max() -> Self { | ||
Self(#value_type::MAX) | ||
} | ||
} | ||
} | ||
} |
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,23 @@ | ||
mod arithmetic; | ||
mod basic; | ||
mod basic_primitive; | ||
mod basic_string; | ||
mod display; | ||
mod hash; | ||
mod min_max; | ||
mod nan; | ||
mod negate; | ||
mod not; | ||
mod underlying_type; | ||
|
||
pub(crate) use arithmetic::implement_arithmetic; | ||
pub(crate) use basic::implement_basic; | ||
pub(crate) use basic_primitive::implement_basic_primitive; | ||
pub(crate) use basic_string::implement_basic_string; | ||
pub(crate) use display::{custom_display, implement_display}; | ||
pub(crate) use hash::implement_hash; | ||
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}; |
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,12 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_nan(name: &syn::Ident, value_type: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl #name { | ||
fn nan() -> Self { | ||
Self(#value_type::NAN) | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_negate(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::ops::Neg for #name { | ||
type Output = Self; | ||
|
||
fn neg(self) -> Self::Output { | ||
Self(-self.value()) | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn implement_not(name: &syn::Ident) -> TokenStream { | ||
quote! { | ||
impl std::ops::Not for #name { | ||
type Output = Self; | ||
|
||
fn not(self) -> Self::Output { | ||
#name(!self.value()) | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
use syn::{Data, DeriveInput, Type}; | ||
|
||
#[repr(u8)] | ||
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] | ||
pub(crate) enum UnderlyingType { | ||
Int, | ||
Float, | ||
UInt, | ||
Bool, | ||
Char, | ||
String, | ||
} | ||
|
||
pub(crate) fn get_type_ident(input: &DeriveInput) -> &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; | ||
} | ||
} | ||
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"), | ||
} | ||
} |
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
Oops, something went wrong.