Skip to content

Commit

Permalink
refactor: prelude for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
onsails committed Nov 13, 2023
1 parent e2db95b commit 072b8fc
Showing 1 changed file with 52 additions and 46 deletions.
98 changes: 52 additions & 46 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,79 @@
// include!("gen/mod.rs");
mod gen;
use std::{num::ParseIntError, str::FromStr};

use ethnum::U256;
pub use gen::*;

pub type ClientChannel = tonic::transport::Channel;
pub use tonic;
pub mod prelude {
use std::{num::ParseIntError, str::FromStr};

use cosmos::base::v1beta1::Coin;
use super::*;
use ethnum::U256;

pub trait CoinExt<'a> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin>;
}
pub type ClientChannel = tonic::transport::Channel;
pub use tonic;

use cosmos::base::v1beta1::Coin;

// impl<'a, I> CoinOptionExt<'a> for I
// where
// I: AsRef<[Coin]>,
// {
// fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
// let denom = denom.into();
// self.as_ref().iter().find(move |c| c.denom == denom)
// }
// }

impl<'a> CoinExt<'a> for Option<Coin> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
self.as_ref().filter(|c| c.denom == denom.into())
pub trait CoinExt<'a> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin>;
}

// impl<'a, I> CoinOptionExt<'a> for I
// where
// I: AsRef<[Coin]>,
// {
// fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
// let denom = denom.into();
// self.as_ref().iter().find(move |c| c.denom == denom)
// }
// }

impl<'a> CoinExt<'a> for Option<Coin> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
self.as_ref().filter(|c| c.denom == denom.into())
}
}
}

impl<'a> CoinExt<'a> for Vec<Coin> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
let denom = denom.into();
for coin in self.iter() {
if coin.denom == denom {
return Some(&coin);
impl<'a> CoinExt<'a> for Vec<Coin> {
fn get_denom(&'a self, denom: impl Into<String>) -> Option<&'a Coin> {
let denom = denom.into();
for coin in self.iter() {
if coin.denom == denom {
return Some(&coin);
}
}
None
}
None
}
}

pub trait AmountExt {
fn to_u256(&self) -> Result<U256, ParseIntError>;
}
pub trait AmountExt {
fn to_u256(&self) -> Result<U256, ParseIntError>;
}

impl AmountExt for String {
fn to_u256(&self) -> Result<U256, ParseIntError> {
U256::from_str(&self)
impl AmountExt for String {
fn to_u256(&self) -> Result<U256, ParseIntError> {
U256::from_str(&self)
}
}
}

impl<'a> AmountExt for &'a str {
fn to_u256(&self) -> Result<U256, ParseIntError> {
U256::from_str(&self)
impl<'a> AmountExt for &'a str {
fn to_u256(&self) -> Result<U256, ParseIntError> {
U256::from_str(&self)
}
}
}

impl AmountExt for Coin {
fn to_u256(&self) -> Result<U256, ParseIntError> {
self.amount.to_u256()
impl AmountExt for Coin {
fn to_u256(&self) -> Result<U256, ParseIntError> {
self.amount.to_u256()
}
}
}

#[cfg(test)]
mod test {
use super::*;
use super::cosmos::base::v1beta1::Coin;
use super::prelude::*;
use ethnum::U256;
use std::str::FromStr;

#[test]
fn coin_option_ext() {
Expand Down

0 comments on commit 072b8fc

Please sign in to comment.