use crate::{ic_registry::MainnetRegistryFetchRequest, subnet_catalog::MAINNET_NETWORK};
#[derive(Clone, Copy, Debug, Default)]
pub struct LiveNnsSource;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NnsSourceRequest {
pub network: String,
pub endpoint: String,
pub fetched_at: String,
pub fetched_by: String,
}
impl NnsSourceRequest {
#[must_use]
pub fn new(
network: impl Into<String>,
endpoint: impl Into<String>,
fetched_at: impl Into<String>,
fetched_by: impl Into<String>,
) -> Self {
Self {
network: network.into(),
endpoint: endpoint.into(),
fetched_at: fetched_at.into(),
fetched_by: fetched_by.into(),
}
}
}
pub fn mainnet_registry_fetch_request<Error>(
request: &NnsSourceRequest,
unsupported_network: impl FnOnce(String) -> Error,
) -> Result<MainnetRegistryFetchRequest, Error> {
enforce_mainnet_network_with(&request.network, unsupported_network)?;
let mut fetch_request = MainnetRegistryFetchRequest::new(request.fetched_at.clone());
fetch_request.endpoint.clone_from(&request.endpoint);
fetch_request.fetched_by.clone_from(&request.fetched_by);
Ok(fetch_request)
}
pub(in crate::nns) fn enforce_mainnet_network_with<Error>(
network: &str,
unsupported_network: impl FnOnce(String) -> Error,
) -> Result<(), Error> {
if network == MAINNET_NETWORK {
return Ok(());
}
Err(unsupported_network(network.to_string()))
}