mirror of
https://github.com/kristoferssolo/traxor.git
synced 2026-02-04 06:42:04 +00:00
refactor(units): remove macros
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use super::unit::{Unit, UnitDisplay};
|
||||
use derive_macro::UnitConversions;
|
||||
use crate::impl_unit_wrapper;
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)]
|
||||
#[error(UnitError)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct FileSize(Unit);
|
||||
impl_unit_wrapper!(FileSize);
|
||||
|
||||
impl FileSize {
|
||||
pub fn new(bytes: u64) -> Self {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use super::unit::{Unit, UnitDisplay};
|
||||
use derive_macro::UnitConversions;
|
||||
use crate::impl_unit_wrapper;
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)]
|
||||
#[error(UnitError)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct NetSpeed(Unit);
|
||||
impl_unit_wrapper!(NetSpeed);
|
||||
|
||||
impl NetSpeed {
|
||||
pub fn new(bytes_per_second: u64) -> Self {
|
||||
|
||||
@@ -1,23 +1,73 @@
|
||||
use derive_macro::UnitConversions;
|
||||
use std::fmt::Display;
|
||||
use thiserror::Error;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
#[derive(Error, Debug, PartialEq)]
|
||||
pub enum UnitError {
|
||||
#[error("Value cannot be negative: {value}")]
|
||||
NegativeValue { value: i64 },
|
||||
#[error("Value is too large: {value}")]
|
||||
ValueTooLarge { value: f64 },
|
||||
#[error("Value is invalid: {reason}")]
|
||||
InvalidValue { reason: String },
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
|
||||
macro_rules! impl_sealed {
|
||||
($($t:ty),*) => {
|
||||
$(impl Sealed for $t {})*
|
||||
};
|
||||
}
|
||||
|
||||
impl_sealed!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)]
|
||||
#[error(UnitError)]
|
||||
pub struct Unit(u64);
|
||||
pub trait IntoU64: sealed::Sealed {
|
||||
fn into_u64(self) -> u64;
|
||||
}
|
||||
|
||||
macro_rules! impl_into_u64 {
|
||||
($($t:ty),*) => {$(
|
||||
impl IntoU64 for $t {
|
||||
fn into_u64(self) -> u64 {
|
||||
self.try_into().unwrap_or(0)
|
||||
}
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
impl_into_u64!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct UnitWrapper<T = u64>(T);
|
||||
|
||||
impl<T> Default for UnitWrapper<T>
|
||||
where
|
||||
T: From<u64>,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self(T::from(0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> From<U> for UnitWrapper<T>
|
||||
where
|
||||
U: IntoU64,
|
||||
T: From<u64>,
|
||||
{
|
||||
fn from(value: U) -> Self {
|
||||
Self(T::from(value.into_u64()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> UnitWrapper<T> {
|
||||
pub fn new(inner: T) -> Self {
|
||||
Self(inner)
|
||||
}
|
||||
|
||||
pub fn inner(&self) -> &T {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub type Unit = UnitWrapper<u64>;
|
||||
|
||||
impl Unit {
|
||||
pub const fn new(value: u64) -> Self {
|
||||
pub const fn from_raw(value: u64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
@@ -40,21 +90,47 @@ impl<'a> UnitDisplay<'a> {
|
||||
|
||||
impl<'a> Display for UnitDisplay<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
const THREASHOLD: f64 = 1024.0;
|
||||
const THRESHOLD: f64 = 1024.0;
|
||||
|
||||
let value = self.unit.0 as f64;
|
||||
|
||||
if value < THREASHOLD {
|
||||
if value < THRESHOLD {
|
||||
return write!(f, "{} {}", self.unit.0, self.units[0]);
|
||||
}
|
||||
|
||||
let mut size = value;
|
||||
let mut unit_index = 0;
|
||||
|
||||
while size >= THREASHOLD && unit_index < self.units.len() - 1 {
|
||||
size /= THREASHOLD;
|
||||
while size >= THRESHOLD && unit_index < self.units.len() - 1 {
|
||||
size /= THRESHOLD;
|
||||
unit_index += 1;
|
||||
}
|
||||
write!(f, "{:.2} {}", size, self.units[unit_index])
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_unit_wrapper {
|
||||
($wrapper:ident) => {
|
||||
impl From<Unit> for $wrapper {
|
||||
fn from(unit: $crate::app::utils::unit::Unit) -> Self {
|
||||
Self(unit)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for $wrapper
|
||||
where
|
||||
T: $crate::app::utils::unit::IntoU64,
|
||||
{
|
||||
fn from(value: T) -> Self {
|
||||
Self($crate::app::utils::unit::Unit::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl $wrapper {
|
||||
pub fn unit(&self) -> &Unit {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user