diff --git a/examples/Eq_example.rs b/examples/Eq_example.rs new file mode 100644 index 0000000..fdea5b2 --- /dev/null +++ b/examples/Eq_example.rs @@ -0,0 +1,34 @@ +use tagged_core::Tagged; + +fn main() { +} +#[test] +fn test_eq() { + let a = Tagged::::new(42); + let b = Tagged::::new(42); + let c = Tagged::::new(35); + let d = Tagged::::new(0); + + assert!(a == b); + assert!(a > c); + assert!(a >= d); + +} + +#[test] +fn test_with_struct() { + use tagged_core::Tagged; + + struct UserIdTag { + a: Tagged, + b: Tagged, + } + + + let instance = UserIdTag{a: 1.into(), b: 2.into()}; + + assert!(instance.a < instance.b); + + // println!("{}", a); + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d0730d9..5ee3a72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ -pub mod rust_tagged{ - pub use tagged_core::*; -} \ No newline at end of file +// pub mod rust_tagged{ +// pub use tagged_core::*; +// } + +pub use tagged_core::*; diff --git a/tagged-core/examples/Eq_example.rs b/tagged-core/examples/Eq_example.rs new file mode 100644 index 0000000..0fd41fc --- /dev/null +++ b/tagged-core/examples/Eq_example.rs @@ -0,0 +1,32 @@ +use tagged_core::Tagged; +fn main() {} +#[test] +fn test_eq() { + let a = Tagged::::new(42); + let b = Tagged::::new(42); + let c = Tagged::::new(35); + let d = Tagged::::new(0); + + assert!(a == b); + assert!(a > c); + assert!(a >= d); + +} + +#[test] +fn test_with_struct() { + use tagged_core::Tagged; + + struct UserIdTag { + a: Tagged, + b: Tagged, + } + + + let instance = UserIdTag{a: 1.into(), b: 2.into()}; + + assert!(instance.a < instance.b); + + // println!("{}", a); + +} \ No newline at end of file diff --git a/tagged-core/src/lib.rs b/tagged-core/src/lib.rs index aaaf002..4577924 100644 --- a/tagged-core/src/lib.rs +++ b/tagged-core/src/lib.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::ops::Deref; /// rust-tagged provides a simple way to define strongly typed wrappers over primitive types like String, i32, Uuid, chrono::DateTime, etc. It helps eliminate bugs caused by misusing raw primitives for conceptually distinct fields such as UserId, Email, ProductId, and more. /// @@ -8,7 +9,7 @@ use std::ops::Deref; /// # Example /// /// ``` -/// use rust_tagged::{Tagged}; +/// use tagged_core::{Tagged}; /// /// #[derive(Debug)] /// struct EmailTag; @@ -24,12 +25,12 @@ use std::ops::Deref; /// println!("Raw String: {raw}"); /// } /// ``` -pub struct Tagged { +pub struct Tagged { value: T, - _marker: std::marker::PhantomData, + _marker: std::marker::PhantomData, } -impl Tagged { +impl Tagged { pub fn new(value: T) -> Self { Self { value, @@ -43,41 +44,60 @@ impl Tagged { } -/// Blanket `From` for `Tagged` -impl From for Tagged { +/// Blanket `From` for `Tagged` +impl From for Tagged { fn from(value: T) -> Self { Tagged::new(value) } } -/// Support `From<&str>` → `Tagged` -impl From<&str> for Tagged { +/// Support `From<&str>` → `Tagged` +impl From<&str> for Tagged { fn from(s: &str) -> Self { Tagged::new(s.to_string()) } } -/// Support `From<&String>` → `Tagged` -impl From<&String> for Tagged { +/// Support `From<&String>` → `Tagged` +impl From<&String> for Tagged { fn from(s: &String) -> Self { Tagged::new(s.clone()) } } -impl Deref for Tagged { +impl Deref for Tagged { type Target = T; fn deref(&self) -> &Self::Target { &self.value } } +impl PartialEq for Tagged { + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + +impl Eq for Tagged {} + +impl PartialOrd for Tagged { + fn partial_cmp(&self, other: &Self) -> Option { + self.value.partial_cmp(&other.value) + } +} + +impl Ord for Tagged { + fn cmp(&self, other: &Self) -> Ordering { + self.value.cmp(&other.value) + } +} // For all common primitive types // macro_rules! impl_from_tagged { // ($($t:ty),*) => { // $( -// impl From> for $t { -// fn from(tagged: Tagged<$t, U>) -> Self { +// impl From> for $t { +// fn from(tagged: Tagged<$t, Tag>) -> Self { // tagged.value // } // } @@ -111,4 +131,5 @@ mod tests { assert_eq!(tagged_struct.id.value, 0); } -} \ No newline at end of file +} +