From c54647c877096074a130f610eba98704b4201e01 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 7 Jul 2025 22:47:18 +0300 Subject: [PATCH] Revert "refactor(units): remove macros" This reverts commit 2d5b537821f0c3ccdcd327aa612e848d0af2d0d8. --- derive_macro/src/lib.rs | 7 +++++++ derive_macro/src/merge.rs | 1 + src/app/utils/filesize.rs | 20 ++++---------------- src/app/utils/netspeed.rs | 20 ++++---------------- src/app/utils/unit.rs | 21 +++------------------ 5 files changed, 19 insertions(+), 50 deletions(-) diff --git a/derive_macro/src/lib.rs b/derive_macro/src/lib.rs index 410171d..5a8bc98 100644 --- a/derive_macro/src/lib.rs +++ b/derive_macro/src/lib.rs @@ -1,4 +1,5 @@ mod merge; +mod unit; use proc_macro::TokenStream; use syn::{DeriveInput, parse_macro_input}; @@ -8,3 +9,9 @@ pub fn merge_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); merge::impl_merge_derive(input) } + +#[proc_macro_derive(UnitConversions, attributes(units, error))] +pub fn unit_derive(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + unit::impl_unit_conversions(input) +} diff --git a/derive_macro/src/merge.rs b/derive_macro/src/merge.rs index 83c1fc1..bd682f3 100644 --- a/derive_macro/src/merge.rs +++ b/derive_macro/src/merge.rs @@ -55,3 +55,4 @@ pub fn impl_merge_derive(input: DeriveInput) -> TokenStream { expanded.into() } + diff --git a/src/app/utils/filesize.rs b/src/app/utils/filesize.rs index ee51399..b83a451 100644 --- a/src/app/utils/filesize.rs +++ b/src/app/utils/filesize.rs @@ -1,7 +1,9 @@ -use super::unit::{Unit, UnitDisplay, UnitError}; +use super::unit::{Unit, UnitDisplay}; +use derive_macro::UnitConversions; use std::fmt::Display; -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)] +#[error(UnitError)] pub struct FileSize(Unit); impl FileSize { @@ -16,17 +18,3 @@ impl Display for FileSize { write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) } } - -impl From for FileSize { - fn from(value: u64) -> Self { - Self(Unit::from(value)) - } -} - -impl TryFrom for FileSize { - type Error = UnitError; - - fn try_from(value: i64) -> Result { - Ok(Self(Unit::try_from(value)?)) - } -} diff --git a/src/app/utils/netspeed.rs b/src/app/utils/netspeed.rs index 5355f85..fe99b2e 100644 --- a/src/app/utils/netspeed.rs +++ b/src/app/utils/netspeed.rs @@ -1,7 +1,9 @@ -use super::unit::{Unit, UnitDisplay, UnitError}; +use super::unit::{Unit, UnitDisplay}; +use derive_macro::UnitConversions; use std::fmt::Display; -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)] +#[error(UnitError)] pub struct NetSpeed(Unit); impl NetSpeed { @@ -16,17 +18,3 @@ impl Display for NetSpeed { write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) } } - -impl From for NetSpeed { - fn from(value: u64) -> Self { - Self(Unit::from(value)) - } -} - -impl TryFrom for NetSpeed { - type Error = UnitError; - - fn try_from(value: i64) -> Result { - Ok(Self(Unit::try_from(value)?)) - } -} diff --git a/src/app/utils/unit.rs b/src/app/utils/unit.rs index 2704429..1fa9e82 100644 --- a/src/app/utils/unit.rs +++ b/src/app/utils/unit.rs @@ -1,3 +1,4 @@ +use derive_macro::UnitConversions; use std::fmt::Display; use thiserror::Error; @@ -11,7 +12,8 @@ pub enum UnitError { InvalidValue { reason: String }, } -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)] +#[error(UnitError)] pub struct Unit(u64); impl Unit { @@ -56,20 +58,3 @@ impl<'a> Display for UnitDisplay<'a> { write!(f, "{:.2} {}", size, self.units[unit_index]) } } - -impl From for Unit { - fn from(value: u64) -> Self { - Self(value) - } -} - -impl TryFrom for Unit { - type Error = UnitError; - - fn try_from(value: i64) -> Result { - if value < 0 { - return Err(UnitError::NegativeValue { value }); - } - Ok(Self(value as u64)) - } -}