use flow_abi::{Catalog, Signature, TypeSpec, description};
use flow_types::RecordShape;
use flow_types::{Body, Decl, Identity, ModulePath, Segments, Tree, Type};
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(),
}
}
pub(crate) fn tool(name: &str) -> description::Identity {
description::Identity::of(&local(name))
}
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(),
),
}
}
pub(crate) fn newtype(name: &str, base: Type, params: &[&str]) -> Decl {
Decl {
body: Body::Newtype(base),
..record(name, &[], params)
}
}
pub(crate) fn catalog(decls: Vec<Decl>) -> Catalog {
Catalog::std(decls).expect("regular test declarations")
}
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")
}
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,
}
}
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
}
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")
}