Files
pezkuwi-sdk/vendor/pezkuwi-subxt/lightclient/src/chain_config.rs
T
pezkuwichain 62674ce919 feat: Vendor pezkuwi-subxt and pezkuwi-zombienet-sdk into monorepo
- Add pezkuwi-subxt crates to vendor/pezkuwi-subxt
- Add pezkuwi-zombienet-sdk crates to vendor/pezkuwi-zombienet-sdk
- Convert git dependencies to path dependencies
- Add vendor crates to workspace members
- Remove test/example crates from vendor (not needed for SDK)
- Fix feature propagation issues detected by zepter
- Fix workspace inheritance for internal dependencies
- All 606 crates now in workspace
- All 6919 internal dependency links verified correct
- No git dependencies remaining
2025-12-23 09:37:11 +03:00

66 lines
1.8 KiB
Rust

// Copyright 2019-2025 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use serde_json::Value;
use std::borrow::Cow;
/// Something went wrong building chain config.
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum ChainConfigError {
/// The provided chain spec is the wrong shape.
#[error("Invalid chain spec format")]
InvalidSpecFormat,
}
/// Configuration to connect to a chain.
pub struct ChainConfig<'a> {
// The chain spec to use.
chain_spec: Cow<'a, str>,
}
impl<'a> From<&'a str> for ChainConfig<'a> {
fn from(chain_spec: &'a str) -> Self {
ChainConfig::chain_spec(chain_spec)
}
}
impl From<String> for ChainConfig<'_> {
fn from(chain_spec: String) -> Self {
ChainConfig::chain_spec(chain_spec)
}
}
impl<'a> ChainConfig<'a> {
/// Construct a chain config from a chain spec.
pub fn chain_spec(chain_spec: impl Into<Cow<'a, str>>) -> Self {
ChainConfig { chain_spec: chain_spec.into() }
}
/// Set the bootnodes to the given ones.
pub fn set_bootnodes<S: AsRef<str>>(
self,
bootnodes: impl IntoIterator<Item = S>,
) -> Result<Self, ChainConfigError> {
let mut chain_spec_json: Value = serde_json::from_str(&self.chain_spec)
.map_err(|_e| ChainConfigError::InvalidSpecFormat)?;
if let Value::Object(map) = &mut chain_spec_json {
let bootnodes =
bootnodes.into_iter().map(|s| Value::String(s.as_ref().to_owned())).collect();
map.insert("bootNodes".to_string(), Value::Array(bootnodes));
} else {
return Err(ChainConfigError::InvalidSpecFormat);
}
Ok(ChainConfig { chain_spec: Cow::Owned(chain_spec_json.to_string()) })
}
// Used internally to fetch the chain spec back out.
pub(crate) fn as_chain_spec(&self) -> &str {
&self.chain_spec
}
}