mirror of
https://github.com/kristoferssolo/traxor.git
synced 2026-02-04 06:42:04 +00:00
refactor(units): use derive macro
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user