From f94c2a1a423d7abcb0b01ad8d5ee641e34918ef0 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Mon, 7 Oct 2024 03:46:14 -0400 Subject: [PATCH] refactor: add `derive_more` and adjust imports (#86) Integrated `derive_more` for `Deref` attribute across various structs and adjusted the core module imports accordingly. This helps in simplifying the code and reducing redundancy. --- Cargo.toml | 1 + src/entities/currency.rs | 14 +++----------- src/entities/fractions/currency_amount.rs | 2 +- src/entities/fractions/fraction.rs | 16 ++++------------ src/entities/fractions/price.rs | 1 - src/prelude.rs | 7 +------ 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54fa16d..2770fbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT" [dependencies] alloy-primitives = "0.8" bigdecimal = "0.4.5" +derive_more = { version = "1.0.0", features = ["deref"] } eth_checksum = { version = "0.1.2", optional = true } lazy_static = "1.5" num-bigint = "0.4" diff --git a/src/entities/currency.rs b/src/entities/currency.rs index 51d3605..76059e1 100644 --- a/src/entities/currency.rs +++ b/src/entities/currency.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use alloy_primitives::ChainId; +use derive_more::Deref; #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub enum Currency { @@ -9,7 +10,7 @@ pub enum Currency { /// [`CurrencyLike`] is a generic struct representing a currency with a specific chain ID, /// decimals, symbol, name, and additional metadata. -#[derive(Clone, Debug, Hash, PartialEq, Eq)] +#[derive(Clone, Debug, Hash, PartialEq, Eq, Deref)] pub struct CurrencyLike { /// The chain ID on which this currency resides pub chain_id: ChainId, @@ -24,19 +25,10 @@ pub struct CurrencyLike { pub name: Option, /// Metadata associated with the currency + #[deref] pub meta: M, } -/// Implement [`Deref`] to allow direct access to the metadata of the currency -impl Deref for CurrencyLike { - type Target = M; - - #[inline] - fn deref(&self) -> &Self::Target { - &self.meta - } -} - macro_rules! match_currency_method { ($method:ident, $return_type:ty) => { #[inline] diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index 3bcb606..3827b27 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -1,5 +1,5 @@ -/// External crate dependencies use crate::prelude::*; +use core::ops::Div; /// Currency amount struct that represents a rational amount of a currency pub type CurrencyAmount = FractionLike>; diff --git a/src/entities/fractions/fraction.rs b/src/entities/fractions/fraction.rs index 83460e8..ac73b03 100644 --- a/src/entities/fractions/fraction.rs +++ b/src/entities/fractions/fraction.rs @@ -1,15 +1,17 @@ use crate::prelude::*; use core::{ hash::{Hash, Hasher}, - ops::{Add, Deref, Mul, Sub}, + ops::{Add, Div, Mul, Sub}, }; +use derive_more::Deref; /// Struct representing a fraction with metadata -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deref)] pub struct FractionLike { pub numerator: BigInt, pub denominator: BigInt, /// Metadata associated with the fraction + #[deref] pub meta: M, } @@ -24,16 +26,6 @@ impl Default for FractionLike { } } -/// Implement [`Deref`] to allow direct access to the metadata of the fraction -impl Deref for FractionLike { - type Target = M; - - #[inline] - fn deref(&self) -> &Self::Target { - &self.meta - } -} - /// Type alias for a simple Fraction without metadata pub type Fraction = FractionLike<()>; diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 5207b72..ceea6ef 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -1,4 +1,3 @@ -/// External crate dependencies use crate::prelude::*; /// Type alias for a Price, a [`FractionLike`] with metadata [`PriceMeta`] diff --git a/src/prelude.rs b/src/prelude.rs index 489145d..540b856 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -25,12 +25,7 @@ pub use alloc::{ }; pub use alloy_primitives::{address, Address}; pub use bigdecimal::{BigDecimal, RoundingMode}; -pub use core::{ - cmp::Ordering, - num::NonZeroU64, - ops::{Deref, Div}, - str::FromStr, -}; +pub use core::{cmp::Ordering, num::NonZeroU64, str::FromStr}; pub use lazy_static::lazy_static; pub use num_bigint::{BigInt, BigUint, ToBigInt, ToBigUint}; pub use num_integer::Integer;