use crate::{CacheFileError, HostCacheError};
use std::path::PathBuf;
pub trait LoadJsonCacheErrorMapper {
type Error;
fn missing_cache(&self, path: PathBuf) -> Self::Error;
fn cache_operation(&self, source: CacheFileError) -> Self::Error;
fn parse_cache(&self, path: PathBuf, source: serde_json::Error) -> Self::Error;
fn unsupported_schema(&self, version: u32, expected: u32) -> Self::Error;
fn network_mismatch(&self, requested: String, actual: String) -> Self::Error;
}
#[cfg(any(feature = "icrc-host", feature = "nns-topology-host"))]
pub struct HostJsonCacheErrorMapper {
component: &'static str,
}
#[cfg(any(feature = "icrc-host", feature = "nns-topology-host"))]
impl HostJsonCacheErrorMapper {
pub const fn new(component: &'static str) -> Self {
Self { component }
}
}
#[cfg(any(feature = "icrc-host", feature = "nns-topology-host"))]
impl LoadJsonCacheErrorMapper for HostJsonCacheErrorMapper {
type Error = HostCacheError;
fn missing_cache(&self, path: PathBuf) -> Self::Error {
HostCacheError::missing_cache(self.component, path)
}
fn cache_operation(&self, source: CacheFileError) -> Self::Error {
HostCacheError::operation(self.component, source)
}
fn parse_cache(&self, path: PathBuf, source: serde_json::Error) -> Self::Error {
HostCacheError::parse_cache(self.component, path, source)
}
fn unsupported_schema(&self, version: u32, expected: u32) -> Self::Error {
HostCacheError::unsupported_cache_schema_version(self.component, version, expected)
}
fn network_mismatch(&self, requested: String, actual: String) -> Self::Error {
HostCacheError::network_mismatch(self.component, requested, actual)
}
}
#[cfg(any(feature = "dashboard-host", feature = "sns-host"))]
pub struct OwnerJsonCacheErrorMapper<Error> {
component: &'static str,
missing_cache: fn(PathBuf) -> Error,
}
#[cfg(any(feature = "dashboard-host", feature = "sns-host"))]
impl<Error> OwnerJsonCacheErrorMapper<Error> {
pub const fn new(component: &'static str, missing_cache: fn(PathBuf) -> Error) -> Self {
Self {
component,
missing_cache,
}
}
}
#[cfg(any(feature = "dashboard-host", feature = "sns-host"))]
impl<Error> LoadJsonCacheErrorMapper for OwnerJsonCacheErrorMapper<Error>
where
Error: From<HostCacheError>,
{
type Error = Error;
fn missing_cache(&self, path: PathBuf) -> Self::Error {
(self.missing_cache)(path)
}
fn cache_operation(&self, source: CacheFileError) -> Self::Error {
HostCacheError::operation(self.component, source).into()
}
fn parse_cache(&self, path: PathBuf, source: serde_json::Error) -> Self::Error {
HostCacheError::parse_cache(self.component, path, source).into()
}
fn unsupported_schema(&self, version: u32, expected: u32) -> Self::Error {
HostCacheError::unsupported_cache_schema_version(self.component, version, expected).into()
}
fn network_mismatch(&self, requested: String, actual: String) -> Self::Error {
HostCacheError::network_mismatch(self.component, requested, actual).into()
}
}