noosphere_core/context/content/
file.rs

1use std::pin::Pin;
2
3use crate::data::{Did, Link, MemoIpld};
4use tokio::io::AsyncRead;
5
6/// A type that may be used as the contents field in a [SphereFile]
7#[cfg(not(target_arch = "wasm32"))]
8pub trait AsyncFileBody: AsyncRead + Unpin + Send {}
9
10#[cfg(not(target_arch = "wasm32"))]
11impl<S> AsyncFileBody for S where S: AsyncRead + Unpin + Send {}
12
13#[cfg(target_arch = "wasm32")]
14/// A type that may be used as the contents field in a [SphereFile]
15pub trait AsyncFileBody: AsyncRead + Unpin {}
16
17#[cfg(target_arch = "wasm32")]
18impl<S> AsyncFileBody for S where S: AsyncRead + Unpin {}
19
20/// A descriptor for contents that is stored in a sphere.
21pub struct SphereFile<C> {
22    /// The identity of the associated sphere from which the file was read
23    pub sphere_identity: Did,
24    /// The version of the associated sphere from which the file was read
25    pub sphere_version: Link<MemoIpld>,
26    /// The version of the memo that wraps the file's body contents
27    pub memo_version: Link<MemoIpld>,
28    /// The memo that wraps the file's body contents
29    pub memo: MemoIpld,
30    /// The body contents of the file
31    pub contents: C,
32}
33
34impl<C> SphereFile<C>
35where
36    C: AsyncFileBody + 'static,
37{
38    /// Consume the file and return a version of it where its body contents have
39    /// been boxed and pinned
40    pub fn boxed(self) -> SphereFile<Pin<Box<dyn AsyncFileBody + 'static>>> {
41        SphereFile {
42            sphere_identity: self.sphere_identity,
43            sphere_version: self.sphere_version,
44            memo_version: self.memo_version,
45            memo: self.memo,
46            contents: Box::pin(self.contents),
47        }
48    }
49}