Revert "refactor(units): remove macros"

This reverts commit 2d5b537821f0c3ccdcd327aa612e848d0af2d0d8.
This commit is contained in:
2025-07-07 22:47:18 +03:00
parent 20b9eb0e58
commit c54647c877
5 changed files with 19 additions and 50 deletions

View File

@@ -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<u64> for FileSize {
fn from(value: u64) -> Self {
Self(Unit::from(value))
}
}
impl TryFrom<i64> for FileSize {
type Error = UnitError;
fn try_from(value: i64) -> Result<Self, Self::Error> {
Ok(Self(Unit::try_from(value)?))
}
}

View File

@@ -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<u64> for NetSpeed {
fn from(value: u64) -> Self {
Self(Unit::from(value))
}
}
impl TryFrom<i64> for NetSpeed {
type Error = UnitError;
fn try_from(value: i64) -> Result<Self, Self::Error> {
Ok(Self(Unit::try_from(value)?))
}
}

View File

@@ -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<u64> for Unit {
fn from(value: u64) -> Self {
Self(value)
}
}
impl TryFrom<i64> for Unit {
type Error = UnitError;
fn try_from(value: i64) -> Result<Self, Self::Error> {
if value < 0 {
return Err(UnitError::NegativeValue { value });
}
Ok(Self(value as u64))
}
}