Skip to content

Commit

Permalink
use url serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fbrv committed Jul 24, 2024
1 parent a6c5d09 commit 444604a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tracing = "0.1.37"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tree_hash = "0.6.0"
tree_hash_derive = "0.6.0"
url = "2.5.0"
url = { version="2.5.0", features=["serde"] }


[dev-dependencies]
Expand Down
32 changes: 3 additions & 29 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs, path::Path, str::FromStr};
use std::{fs, path::Path};

use alloy::rpc::types::beacon::BlsPublicKey;
use eyre::{Result, WrapErr};
use hashbrown::HashMap;
use serde::{Deserialize, Deserializer};
use serde::Deserialize;
use url::Url;

#[derive(Debug, Deserialize)]
Expand All @@ -27,38 +27,12 @@ pub struct Lookahead {
pub chain_id: u16,
#[serde(rename = "relays")]
pub relays: Vec<String>,
#[serde(rename = "registry", default, deserialize_with = "deserialize_registry")]
#[serde(rename = "registry")]
pub registry: Option<HashMap<BlsPublicKey, Url>>,
#[serde(rename = "url-provider")]
pub url_provider: UrlProvider,
}

fn deserialize_registry<'de, D>(
deserializer: D,
) -> Result<Option<HashMap<BlsPublicKey, Url>>, D::Error>
where
D: Deserializer<'de>,
{
let temp_registry: Option<HashMap<String, String>> = Option::deserialize(deserializer)?;
if let Some(temp_registry) = temp_registry {
let mut registry: HashMap<BlsPublicKey, Url> = HashMap::new();

for (key, value) in temp_registry {
match BlsPublicKey::from_str(key.as_str()) {
Ok(bls_key) => {
registry.insert(bls_key, Url::from_str(&value).unwrap());
}
Err(_) => {
return Err(serde::de::Error::custom(format!("Failed to convert key: {}", key)));
}
}
}
Ok(Some(registry))
} else {
Ok(None)
}
}

impl Config {
pub fn from_file(filepath: &Path) -> Result<Self> {
let toml_str = fs::read_to_string(filepath)?;
Expand Down

0 comments on commit 444604a

Please sign in to comment.