flow-abi 0.1.0-rc.5

The host boundary's value encoding: the binary encoding and the JSON form, the canonical form and its hash, JSON Schemas, Tool signatures, and the answers a Tool call passes.
Documentation
//! A value's depth is bounded only by the limits a host sets, so a host may
//! raise them far past the native stack. Every walk over a value, encoding
//! it as text or bytes and decoding it back, grows no native stack with that
//! depth.

use flow_abi::{Limits, Mode, canonical, decode_bytes, decode_text, encode, encode_bytes};
use flow_abi_testing::catalog::{TREE, catalog, ctor, dict, raw, some, text, tree};
use flow_types::Type;
use flow_value::{Value, equal};

/// How deep the deep values nest.
const DEPTH: usize = 100_000;

/// Limits that admit a value `depth` deep.
fn deep_limits(depth: usize) -> Limits {
    Limits {
        depth,
        ..Limits::default()
    }
}

/// A value `DEPTH` levels deep around `1`, its type, and its canonical text:
/// each level in turn a list, an option, a dictionary and a tuple.
fn deep() -> (Type, Value, String) {
    let mut ty = Type::Int;
    let mut value = Value::int(1);
    let mut opens = Vec::new();
    let mut closes = Vec::new();
    for level in 0..DEPTH {
        let (open, close) = match level % 4 {
            0 => {
                ty = Type::list(ty);
                value = Value::list([value]);
                ("[", "]")
            }
            1 => {
                ty = Type::option(ty);
                value = some(value);
                ("{\"Some\":", "}")
            }
            2 => {
                ty = Type::dict(Type::Text, ty);
                value = dict(vec![(text("k"), value)]);
                ("{\"k\":", "}")
            }
            _ => {
                ty = Type::Tuple(vec![Type::Int, ty]);
                value = Value::tuple([Value::int(2), value]);
                ("[2,", "]")
            }
        };
        opens.push(open);
        closes.push(close);
    }
    let text = format!(
        "{}1{}",
        opens.iter().rev().copied().collect::<String>(),
        closes.concat()
    );
    (ty, value, text)
}

#[test]
fn a_value_one_hundred_thousand_deep_encodes_as_text_and_decodes_on_a_small_stack() {
    flow_testing::on_a_small_stack(|| {
        let (ty, value, text) = deep();
        let catalog = catalog();
        assert_eq!(
            canonical(&catalog, &ty, &value).expect("the value encodes"),
            text,
            "each level is written around the one inside it"
        );
        let json = encode(&catalog, &ty, &value).expect("the value encodes");
        assert_eq!(json.canonical(), text, "the tree is the text's");
        let read = decode_text(&catalog, &ty, &text, Mode::Decode, &deep_limits(DEPTH))
            .expect("the text decodes");
        assert!(
            equal(&read, &value).expect("data"),
            "the text decodes to the value"
        );
    });
}

/// A `Tree<Int>` value `DEPTH` levels around a `Leaf(1)`, each `Node` and
/// the list of one tree it holds a level, and its canonical text.
fn deep_tree() -> (Type, Value, String) {
    let catalog = catalog();
    let value = (0..DEPTH / 2).fold(ctor(&catalog, TREE, 0, vec![Value::int(1)]), |value, _| {
        ctor(&catalog, TREE, 1, vec![Value::list([value])])
    });
    let text = format!(
        "{}{{\"Leaf\":1}}{}",
        "{\"Node\":[".repeat(DEPTH / 2),
        "]}".repeat(DEPTH / 2)
    );
    (tree(Type::Int), value, text)
}

#[test]
fn a_declared_value_one_hundred_thousand_deep_encodes_and_decodes_on_a_small_stack() {
    flow_testing::on_a_small_stack(|| {
        let (ty, value, text) = deep_tree();
        let catalog = catalog();
        assert_eq!(
            canonical(&catalog, &ty, &value).expect("the value encodes"),
            text,
            "each node is written around the tree it holds"
        );
        let read = decode_text(&catalog, &ty, &text, Mode::Decode, &deep_limits(DEPTH + 1))
            .expect("the text decodes");
        assert!(
            equal(&read, &value).expect("data"),
            "the text decodes to the value"
        );
        let bytes = encode_bytes(&catalog, &ty, &value).expect("the value encodes");
        let read = decode_bytes(&catalog, &ty, &bytes, Mode::Decode, &deep_limits(DEPTH + 1))
            .expect("the bytes decode");
        assert!(
            equal(&read, &value).expect("data"),
            "the bytes decode to the value"
        );
    });
}

#[test]
fn a_value_one_hundred_thousand_deep_encodes_as_bytes_and_decodes_on_a_small_stack() {
    flow_testing::on_a_small_stack(|| {
        let (ty, value, _) = deep();
        let catalog = catalog();
        let bytes = encode_bytes(&catalog, &ty, &value).expect("the value encodes");
        let read = decode_bytes(&catalog, &ty, &bytes, Mode::Decode, &deep_limits(DEPTH))
            .expect("the bytes decode");
        assert!(
            equal(&read, &value).expect("data"),
            "the bytes decode to the value"
        );
    });
}

#[test]
fn a_json_value_one_hundred_thousand_deep_encodes_and_decodes_on_a_small_stack() {
    flow_testing::on_a_small_stack(|| {
        let value = (0..DEPTH).fold(raw(0, vec![]), |value, _| {
            raw(4, vec![Value::list([value])])
        });
        let catalog = catalog();
        let text = format!("{}null{}", "[".repeat(DEPTH), "]".repeat(DEPTH));
        assert_eq!(
            canonical(&catalog, &Type::Json, &value).expect("the value encodes"),
            text,
            "each array holds the one inside it"
        );
        let read = decode_text(
            &catalog,
            &Type::Json,
            &text,
            Mode::Decode,
            &deep_limits(2 * DEPTH + 1),
        )
        .expect("the text decodes");
        assert!(
            equal(&read, &value).expect("data"),
            "the text decodes to the value"
        );
        let bytes = encode_bytes(&catalog, &Type::Json, &value).expect("the value encodes");
        let read = decode_bytes(
            &catalog,
            &Type::Json,
            &bytes,
            Mode::Decode,
            &deep_limits(2 * DEPTH + 1),
        )
        .expect("the bytes decode");
        assert!(
            equal(&read, &value).expect("data"),
            "the bytes decode to the value"
        );
    });
}