Skip to main content

triblespace_core/blob/encodings/
rawbytes.rs

1use crate::inline::Encodes;
2use crate::blob::Blob;
3use crate::blob::BlobEncoding;
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::MetaDescribe;
11use crate::trible::Fragment;
12
13use anybytes::Bytes;
14
15/// Opaque raw bytes — no structural interpretation.
16///
17/// Use this for any payload whose decode target is `Bytes`/`Vec<u8>` —
18/// XSD `hexBinary` / `base64Binary` literals, file contents, digest
19/// values carried inline, attachments, key material. Distinct from
20/// [`UnknownBlob`](super::UnknownBlob) (the explicit "I don't know
21/// what schema this is" fallback): `RawBytes` is a positive choice
22/// meaning "I do know what this is — it's bytes."
23///
24/// "File-provenance" or "attachment" semantic intent lives at the
25/// attribute level (`file::contents`, `request::body`, etc.), not at
26/// the encoding level — `RawBytes` is the byte encoding; attributes
27/// supply the meaning.
28pub struct RawBytes;
29
30impl BlobEncoding for RawBytes {}
31
32impl MetaDescribe for RawBytes {
33    fn describe() -> Fragment {
34        let id: Id = id_hex!("4C1BA1EB2FDCC637C2F269A46FCA2398");
35        entity! {
36            ExclusiveId::force_ref(&id) @
37                metadata::name: "rawbytes",
38                metadata::description: "Opaque raw bytes with no further structural interpretation. Used for any payload whose decode target is Bytes/Vec<u8>: XSD hexBinary / base64Binary literals, file contents, inline digests, key material. Distinct from UnknownBlob (the 'unknown schema' fallback): RawBytes is a positive choice meaning the schema *is* raw bytes.",
39                metadata::tag: metadata::KIND_BLOB_ENCODING,
40        }
41    }
42}
43
44impl TryFromBlob<RawBytes> for Bytes {
45    type Error = std::convert::Infallible;
46
47    fn try_from_blob(blob: Blob<RawBytes>) -> Result<Self, Self::Error> {
48        Ok(blob.bytes)
49    }
50}
51
52impl Encodes<Bytes> for RawBytes
53where crate::inline::encodings::hash::Handle<RawBytes>: crate::inline::InlineEncoding,
54{
55    type Output = Blob<RawBytes>;
56    fn encode(source: Bytes) -> Blob<RawBytes> {
57        Blob::new(source)
58    }
59}
60
61impl Encodes<Vec<u8>> for RawBytes
62where crate::inline::encodings::hash::Handle<RawBytes>: crate::inline::InlineEncoding,
63{
64    type Output = Blob<RawBytes>;
65    fn encode(source: Vec<u8>) -> Blob<RawBytes> {
66        Blob::new(Bytes::from_source(source))
67    }
68}
69
70impl Encodes<&[u8]> for RawBytes
71where crate::inline::encodings::hash::Handle<RawBytes>: crate::inline::InlineEncoding,
72{
73    type Output = Blob<RawBytes>;
74    fn encode(source: &[u8]) -> Blob<RawBytes> {
75        Blob::new(Bytes::from_source(source.to_vec()))
76    }
77}