Skip to main content

triblespace_core/blob/schemas/
filebytes.rs

1use crate::blob::Blob;
2use crate::blob::BlobSchema;
3use crate::blob::ToBlob;
4use crate::blob::TryFromBlob;
5use crate::id::ExclusiveId;
6use crate::id::Id;
7use crate::id_hex;
8use crate::macros::entity;
9use crate::metadata;
10use crate::metadata::{ConstDescribe, ConstId};
11use crate::repo::BlobStore;
12use crate::trible::Fragment;
13use crate::value::schemas::hash::Blake3;
14
15use anybytes::Bytes;
16
17/// Raw file bytes stored as a blob.
18///
19/// Use this schema when the payload represents a file snapshot and you want to
20/// preserve that provenance explicitly instead of falling back to UnknownBlob.
21pub struct FileBytes;
22
23impl BlobSchema for FileBytes {}
24
25impl ConstId for FileBytes {
26    const ID: Id = id_hex!("5DE76157AE4FDEA830019916805E80A4");
27}
28
29impl ConstDescribe for FileBytes {
30    fn describe<B>(blobs: &mut B) -> Result<Fragment, B::PutError>
31    where
32        B: BlobStore<Blake3>,
33    {
34        let id = Self::ID;
35        let description = blobs.put(
36            "Opaque file bytes captured as a blob. Use when the payload represents a file snapshot (attachments, dataset artifacts, exported archives) and you want to preserve that provenance in the schema rather than treating it as UnknownBlob. The meaning is given by adjacent metadata attributes such as mime type, filename, and dimensions.",
37        )?;
38        Ok(entity! {
39            ExclusiveId::force_ref(&id) @
40                metadata::name: blobs.put("filebytes")?,
41                metadata::description: description,
42                metadata::tag: metadata::KIND_BLOB_SCHEMA,
43        })
44    }
45}
46
47impl TryFromBlob<FileBytes> for Bytes {
48    type Error = std::convert::Infallible;
49
50    fn try_from_blob(blob: Blob<FileBytes>) -> Result<Self, Self::Error> {
51        Ok(blob.bytes)
52    }
53}
54
55impl ToBlob<FileBytes> for Bytes {
56    fn to_blob(self) -> Blob<FileBytes> {
57        Blob::new(self)
58    }
59}
60
61impl ToBlob<FileBytes> for Vec<u8> {
62    fn to_blob(self) -> Blob<FileBytes> {
63        Blob::new(Bytes::from_source(self))
64    }
65}
66
67impl ToBlob<FileBytes> for &[u8] {
68    fn to_blob(self) -> Blob<FileBytes> {
69        Blob::new(Bytes::from_source(self.to_vec()))
70    }
71}