Skip to content

Commit

Permalink
feat: move iter_parent_names to pub utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenzing committed Jul 12, 2024
1 parent c637a32 commit 929c704
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
13 changes: 6 additions & 7 deletions src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{domain_id::DomainIdProvider, errors::CCIPReaderError, types::ResolveResult};
use crate::{
domain_id::DomainIdProvider, errors::CCIPReaderError, types::ResolveResult,
utils::iter_parent_names,
};
use alloy::{
eips::BlockId,
hex::FromHex,
Expand All @@ -10,7 +13,7 @@ use alloy::{
};
use async_recursion::async_recursion;
use serde_json::Value;
use std::{iter::successors, time::Duration};
use std::time::Duration;

use crate::{
ccip, consts, contracts,
Expand Down Expand Up @@ -159,7 +162,7 @@ where
}

pub async fn get_resolver(&self, name: &str) -> Result<Address, CCIPReaderError> {
for parent_name in Self::iter_parent_names(name) {
for parent_name in iter_parent_names(name) {
if parent_name.is_empty() || parent_name.eq(".") {
return Ok(Address::ZERO);
}
Expand Down Expand Up @@ -203,10 +206,6 @@ where
Ok(ResolveResult { addr: response._0 })
}

pub fn iter_parent_names(name: &str) -> Vec<&str> {
successors(Some(name), |&last| last.split_once('.').map(|it| it.1)).collect()
}

async fn query_resolver_parameters<C: SolCall>(
&self,
name: &str,
Expand Down
54 changes: 33 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
use anyhow::bail;
use std::time::Duration;

pub(crate) fn truncate_str(src: &str, side: usize) -> String {
if src.len() < side * 2 + 3 {
return src.to_string();
}

format!("{}..{}", &src[..side], &src[src.len() - side..])
}

pub(crate) fn build_reqwest(timeout: Duration) -> reqwest::Client {
reqwest::Client::builder()
.timeout(timeout)
.build()
.expect("should be a valid reqwest client")
}

pub(crate) fn sanitaze_error_data_from_rpc(data: String) -> String {
data.trim_start_matches("Reverted").trim().to_string()
}
use std::{iter::successors, time::Duration};

/// Encodes a domain name into its binary representation according to the DNS
/// wire format. Each label (i.e., substring separated by dots) in the domain
Expand All @@ -37,7 +18,7 @@ pub(crate) fn sanitaze_error_data_from_rpc(data: String) -> String {
/// # Example
///
/// ```
/// use alloy_ccip_read::utils::{dns_encode};
/// use alloy_ccip_read::utils::dns_encode;
///
/// let encoded = dns_encode("tanrikulu.eth").unwrap();
/// assert_eq!(encoded, vec![9, b't', b'a', b'n', b'r', b'i', b'k', b'u', b'l', b'u', 3, b'e', b't', b'h', 0]);
Expand All @@ -62,6 +43,37 @@ pub fn dns_encode(domain: &str) -> Result<Vec<u8>, anyhow::Error> {
Ok(encoded)
}

/// Returns an iterator over the parent names of a given domain name.
/// ```
/// use alloy_ccip_read::utils::iter_parent_names;
/// use std::iter::FromIterator;
///
/// let parent_names = Vec::from_iter(iter_parent_names("tanrikulu.eth"));
/// assert_eq!(parent_names, vec!["tanrikulu.eth", "eth"]);
/// ```
pub fn iter_parent_names(name: &str) -> impl IntoIterator<Item = &str> {
successors(Some(name), |&last| last.split_once('.').map(|it| it.1))
}

pub(crate) fn truncate_str(src: &str, side: usize) -> String {
if src.len() < side * 2 + 3 {
return src.to_string();
}

format!("{}..{}", &src[..side], &src[src.len() - side..])
}

pub(crate) fn build_reqwest(timeout: Duration) -> reqwest::Client {
reqwest::Client::builder()
.timeout(timeout)
.build()
.expect("should be a valid reqwest client")
}

pub(crate) fn sanitaze_error_data_from_rpc(data: String) -> String {
data.trim_start_matches("Reverted").trim().to_string()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 929c704

Please sign in to comment.