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
//! std's `url.Url` and `media.MediaType` are newtypes with a form of their
//! own: each is encoded as its normal text, JSON decoding reads any spelling
//! of the form as the one value it denotes and refuses text outside it, and
//! the binary encoding is the normal text and nothing else (`abi.md`,
//! *Decoding*, *Schemas*; `stdlib.md`, *`url`*, *`media`*).

use flow_abi_testing::catalog as common;

use common::*;
use flow_abi::{Limits, Mode, decode_bytes, encode_bytes, schema};
use flow_types::Type;

fn url() -> Type {
    named(URL)
}

fn media() -> Type {
    named(MEDIA_TYPE)
}

/// `value` at `ty` in the binary encoding.
fn bytes_of(ty: &Type, value: &flow_value::Value) -> Vec<u8> {
    encode_bytes(&forms_catalog(), ty, value).expect("encodes")
}

/// What reading the text `spelled`, binary-encoded, at `ty` gives: its text,
/// or the path and problem of the refusal.
fn binary_read(ty: &Type, spelled: &str) -> Result<String, (String, String)> {
    let bytes = bytes_of(&Type::Text, &text(spelled));
    decode_bytes(&forms_catalog(), ty, &bytes, Mode::Reply, &Limits::LANGUAGE)
        .map(|value| text_of(&value).to_owned())
        .map_err(|error| (error.path.to_string(), error.problem))
}

// === url.Url ================================================================

#[test]
fn a_url_is_encoded_as_its_normal_text_exactly_as_its_base() {
    let catalog = forms_catalog();
    assert_eq!(
        enc(&catalog, &url(), &text("http://example.com/")),
        "\"http://example.com/\""
    );
    assert_eq!(
        bytes_of(&url(), &text("http://example.com/")),
        bytes_of(&Type::Text, &text("http://example.com/"))
    );
}

#[test]
fn json_decoding_reads_any_spelling_of_a_url_as_the_one_url_it_denotes() {
    let catalog = forms_catalog();
    for (spelled, normal) in [
        ("HTTP://Example.COM", "http://example.com/"),
        ("http://h/a/../b?%7e", "http://h/b?~"),
        ("mailto:ada@example.com", "mailto:ada@example.com"),
    ] {
        assert_eq!(
            reread(&catalog, &url(), &format!("\"{spelled}\"")),
            format!("\"{normal}\""),
            "{spelled:?}"
        );
        assert_eq!(flow_abi::normal_url(spelled).as_deref(), Some(normal));
    }
}

#[test]
fn a_string_that_is_not_a_url_does_not_decode_as_a_url() {
    let catalog = forms_catalog();
    for spelled in ["/relative", "http://a b/", "", "http://bücher.example/"] {
        assert_eq!(
            refusal(&catalog, &url(), &format!("\"{spelled}\"")),
            (
                String::new(),
                format!("is \"{spelled}\", which is not a URL")
            )
        );
    }
}

#[test]
fn the_binary_encoding_of_a_url_is_its_normal_text_and_nothing_else() {
    assert_eq!(
        binary_read(&url(), "http://example.com/"),
        Ok("http://example.com/".to_owned())
    );
    assert_eq!(
        binary_read(&url(), "HTTP://Example.COM"),
        Err((
            String::new(),
            "is a URL written \"HTTP://Example.COM\", which is not its normal form \"http://example.com/\""
                .to_owned()
        ))
    );
    assert_eq!(
        binary_read(&url(), "/relative"),
        Err((
            String::new(),
            "is \"/relative\", which is not a URL".to_owned()
        ))
    );
}

#[test]
fn only_std_s_url_has_the_form_not_a_type_another_repository_names_url() {
    let catalog = forms_catalog();
    assert_eq!(
        reread(&catalog, &named(OTHER_URL), "\"HTTP://A B\""),
        "\"HTTP://A B\""
    );
}

#[test]
fn a_url_s_schema_is_a_string_with_the_format_uri() {
    let catalog = forms_catalog();
    let written = schema(&catalog, &url()).expect("a schema").canonical();
    assert!(
        written.contains(r#"{"format":"uri","type":"string"}"#),
        "{written}"
    );
}

// === media.MediaType ========================================================

#[test]
fn json_decoding_reads_any_spelling_of_a_media_type_as_the_one_it_denotes() {
    let catalog = forms_catalog();
    assert_eq!(
        reread(&catalog, &media(), r#""Text/HTML ; Charset=\"utf-8\"""#),
        r#""text/html;charset=utf-8""#
    );
    assert_eq!(flow_abi::normal_media_type("A/B;;").as_deref(), Some("a/b"));
}

#[test]
fn a_string_that_is_not_a_media_type_does_not_decode_as_a_media_type() {
    let catalog = forms_catalog();
    assert_eq!(
        refusal(&catalog, &media(), r#""text""#),
        (
            String::new(),
            r#"is "text", which is not a media type"#.to_owned()
        )
    );
}

#[test]
fn the_binary_encoding_of_a_media_type_is_its_normal_text_and_nothing_else() {
    assert_eq!(
        binary_read(&media(), "text/html"),
        Ok("text/html".to_owned())
    );
    assert_eq!(
        binary_read(&media(), "Text/HTML"),
        Err((
            String::new(),
            r#"is a media type written "Text/HTML", which is not its normal form "text/html""#
                .to_owned()
        ))
    );
}

#[test]
fn a_media_type_s_schema_is_its_base_s_with_no_format() {
    let catalog = forms_catalog();
    let written = schema(&catalog, &media()).expect("a schema").canonical();
    assert!(written.contains(r#"{"type":"string"}"#), "{written}");
    assert!(!written.contains("format"), "{written}");
}

// === Inside other values ====================================================

#[test]
fn a_value_out_of_its_form_anywhere_in_a_reply_does_not_decode_and_names_its_place() {
    let catalog = forms_catalog();
    assert_eq!(
        reread(
            &catalog,
            &named(LINK),
            r#"{"href": "HTTP://A", "kind": "A/B"}"#
        ),
        r#"{"href":"http://a/","kind":"a/b"}"#
    );
    assert_eq!(
        refusal(
            &catalog,
            &named(LINK),
            r#"{"href": "http://a/", "kind": "a"}"#
        )
        .0,
        "kind"
    );
    let bytes = [
        bytes_of(&Type::Text, &text("HTTP://A")),
        bytes_of(&Type::Text, &text("a/b")),
    ]
    .concat();
    let refused = decode_bytes(
        &catalog,
        &named(LINK),
        &bytes,
        Mode::Reply,
        &Limits::LANGUAGE,
    )
    .expect_err("a URL out of its form");
    assert_eq!(refused.path, "href");
}

#[test]
fn a_value_out_of_its_form_is_not_encoded_in_either_form() {
    let catalog = forms_catalog();
    let link = rec(&catalog, LINK, vec![text("HTTP://A"), text("a/b")]);
    let refused =
        |error: flow_abi::EncodeError| (error.path.to_string(), error.kind, error.problem);
    let out_of_form = (
        "href".to_owned(),
        flow_abi::EncodeErrorKind::Mismatch,
        "is a URL written \"HTTP://A\", which is not its normal form \"http://a/\"".to_owned(),
    );
    assert_eq!(
        encode_bytes(&catalog, &named(LINK), &link).map_err(refused),
        Err(out_of_form.clone())
    );
    assert_eq!(
        flow_abi::encode(&catalog, &named(LINK), &link).map_err(refused),
        Err(out_of_form)
    );
}