mirror of
https://github.com/kristoferssolo/traxor.git
synced 2025-10-21 20:10:35 +00:00
refactor(units): remove macros
This commit is contained in:
parent
fe3ddf165e
commit
20b9eb0e58
@ -1,5 +1,4 @@
|
||||
mod merge;
|
||||
mod unit;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use syn::{DeriveInput, parse_macro_input};
|
||||
@ -9,9 +8,3 @@ 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)
|
||||
}
|
||||
|
||||
@ -55,4 +55,3 @@ pub fn impl_merge_derive(input: DeriveInput) -> TokenStream {
|
||||
|
||||
expanded.into()
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
use super::unit::{Unit, UnitDisplay};
|
||||
use derive_macro::UnitConversions;
|
||||
use super::unit::{Unit, UnitDisplay, UnitError};
|
||||
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 FileSize {
|
||||
@ -18,3 +16,17 @@ 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)?))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
use super::unit::{Unit, UnitDisplay};
|
||||
use derive_macro::UnitConversions;
|
||||
use super::unit::{Unit, UnitDisplay, UnitError};
|
||||
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 NetSpeed {
|
||||
@ -18,3 +16,17 @@ 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)?))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
use derive_macro::UnitConversions;
|
||||
use std::fmt::Display;
|
||||
use thiserror::Error;
|
||||
|
||||
@ -12,8 +11,7 @@ pub enum UnitError {
|
||||
InvalidValue { reason: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, UnitConversions)]
|
||||
#[error(UnitError)]
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct Unit(u64);
|
||||
|
||||
impl Unit {
|
||||
@ -58,3 +56,20 @@ 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))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user