Skip to main content

TeaLeaf

Struct TeaLeaf 

Source
pub struct TeaLeaf {
    pub schemas: IndexMap<String, Schema>,
    pub unions: IndexMap<String, Union>,
    pub data: IndexMap<String, Value>,
    /* private fields */
}
Expand description

A parsed TeaLeaf document

Fields§

§schemas: IndexMap<String, Schema>§unions: IndexMap<String, Union>§data: IndexMap<String, Value>

Implementations§

Source§

impl TeaLeaf

Source

pub fn new( schemas: IndexMap<String, Schema>, data: IndexMap<String, Value>, ) -> Self

Create a new TeaLeaf document from data and schemas.

This constructor is primarily for programmatic document creation. For parsing from formats, use parse(), load(), or from_json().

Source

pub fn parse(input: &str) -> Result<Self>

Parse TeaLeaf text format

Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<Self>

Load from text file

Include paths are resolved relative to the loaded file’s directory.

Source

pub fn get(&self, key: &str) -> Option<&Value>

Get a value by key

Source

pub fn schema(&self, name: &str) -> Option<&Schema>

Get a schema by name

Source

pub fn union(&self, name: &str) -> Option<&Union>

Get a union by name

Source

pub fn compile<P: AsRef<Path>>(&self, path: P, compress: bool) -> Result<()>

Compile to binary format

Source

pub fn from_json(json: &str) -> Result<Self>

Parse from JSON string.

§Stability Policy

This function follows a “plain JSON only” policy:

  • JSON is parsed as-is with no magic conversion
  • {"$ref": "x"} stays as an Object, NOT a Ref
  • {"$tag": "ok", "$value": 200} stays as an Object, NOT a Tagged
  • "0xcafef00d" stays as a String, NOT Bytes
  • "2024-01-15T10:30:00Z" stays as a String, NOT a Timestamp
  • [[1, "one"], [2, "two"]] stays as an Array, NOT a Map

To create special TeaLeaf types, use the text format or binary API directly.

§Number Type Inference
  • Integers that fit i64Value::Int
  • Large positive integers that fit u64Value::UInt
  • Numbers with decimals or scientific notation → Value::Float
Source

pub fn from_json_with_schemas(json: &str) -> Result<Self>

Parse from JSON string with automatic schema inference.

This variant analyzes the JSON structure and automatically:

  • Detects arrays of uniformly-structured objects
  • Infers schema names from parent keys (e.g., “products” → “product”)
  • Generates @struct definitions for uniform arrays
  • Enables @table format output when serialized

Use to_tl_with_schemas() to serialize with the inferred schemas.

Source

pub fn to_tl_with_schemas(&self) -> String

Serialize to TeaLeaf text format with schemas.

If schemas are present (either from parsing or inference), outputs @struct definitions and uses @table format for matching arrays.

If this document represents a root-level JSON array (from from_json), the output will include @root-array directive for round-trip fidelity.

Source

pub fn to_json(&self) -> Result<String>

Convert to JSON string (pretty-printed).

§Stability Policy - TeaLeaf→JSON Fixed Representations

Special TeaLeaf types serialize to JSON with these stable formats:

TeaLeaf TypeJSON Format
Bytes"0xcafef00d" (lowercase hex with 0x prefix)
Timestamp"2024-01-15T10:30:00.123Z" (ISO 8601 UTC)
Ref{"$ref": "key_name"}
Tagged{"$tag": "tag_name", "$value": <value>}
Map[[key1, val1], [key2, val2], ...]
Float NaNnull (JSON has no NaN)
Float ±Infnull (JSON has no Infinity)

These representations are contractually stable and will not change.

Source

pub fn to_json_compact(&self) -> Result<String>

Convert to compact JSON string (no pretty printing)

Source

pub fn set_root_array(&mut self, is_root_array: bool)

Set whether the document represents a root-level array.

Source

pub fn from_reader(reader: &Reader) -> Result<Self>

Create a TeaLeaf document from a binary Reader.

Reads all sections from the reader and carries schemas and unions through.

Source

pub fn from_dto<T: ToTeaLeaf>(key: &str, dto: &T) -> Self

Create a TeaLeaf document from a single DTO.

The DTO is placed under the given key in the document data map. Schemas are automatically collected from the DTO type.

Source

pub fn from_dto_array<T: ToTeaLeaf>(key: &str, items: &[T]) -> Self

Create a TeaLeaf document from a slice of DTOs.

The array is placed under the given key and schemas are collected from the element type.

Source

pub fn to_dto<T: FromTeaLeaf>(&self, key: &str) -> Result<T>

Extract a DTO from this document by key.

Source

pub fn to_dto_vec<T: FromTeaLeaf>(&self, key: &str) -> Result<Vec<T>>

Extract all values under a key as Vec<T>.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.