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
//! A library whose Tools take another library's type (`abi.md`, *Imports,
//! never copies*): `payments` depends on `plans` by a path in the same
//! tree, so its contract imports `plans.Plan` from the contract of `plans`
//! and declares nothing for it. A program reaching both libraries checks,
//! binds each Tool against its contract and runs, a `Plan` that one library
//! answered crossing into the other.

mod common;

use std::path::Path;

use common::flow;
use tempfile::TempDir;

fn write(path: &Path, text: &str) {
    std::fs::write(path, text).expect("the fixture file is written");
}

#[test]
fn a_type_of_a_path_dependency_is_imported_and_crosses_between_two_libraries() {
    let project = TempDir::with_prefix("flow-library-types").expect("a directory");
    let tree = project.path();
    for name in ["plans", "payments"] {
        common::new_library(common::rust_toolkit(), &tree.join(name));
    }
    write(
        &tree.join("plans/src/lib.rs"),
        "use flow_toolkit::{Flow, tool};\n\n\
         /// A subscription plan.\n\
         #[derive(Flow, Debug)]\n\
         pub struct Plan {\n    pub name: String,\n    pub cents: flow_toolkit::Int,\n}\n\n\
         /// The plan a customer starts on.\n\
         #[tool]\n\
         pub fn starter() -> Plan {\n    Plan { name: \"starter\".into(), cents: 500.into() }\n}\n",
    );
    let manifest = tree.join("payments/Cargo.toml");
    let text = std::fs::read_to_string(&manifest).unwrap();
    write(
        &manifest,
        &text.replace(
            "[dependencies]\n",
            "[dependencies]\nplans = { path = \"../plans\" }\n",
        ),
    );
    // A new dependency changes the lock, which its author updates: the
    // toolkit builds against the lock as it is.
    flow_testing::lock_from_repository(&tree.join("payments"));
    write(
        &tree.join("payments/src/lib.rs"),
        "use flow_toolkit::tool;\n\n\
         /// What `plan` costs, in cents, for `reason`.\n\
         #[tool]\n\
         pub fn charge(plan: plans::Plan, reason: String) -> flow_toolkit::Int {\n    let _ = reason;\n    plan.cents\n}\n",
    );
    for name in ["plans", "payments"] {
        let built = flow(&["build", tree.join(name).to_str().unwrap()]);
        assert_eq!(built.status.code(), Some(0), "{}", common::said(&built));
    }
    assert_eq!(
        std::fs::read_to_string(tree.join("payments/contract/lib.flow")).unwrap(),
        "// Generated by flow build from rust module payments. Do not edit.\n\
         use \"../plans\"\n\n\
         /// What `plan` costs, in cents, for `reason`.\n\
         // from src/lib.rs: charge\n\
         pub flow charge(plan: plans.Plan, reason: Text) -> Int\n"
    );
    assert!(
        std::fs::read_to_string(tree.join("plans/contract/lib.flow"))
            .unwrap()
            .contains("pub type Plan {\n    pub name: Text,\n    pub cents: Int,\n}\n"),
    );

    let program = tree.join("main.flow");
    write(
        &program,
        "use \"./plans\"\n\
         use \"./payments\"\n\n\
         pub flow main() -> Result<Int, ToolProblem> = {\n    \
             let plan = plans.starter()?\n    \
             payments.charge(plan, \"renewal\")\n}\n",
    );
    let checked = flow(&["check", program.to_str().unwrap()]);
    assert_eq!(checked.status.code(), Some(0), "{}", common::said(&checked));
    let run = flow(&["run", program.to_str().unwrap()]);
    assert_eq!(run.status.code(), Some(0), "{}", common::said(&run));
    assert!(
        String::from_utf8_lossy(&run.stdout).contains("500"),
        "{}",
        common::said(&run)
    );
}