Skip to main content

triblespace_core/blob/
encodings.rs

1//! This is a collection of Rust types that can be (de)serialized as [crate::prelude::Blob]s.
2
3/// Flat typed array blob encoding.
4pub mod array;
5/// Arbitrary-length UTF-8 text blob encoding.
6pub mod longstring;
7/// Opaque raw bytes blob encoding (positive choice, distinct from UnknownBlob).
8pub mod rawbytes;
9/// Canonical trible sequence blob encoding.
10pub mod simplearchive;
11/// Succinct (Ring-based) compressed trible archive blob encoding.
12pub mod succinctarchive;
13/// WebAssembly bytecode blob encoding.
14pub mod wasmcode;
15
16use crate::inline::Encodes;
17use anybytes::Bytes;
18
19use crate::blob::BlobEncoding;
20use crate::id_hex;
21use crate::macros::entity;
22use crate::metadata::{self, MetaDescribe};
23
24use super::Blob;
25use super::TryFromBlob;
26
27/// A blob encoding for an unknown blob.
28/// This blob encoding is used as a fallback when the blob encoding is not known.
29/// It is not recommended to use this blob encoding in practice.
30/// Instead, use a specific blob encoding.
31///
32/// Any bit pattern can be a valid blob of this schema.
33pub struct UnknownBlob;
34impl BlobEncoding for UnknownBlob {}
35
36impl MetaDescribe for UnknownBlob {
37    fn describe() -> crate::trible::Fragment {
38        // Fixed-id fallback schema. Even though it's discouraged in
39        // practice, the metadata should still self-describe so a
40        // consumer encountering this id can recognise it.
41        let id = id_hex!("EAB14005141181B0C10C4B5DD7985F8D");
42        entity! { crate::id::ExclusiveId::force_ref(&id) @
43            metadata::name:        "UnknownBlob",
44            metadata::description: "Fallback blob encoding for byte payloads with no known type. Discouraged in practice — use a specific blob encoding (e.g. `LongString`, `Array<T>`, `SimpleArchive`) instead.",
45            metadata::tag:         metadata::KIND_BLOB_ENCODING,
46        }
47    }
48}
49
50impl TryFromBlob<UnknownBlob> for Bytes {
51    type Error = std::convert::Infallible;
52
53    fn try_from_blob(blob: Blob<UnknownBlob>) -> Result<Self, Self::Error> {
54        Ok(blob.bytes)
55    }
56}
57
58impl Encodes<Bytes> for UnknownBlob
59where crate::inline::encodings::hash::Handle<UnknownBlob>: crate::inline::InlineEncoding,
60{
61    type Output = Blob<UnknownBlob>;
62    fn encode(source: Bytes) -> Blob<UnknownBlob> {
63        Blob::new(source)
64    }
65}