Skip to main content

triblespace_core/value/
schemas.rs

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