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
//! The codec [`flow_abi::wire!`] declares: a message nests as deeply as its
//! bytes do with the native stack the same at every depth, a constructor
//! takes its parts by position in any number, two declared types of one
//! name project to two declarations, and a stream ends at a value that
//! reads no bytes.

use flow_abi::description::Term;
use flow_abi::wire::{Declared, Wire};
use flow_abi::{Catalog, write_shaped};
use flow_value::Int;

/// How deep the deep term nests.
const DEPTH: usize = 100_000;

#[test]
fn a_term_nested_a_hundred_thousand_deep_reads_and_drops_on_a_small_stack() {
    let mut bytes = vec![10; DEPTH];
    bytes.push(0);
    flow_testing::on_a_small_stack(|| {
        let term = Term::decode(&bytes).expect("a term of any depth reads");
        let mut depth = 0;
        let mut at = &term;
        while let Term::List(item) = at {
            depth += 1;
            at = item;
        }
        assert_eq!((depth, matches!(at, Term::Int)), (DEPTH, true));
    });
}

mod one {
    flow_abi::wire! {
        /// A field, as one module declares it.
        #[derive(Debug, PartialEq)]
        pub struct Field {
            pub n: flow_value::Int,
        }
    }
}

mod two {
    flow_abi::wire! {
        /// A field of the same name, as another module declares it.
        #[derive(Debug, PartialEq)]
        pub struct Field {
            pub on: bool,
            pub name: String,
        }
    }
}

flow_abi::wire! {
    /// A message reaching both.
    #[derive(Debug, PartialEq)]
    pub struct Both {
        /// The first module's.
        pub first: one::Field,
        /// The second module's.
        pub second: two::Field,
        /// A constructor of parts by position.
        pub third: Shapes,
    }
}

flow_abi::wire! {
    /// Constructors of one, two and three parts by position.
    #[derive(Debug, PartialEq)]
    pub enum Shapes {
        /// One part.
        One(bool),
        /// Two parts.
        Two(bool, String),
        /// Three parts.
        Three(String, bool, one::Field),
    }
}

fn catalog() -> Catalog {
    Catalog::new(Vec::new(), flow_abi_testing::catalog::option_shape(), None).expect("std's Option")
}

#[test]
fn two_declared_types_of_one_name_are_two_declarations() {
    let (catalog, ty) = Both::shape().project(&catalog()).expect("a Flow type");
    let fields: Vec<_> = catalog
        .decls()
        .filter(|(_, decl)| decl.identity.name == "Field")
        .map(|(id, decl)| (id, decl.lexical_ordinal))
        .collect();
    assert_eq!(fields.len(), 2, "{fields:?}");
    assert_ne!(fields[0].0, fields[1].0);
    assert_eq!([fields[0].1, fields[1].1], [1, 2]);
    let both = Both {
        first: one::Field { n: Int::from(7) },
        second: two::Field {
            on: true,
            name: "x".to_owned(),
        },
        third: Shapes::Three("y".to_owned(), false, one::Field { n: Int::from(-1) }),
    };
    let mut value = Vec::new();
    write_shaped(&mut value, &catalog, &ty, &both).expect("its bytes at its type");
    assert_eq!(value, both.bytes());
    assert_eq!(Both::decode(&both.bytes()), Ok(both));
}

#[test]
fn a_constructor_takes_any_number_of_parts_by_position() {
    for shapes in [
        Shapes::One(true),
        Shapes::Two(false, "a".to_owned()),
        Shapes::Three("b".to_owned(), true, one::Field { n: Int::from(3) }),
    ] {
        let (catalog, ty) = Shapes::shape().project(&catalog()).expect("a Flow type");
        let mut value = Vec::new();
        write_shaped(&mut value, &catalog, &ty, &shapes).expect("its bytes at its type");
        assert_eq!(value, shapes.bytes());
        assert_eq!(Shapes::decode(&shapes.bytes()), Ok(shapes));
    }
}

flow_abi::wire! {
    /// A message of no fields, which reads no bytes.
    #[derive(Debug, PartialEq)]
    pub struct Empty {}
}

#[test]
fn a_stream_of_a_message_that_reads_no_bytes_ends_where_it_stands() {
    let (values, rest) = flow_abi::wire::stream::<Empty>(&[1, 2]);
    assert_eq!((values.len(), rest), (0, Some(0)));
}