noosphere_into/write/
target.rs

1use anyhow::Result;
2use std::future::Future;
3use std::path::Path;
4
5use async_trait::async_trait;
6use tokio::io::AsyncRead;
7
8#[cfg(not(target_arch = "wasm32"))]
9pub trait WriteTargetConditionalSend: Send {}
10
11#[cfg(not(target_arch = "wasm32"))]
12pub trait WriteTargetConditionalSendSync: Send + Sync {}
13
14#[cfg(target_arch = "wasm32")]
15pub trait WriteTargetConditionalSend {}
16
17#[cfg(target_arch = "wasm32")]
18pub trait WriteTargetConditionalSendSync {}
19
20/// An interface for accessing durable storage. This is used by transformers
21/// in this crate to render files from Noosphere content.
22#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
23#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
24pub trait WriteTarget: Clone + WriteTargetConditionalSendSync {
25    /// Returns true if a file exists at the provided path
26    async fn exists(&self, path: &Path) -> Result<bool>;
27
28    /// Given a path and an [AsyncRead], write the contents of the [AsyncRead]
29    /// to the path
30    async fn write<R>(&self, path: &Path, contents: R) -> Result<()>
31    where
32        R: AsyncRead + Unpin + WriteTargetConditionalSend;
33
34    /// Create a symbolic link between the give source path and destination path
35    async fn symlink(&self, src: &Path, dst: &Path) -> Result<()>;
36
37    /// Spawn a [Future] in a platform-appropriate fashion and poll it to
38    /// completion
39    async fn spawn<F>(future: F) -> Result<()>
40    where
41        F: Future<Output = Result<()>> + WriteTargetConditionalSend + 'static;
42}
43
44impl<W> WriteTargetConditionalSendSync for W where W: WriteTarget {}
45
46#[cfg(not(target_arch = "wasm32"))]
47impl<S> WriteTargetConditionalSend for S where S: Send {}
48
49#[cfg(target_arch = "wasm32")]
50impl<S> WriteTargetConditionalSend for S {}