flow-abi 0.1.0

The value encoding at the host boundary: type-directed JSON encoding and decoding, the canonical form and its hash, JSON Schemas, and Tool declaration fingerprints.
Documentation
//! A `@tool` declaration's identity and fingerprint (`abi.md`, *Identity
//! and fingerprints*).
//!
//! The fingerprint is the SHA-256 hash of the canonical signature: the
//! canonical JSON of the parameters' names and schemas in declaration order,
//! the success and error types' schemas, and the type parameters' names, with
//! every `description` left out and each `$defs` key written without its
//! repository's version. So it moves with the shape of the signature
//! and of every type the signature reaches, and not with a doc comment, the
//! source's layout, or a new version of the repository that leaves the
//! signature alone.

use crate::encode::Digest;
use crate::error::EncodeError;
use crate::json::Json;
use crate::schema::{Purpose, Writer};
use crate::types::{Catalog, Identity, Type};

/// A `@tool` declaration's signature, for a declaration returning
/// `Result<ok, err>`. [`Type::Param`] counts `generic`.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Signature {
    /// Each parameter's name and type, in declaration order.
    pub params: Vec<(String, Type)>,
    /// The success type.
    pub ok: Type,
    /// The declared error type.
    pub err: Type,
    /// The type parameters' names, in declaration order.
    pub generic: Vec<String>,
}

/// A `@tool` declaration, as the host binds it.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ToolDecl {
    /// Where it lives and its name.
    pub identity: Identity,
    /// Its signature.
    pub signature: Signature,
}

impl Signature {
    /// The canonical signature's JSON.
    pub fn canonical(&self, catalog: &Catalog) -> Result<Json, EncodeError> {
        self.written(catalog, Purpose::Signature)
    }

    /// The signature with its schemas written to be read: the canonical
    /// signature's members, each schema with its doc comments as
    /// `description`s and its `$defs` keys with their versions. An
    /// implementation lists this beside its fingerprint in `describe`
    /// (`abi.md`, *The Tool process protocol*).
    pub fn described(&self, catalog: &Catalog) -> Result<Json, EncodeError> {
        self.written(catalog, Purpose::Document)
    }

    fn written(&self, catalog: &Catalog, purpose: Purpose) -> Result<Json, EncodeError> {
        let schema = |ty: &Type| Writer::new(catalog, &self.generic, purpose).document(ty);
        let params = self
            .params
            .iter()
            .map(|(name, ty)| Ok(Json::Array(vec![Json::String(name.clone()), schema(ty)?])))
            .collect::<Result<_, EncodeError>>()?;
        let generic = self.generic.iter().cloned().map(Json::String).collect();
        Ok(Json::Object(vec![
            ("params".into(), Json::Array(params)),
            ("ok".into(), schema(&self.ok)?),
            ("err".into(), schema(&self.err)?),
            ("generic".into(), Json::Array(generic)),
        ]))
    }

    /// The fingerprint: the hash of the canonical signature's text.
    pub fn fingerprint(&self, catalog: &Catalog) -> Result<Digest, EncodeError> {
        Ok(Digest::of(self.canonical(catalog)?.canonical().as_bytes()))
    }
}