macro_rules! impl_nns_inventory_host_error {
($host_error:ty, $component:expr $(,)?) => {
impl $crate::nns::inventory::NnsInventoryHostError for $host_error {
fn cache_refresh_reason(
self,
expected_path: std::path::PathBuf,
) -> Result<$crate::cache_file::CacheRefreshReason, Self> {
match self {
Self::Cache(error) => {
$crate::cache_file::host_cache_refresh_reason(error, &expected_path)
.map_err(Self::Cache)
}
error => Err(error),
}
}
fn invalid_cache(path: std::path::PathBuf, reason: String) -> Self {
Self::Cache($crate::HostCacheError::invalid_cache(
$component, path, reason,
))
}
fn invalid_source_data(reason: String) -> Self {
Self::InvalidSourceData { reason }
}
}
};
}
macro_rules! impl_nns_inventory_report {
(
$report:ty,
$schema_version:expr,
$item_name:literal,
$count_field:ident,
$rows_field:ident
$(, $validate_family:expr)?
$(,)?
) => {
impl $crate::nns::inventory::NnsInventoryReport for $report {
const SCHEMA_VERSION: u32 = $schema_version;
const ITEM_NAME: &'static str = $item_name;
fn schema_version(&self) -> u32 {
self.schema_version
}
fn network(&self) -> &str {
&self.network
}
fn registry_canister_id(&self) -> &str {
&self.registry_canister_id
}
fn fetched_at(&self) -> &str {
&self.fetched_at
}
fn source_endpoint(&self) -> &str {
&self.source_endpoint
}
fn declared_row_count(&self) -> usize {
self.$count_field
}
fn row_count(&self) -> usize {
self.$rows_field.len()
}
fn validate_family(&self) -> Result<(), String> {
$(($validate_family)(self)?;)?
Ok(())
}
}
};
}
macro_rules! nns_leaf_cache {
(
$cache_path_fn:ident,
$lock_path_fn:ident,
$load_fn:ident,
$cache_request:ty,
$list_report:ty,
$host_error:ty,
$component_dir:expr,
$cache_file:expr,
$schema_version:expr
$(,)?
) => {
#[must_use]
pub fn $cache_path_fn(cache_root: &std::path::Path, network: &str) -> std::path::PathBuf {
nns_leaf_cache_paths(cache_root, network).cache_path
}
#[must_use]
pub fn $lock_path_fn(cache_root: &std::path::Path, network: &str) -> std::path::PathBuf {
nns_leaf_cache_paths(cache_root, network).lock_path
}
pub(super) fn $load_fn(
request: &$cache_request,
) -> Result<$crate::cache_file::CachedJsonReport<$list_report>, $host_error> {
super::enforce_mainnet_network(&request.network)?;
let cached = $crate::nns::leaf::load_nns_leaf_json_cache(
request,
$component_dir,
$cache_file,
$schema_version,
)
.map_err(<$host_error as From<$crate::HostCacheError>>::from)?;
<$list_report as $crate::nns::inventory::NnsInventoryReport>::validate(&cached.report)
.map_err(|reason| {
<$host_error as $crate::nns::inventory::NnsInventoryHostError>::invalid_cache(
cached.path.clone(),
reason,
)
})?;
Ok(cached)
}
fn nns_leaf_cache_paths(
cache_root: &std::path::Path,
network: &str,
) -> $crate::nns::leaf::NnsLeafCachePaths {
$crate::nns::leaf::NnsLeafCachePaths::for_component(
cache_root,
$component_dir,
network,
$cache_file,
)
}
};
}
macro_rules! nns_leaf_refresh_report {
(
$report_type:ident,
$schema_version:expr,
$request:ident,
$report:ident,
$write_result:ident,
$count_field:ident
$(, $governance_canister_id:expr)?
$(,)?
) => {
$report_type {
schema_version: $schema_version,
network: $report.network.clone(),
cache_path: $write_result.cache_path,
refresh_lock_path: $write_result.refresh_lock_path,
output_path: $write_result.output_path,
$(governance_canister_id: $governance_canister_id,)?
registry_canister_id: $report.registry_canister_id.clone(),
registry_version: $report.registry_version,
fetched_at: $report.fetched_at.clone(),
source_endpoint: $report.source_endpoint.clone(),
fetched_by: $report.fetched_by.clone(),
dry_run: $request.dry_run,
wrote_cache: $write_result.wrote_cache,
replaced_existing_cache: $write_result.replaced_existing_cache,
$count_field: $report.$count_field,
}
};
}
macro_rules! nns_leaf_refresh_report_text {
(
$report:ident,
$governance_canister_id:expr,
$count_label:literal,
$count_field:ident
$(,)?
) => {
$crate::nns::render::nns_leaf_refresh_report_text($crate::nns::render::NnsLeafRefreshText {
network: &$report.network,
cache_path: &$report.cache_path,
refresh_lock_path: &$report.refresh_lock_path,
governance_canister_id: $governance_canister_id,
registry_canister_id: &$report.registry_canister_id,
registry_version: $report.registry_version,
fetched_at: &$report.fetched_at,
source_endpoint: &$report.source_endpoint,
fetched_by: &$report.fetched_by,
dry_run: $report.dry_run,
wrote_cache: $report.wrote_cache,
replaced_existing_cache: $report.replaced_existing_cache,
count_label: $count_label,
count: $report.$count_field,
})
};
}
macro_rules! impl_nns_leaf_cache_and_refresh_requests {
($cache:ty, $refresh:ty) => {
impl crate::nns::leaf::NnsLeafCacheRequest for $cache {
fn cache_root(&self) -> &std::path::Path {
&self.cache_root
}
fn network(&self) -> &str {
&self.network
}
}
impl crate::nns::leaf::NnsLeafRefreshRequest for $refresh {
type Cache = $cache;
fn cache(&self) -> &Self::Cache {
&self.cache
}
fn now_unix_secs(&self) -> u64 {
self.now_unix_secs
}
fn lock_stale_after_seconds(&self) -> u64 {
self.lock_stale_after_seconds
}
fn dry_run(&self) -> bool {
self.dry_run
}
fn output_path(&self) -> Option<&std::path::Path> {
self.output_path.as_deref()
}
}
};
}
macro_rules! impl_nns_mainnet_network_enforcer {
($error:ident) => {
fn enforce_mainnet_network(network: &str) -> Result<(), $error> {
crate::network::enforce_mainnet_network_with(network, |network| {
$error::UnsupportedNetwork { network }
})
}
};
}