Skip to content

Commit

Permalink
refactor: add derive_more and adjust imports
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuhuiluo committed Oct 7, 2024
1 parent 0c6cc04 commit dc23646
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 3 additions & 11 deletions src/entities/currency.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;
use alloy_primitives::ChainId;
use derive_more::Deref;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Currency {
Expand All @@ -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<const IS_NATIVE: bool, M> {
/// The chain ID on which this currency resides
pub chain_id: ChainId,
Expand All @@ -24,19 +25,10 @@ pub struct CurrencyLike<const IS_NATIVE: bool, M> {
pub name: Option<String>,

/// Metadata associated with the currency
#[deref]
pub meta: M,
}

/// Implement [`Deref`] to allow direct access to the metadata of the currency
impl<const IS_NATIVE: bool, M> Deref for CurrencyLike<IS_NATIVE, M> {
type Target = M;

#[inline]
fn deref(&self) -> &Self::Target {
&self.meta
}
}

macro_rules! match_currency_method {
($method:ident, $return_type:ty) => {
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
@@ -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<T> = FractionLike<CurrencyMeta<T>>;
Expand Down
16 changes: 4 additions & 12 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
@@ -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<M> {
pub numerator: BigInt,
pub denominator: BigInt,
/// Metadata associated with the fraction
#[deref]
pub meta: M,
}

Expand All @@ -24,16 +26,6 @@ impl<M: Default> Default for FractionLike<M> {
}
}

/// Implement [`Deref`] to allow direct access to the metadata of the fraction
impl<M> Deref for FractionLike<M> {
type Target = M;

#[inline]
fn deref(&self) -> &Self::Target {
&self.meta
}
}

/// Type alias for a simple Fraction without metadata
pub type Fraction = FractionLike<()>;

Expand Down
1 change: 0 additions & 1 deletion src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// External crate dependencies
use crate::prelude::*;

/// Type alias for a Price, a [`FractionLike`] with metadata [`PriceMeta`]
Expand Down
7 changes: 1 addition & 6 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dc23646

Please sign in to comment.