flow-toolkit 0.1.0-rc.5

Ordinary typed Rust functions and exact native data for Flow Tools, served from a Rust library's one launcher.
Documentation
//! Type descriptions the unit tests serve and call, built as the compiler
//! builds them: from a catalog of declarations and a signature.

use flow_abi::{Catalog, Signature, TypeSpec, description};
use flow_types::RecordShape;
use flow_types::{Body, Decl, Identity, ModulePath, Segments, Tree, Type};

/// The identity of `name`, declared in the local tree's `main` module.
pub(crate) fn local(name: &str) -> Identity {
    Identity {
        origin: ModulePath::new(
            Tree::Local,
            None,
            "main".parse::<Segments>().expect("a test path is a path"),
        ),
        version: None,
        name: name.into(),
    }
}

/// The identity `describe` lists for the Tool `name`.
pub(crate) fn tool(name: &str) -> description::Identity {
    description::Identity::of(&local(name))
}

/// The record `name` over `params`, with `fields` in this order.
pub(crate) fn record(name: &str, fields: &[(&str, Type)], params: &[&str]) -> Decl {
    Decl {
        lexical_ordinal: 1,
        identity: local(name),
        doc: None,
        members: Vec::new(),
        params: params.iter().map(|param| (*param).to_owned()).collect(),
        opaque: false,
        body: Body::Record(
            RecordShape::new(name, fields.iter().map(|(field, _)| *field)),
            fields.iter().map(|(_, ty)| ty.clone()).collect(),
        ),
    }
}

/// The newtype `name` over `base`.
pub(crate) fn newtype(name: &str, base: Type, params: &[&str]) -> Decl {
    Decl {
        body: Body::Newtype(base),
        ..record(name, &[], params)
    }
}

/// The catalog of `decls`, the `n`th `DeclId(n)`, with std's `Option` and
/// `json.Value`.
pub(crate) fn catalog(decls: Vec<Decl>) -> Catalog {
    Catalog::std(decls).expect("regular test declarations")
}

/// The description of a Tool over `generic` taking `params` and answering
/// `result`, whose declarations `catalog` holds.
pub(crate) fn spec(
    catalog: &Catalog,
    generic: &[&str],
    params: &[(&str, Type)],
    result: Type,
) -> TypeSpec {
    TypeSpec::from_catalog(catalog, &signature(generic, params, result))
        .expect("a description of the signature")
}

/// The signature over `generic` taking `params` and answering `result`.
pub(crate) fn signature(generic: &[&str], params: &[(&str, Type)], result: Type) -> Signature {
    Signature {
        generic: generic.iter().map(|name| (*name).to_owned()).collect(),
        params: params
            .iter()
            .map(|(name, ty)| ((*name).to_owned(), ty.clone()))
            .collect(),
        result,
    }
}

/// `value`'s bytes at `ty`, a type no declaration of its own needs.
pub(crate) fn bytes<T: flow_toolkit::Shaped>(value: &T, ty: &Type) -> Vec<u8> {
    let catalog = catalog(Vec::new());
    let mut out = Vec::new();
    flow_abi::write_shaped(&mut out, &catalog, ty, value).expect("its bytes");
    out
}

/// The value the JSON `json` spells at `ty`, a type no declaration of
/// its own needs.
pub(crate) fn from_json<T: flow_toolkit::Shaped>(ty: &Type, json: &flow_abi::Json) -> T {
    let catalog = catalog(Vec::new());
    let value = flow_abi::decode(
        &catalog,
        ty,
        json,
        flow_abi::Mode::Rebuild,
        &flow_abi::Limits::default(),
    )
    .expect("a value of the type");
    flow_abi::of_shaped_value::<T>(&value, &catalog, ty).expect("one of the Rust type's")
}