Skip to main content

triblespace_core/inline/
encodings.rs

1//! This is a collection of Rust types that can be (de)serialized as [crate::prelude::Inline]s.
2
3/// Boolean inline encoding (all-zero / all-one).
4pub mod boolean;
5/// Ed25519 signature component and public key encodings.
6pub mod ed25519;
7/// 256-bit IEEE-like floating point encodings (little-endian and big-endian).
8pub mod f256;
9/// IEEE-754 double-precision floating point encoding.
10pub mod f64;
11/// Opaque 128-bit identifier encoding.
12pub mod genid;
13/// Cryptographic hash and typed blob handle encodings.
14pub mod hash;
15/// 256-bit signed and unsigned integer encodings (little-endian and big-endian).
16pub mod iu256;
17/// Line/column source location encoding.
18pub mod linelocation;
19/// 256-bit rational number encodings (little-endian and big-endian).
20pub mod r256;
21/// Range encodings for pairs of `u128` values.
22pub mod range;
23/// Inline UTF-8 short string encoding (up to 32 bytes).
24pub mod shortstring;
25/// TAI nanosecond interval encoding.
26pub mod time;
27
28use crate::id::ExclusiveId;
29use crate::id::Id;
30use crate::id_hex;
31use crate::macros::entity;
32use crate::metadata;
33use crate::metadata::MetaDescribe;
34use crate::trible::Fragment;
35use crate::inline::Inline;
36use crate::inline::InlineEncoding;
37use std::convert::Infallible;
38
39/// An inline encoding for unknown values.
40///
41/// Fallback when the encoding of an inline value isn't known. Not
42/// recommended for everyday use — prefer a specific encoding.
43///
44/// Any bit pattern is a valid `Inline<UnknownInline>`.
45pub struct UnknownInline {}
46
47impl MetaDescribe for UnknownInline {
48    fn describe() -> Fragment {
49        let id: Id = id_hex!("4EC697E8599AC79D667C722E2C8BEBF4");
50        #[allow(unused_mut)]
51        let mut tribles = entity! {
52            ExclusiveId::force_ref(&id) @ metadata::tag: metadata::KIND_INLINE_ENCODING
53        };
54        #[cfg(feature = "wasm")]
55        {
56            tribles += entity! { ExclusiveId::force_ref(&id) @
57                metadata::value_formatter: wasm_formatter::UNKNOWN_VALUE_WASM,
58            };
59        }
60        tribles
61    }
62}
63
64#[cfg(feature = "wasm")]
65mod wasm_formatter {
66    use core::fmt::Write;
67
68    use triblespace_core_macros::value_formatter;
69
70    #[value_formatter]
71    pub(crate) fn unknown_value(raw: &[u8; 32], out: &mut impl Write) -> Result<(), u32> {
72        out.write_str("unknown:").map_err(|_| 1u32)?;
73        const TABLE: &[u8; 16] = b"0123456789ABCDEF";
74        for &byte in raw {
75            let hi = (byte >> 4) as usize;
76            let lo = (byte & 0x0F) as usize;
77            out.write_char(TABLE[hi] as char).map_err(|_| 1u32)?;
78            out.write_char(TABLE[lo] as char).map_err(|_| 1u32)?;
79        }
80        Ok(())
81    }
82}
83
84impl InlineEncoding for UnknownInline {
85    type ValidationError = Infallible;
86    type Encoding = Self;
87
88    fn validate(value: Inline<Self>) -> Result<Inline<Self>, Self::ValidationError> {
89        Ok(value)
90    }
91}