Skip to main content

kaish_types/
value.rs

1//! Value types for kaish's AST and runtime.
2
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4
5/// A literal value.
6///
7/// Supports primitives (null, bool, int, float, string), structured JSON data
8/// (arrays and objects), and binary blob references.
9#[derive(Debug, Clone, PartialEq)]
10pub enum Value {
11    Null,
12    Bool(bool),
13    Int(i64),
14    Float(f64),
15    String(String),
16    /// Structured JSON data (arrays, objects, nested structures).
17    /// Use `jq` to query/extract values.
18    Json(serde_json::Value),
19    /// Inline binary data — the shell's single binary value. Carries the bytes
20    /// themselves (keys, a file header, a random draw, a `dd` block), pipe-native
21    /// since the pipe is already a byte buffer. Serializes as the base64 envelope
22    /// from [`crate::bytes`]. Persisting large binary is a separate VFS concern
23    /// (plain files under `/v/blobs`), not a `Value`. See `docs/binary-data.md`.
24    Bytes(Vec<u8>),
25}
26
27impl Serialize for Value {
28    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
29        // Delegate to value_to_json for consistent JSON representation.
30        // Float NaN → null, Bytes → {_type: "bytes", ...}, Json → inline.
31        crate::result::value_to_json(self).serialize(serializer)
32    }
33}
34
35impl<'de> Deserialize<'de> for Value {
36    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
37        let json = serde_json::Value::deserialize(deserializer)?;
38        Ok(crate::result::json_to_value(json))
39    }
40}
41