Skip to content

Commit

Permalink
cli: Fix custom provider.cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto committed Dec 13, 2024
1 parent 2ae3774 commit 1f75ec9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use dirs::home_dir;
use heck::ToSnakeCase;
use reqwest::Url;
use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use solana_cli_config::{Config as SolanaConfig, CONFIG_FILE};
use solana_sdk::clock::Slot;
use solana_sdk::pubkey::Pubkey;
Expand Down Expand Up @@ -590,11 +591,29 @@ struct _Config {

#[derive(Debug, Serialize, Deserialize)]
struct Provider {
#[serde(deserialize_with = "des_cluster")]
#[serde(serialize_with = "ser_cluster", deserialize_with = "des_cluster")]
cluster: Cluster,
wallet: String,
}

fn ser_cluster<S: Serializer>(cluster: &Cluster, s: S) -> Result<S::Ok, S::Error> {
match cluster {
Cluster::Custom(http, ws) => {
match (Url::parse(http), Url::parse(ws)) {
// If `ws` was derived from `http`, serialize `http` as string
(Ok(h), Ok(w)) if h.domain() == w.domain() => s.serialize_str(http),
_ => {
let mut map = s.serialize_map(Some(2))?;
map.serialize_entry("http", http)?;
map.serialize_entry("ws", ws)?;
map.end()
}
}
}
_ => s.serialize_str(&cluster.to_string()),
}
}

fn des_cluster<'de, D>(deserializer: D) -> Result<Cluster, D::Error>
where
D: Deserializer<'de>,
Expand Down

0 comments on commit 1f75ec9

Please sign in to comment.