use core::fmt;
use fluent_uri::Uri;
use serde_json::Value;
#[cfg(target_family = "wasm")]
pub trait RetrieverBounds {}
#[cfg(target_family = "wasm")]
impl<T: ?Sized> RetrieverBounds for T {}
#[cfg(not(target_family = "wasm"))]
pub trait RetrieverBounds: Send + Sync {}
#[cfg(not(target_family = "wasm"))]
impl<T: ?Sized + Send + Sync> RetrieverBounds for T {}
pub trait Retrieve: RetrieverBounds {
fn retrieve(
&self,
uri: &Uri<String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>>;
}
#[derive(Debug, Clone)]
struct DefaultRetrieverError;
impl fmt::Display for DefaultRetrieverError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Default retriever does not fetch resources")
}
}
impl std::error::Error for DefaultRetrieverError {}
#[derive(Debug, PartialEq, Eq)]
pub struct DefaultRetriever;
impl Retrieve for DefaultRetriever {
fn retrieve(&self, _: &Uri<String>) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
Err(Box::new(DefaultRetrieverError))
}
}
#[cfg(feature = "retrieve-async")]
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait AsyncRetrieve: RetrieverBounds {
async fn retrieve(
&self,
uri: &Uri<String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>>;
}
#[cfg(feature = "retrieve-async")]
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl AsyncRetrieve for DefaultRetriever {
async fn retrieve(
&self,
_: &Uri<String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
Err(Box::new(DefaultRetrieverError))
}
}