refactor(units): use derive macro

This commit is contained in:
2025-07-07 22:16:24 +03:00
parent 7d58d1b74c
commit fe3ddf165e
7 changed files with 405 additions and 103 deletions

View File

@@ -1,8 +1,9 @@
use super::unit::{Unit, UnitError};
use crate::app::utils::unit::UnitDisplay;
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 {
@@ -17,35 +18,3 @@ impl Display for FileSize {
write!(f, "{}", UnitDisplay::new(&self.0, UNITS))
}
}
macro_rules! impl_from_unsigned {
($type:ty, $($t:ty), *) => {
$(
impl From<$t> for $type {
fn from(value: $t) -> Self {
Self(Unit::from(value))
}
}
)*
};
}
macro_rules! impl_try_from_signed {
($type:ty, $error:ty, $($t:ty), *) => {
$(
impl TryFrom<$t> for $type {
type Error = $error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
if value < 0 {
return Err(UnitError::NegativeValue { value: value as i64 });
}
Ok(Self(Unit::try_from(value)?))
}
}
)*
};
}
impl_from_unsigned!(FileSize, u8, u16, u32, u64, usize);
impl_try_from_signed!(FileSize, UnitError, i8, i16, i32, i64, isize);

View File

@@ -1,8 +1,9 @@
use super::unit::{Unit, UnitError};
use crate::app::utils::unit::UnitDisplay;
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 {
@@ -17,35 +18,3 @@ impl Display for NetSpeed {
write!(f, "{}", UnitDisplay::new(&self.0, UNITS))
}
}
macro_rules! impl_from_unsigned {
($type:ty, $($t:ty), *) => {
$(
impl From<$t> for $type {
fn from(value: $t) -> Self {
Self(Unit::from(value))
}
}
)*
};
}
macro_rules! impl_try_from_signed {
($type:ty, $error:ty, $($t:ty), *) => {
$(
impl TryFrom<$t> for $type {
type Error = $error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
if value < 0 {
return Err(UnitError::NegativeValue { value: value as i64 });
}
Ok(Self(Unit::try_from(value)?))
}
}
)*
};
}
impl_from_unsigned!(NetSpeed, u8, u16, u32, u64, usize);
impl_try_from_signed!(NetSpeed, UnitError, i8, i16, i32, i64, isize);

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 {
@@ -24,6 +26,7 @@ impl Unit {
}
}
#[derive(Debug)]
pub struct UnitDisplay<'a> {
unit: &'a Unit,
units: &'a [&'a str],
@@ -55,35 +58,3 @@ impl<'a> Display for UnitDisplay<'a> {
write!(f, "{:.2} {}", size, self.units[unit_index])
}
}
macro_rules! impl_from_unsigned {
($type:ty, $($t:ty), *) => {
$(
impl From<$t> for $type {
fn from(value: $t) -> Self {
Self(value as u64)
}
}
)*
};
}
macro_rules! impl_try_from_signed {
($type:ty, $error:ty, $($t:ty), *) => {
$(
impl TryFrom<$t> for $type {
type Error = $error;
fn try_from(value: $t) -> Result<Self, Self::Error> {
if value < 0 {
return Err(UnitError::NegativeValue { value: value as i64 });
}
Ok(Self(value as u64))
}
}
)*
};
}
impl_from_unsigned!(Unit, u8, u16, u32, u64, usize);
impl_try_from_signed!(Unit, UnitError, i8, i16, i32, i64, isize);