mod gateway;
pub use gateway::GatewayClient;
#[cfg(not(target_arch = "wasm32"))]
mod kubo;
#[cfg(not(target_arch = "wasm32"))]
pub use kubo::KuboClient;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use cid::Cid;
use noosphere_common::{ConditionalSend, ConditionalSync};
use std::fmt::Debug;
use tokio::io::AsyncRead;
pub trait IpfsClientAsyncReadSendSync: AsyncRead + ConditionalSync + 'static {}
impl<S> IpfsClientAsyncReadSendSync for S where S: AsyncRead + ConditionalSync + 'static {}
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
pub trait IpfsClient: Clone + Debug {
async fn pin_blocks<'a, I>(&self, cids: I) -> Result<()>
where
I: IntoIterator<Item = &'a Cid> + ConditionalSend + Debug,
I::IntoIter: ConditionalSend;
async fn block_is_pinned(&self, cid: &Cid) -> Result<bool>;
async fn server_identity(&self) -> Result<String>;
async fn syndicate_blocks<R>(&self, car: R) -> Result<()>
where
R: IpfsClientAsyncReadSendSync;
async fn get_block(&self, cid: &Cid) -> Result<Option<Vec<u8>>>;
async fn put_block(&mut self, cid: &Cid, block: &[u8]) -> Result<()>;
async fn require_block(&self, cid: &Cid) -> Result<Vec<u8>> {
match self.get_block(cid).await? {
Some(block) => Ok(block),
None => Err(anyhow!("No block found for CID {}", cid)),
}
}
}