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 call's key: the run's id and the call's id together (`history.md`,
//! *Keys*; `abi.md`, *Toolkits*).
//!
//! A key is an address, not a permission. A Tool hands it to the outside
//! world as an idempotency key or a callback address, and [`reply`] answers
//! the call it names. Its text form is the run's id, then `/`, then the
//! call's id: `7f3a91c04be2…/root/1#1`.
//!
//! [`reply`]: crate::reply()

use std::fmt;
use std::str::FromStr;

use flow_span::{CallRef, ParsePlaceError, RunId};

/// The key of one call: which run, and which call in it.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct CallKey {
    run: RunId,
    call: CallRef,
}

impl CallKey {
    /// The key of the call `call` of the run `run`.
    pub fn new(run: RunId, call: CallRef) -> CallKey {
        CallKey { run, call }
    }

    /// The run's id.
    pub fn run(&self) -> &RunId {
        &self.run
    }

    /// The call's id.
    pub fn call(&self) -> &CallRef {
        &self.call
    }
}

impl fmt::Display for CallKey {
    /// The key's text form: the run's id, `/`, the call's id.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}/{}", self.run, self.call)
    }
}

impl FromStr for CallKey {
    type Err = KeyError;

    /// Reads a key's text form. A run's id is 32 hexadecimal digits and holds
    /// no `/`, so the first `/` ends it.
    fn from_str(text: &str) -> Result<CallKey, KeyError> {
        let Some((run, call)) = text.split_once('/') else {
            return Err(KeyError::NoSlash {
                text: text.to_owned(),
            });
        };
        let run = RunId::parse(run).map_err(KeyError::Run)?;
        let call = CallRef::parse(call).map_err(KeyError::Call)?;
        Ok(CallKey::new(run, call))
    }
}

/// Text that is not a call's key, and which part of it is wrong.
#[derive(Clone, PartialEq, Eq, Debug)]
#[expect(
    clippy::exhaustive_enums,
    reason = "a key's text has three parts to get wrong, which callers match on"
)]
pub enum KeyError {
    /// The text holds no `/`, so it has no run's id and call's id to part.
    NoSlash {
        /// The text, as it was given.
        text: String,
    },
    /// What comes before the first `/` is not a run's id.
    Run(ParsePlaceError),
    /// What comes after the first `/` is not a call's id.
    Call(ParsePlaceError),
}

impl fmt::Display for KeyError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            KeyError::NoSlash { text } => write!(
                formatter,
                "{text:?} is not a call's key: the run's id, '/', then the call's id"
            ),
            KeyError::Run(error) | KeyError::Call(error) => write!(formatter, "{error}"),
        }
    }
}

impl std::error::Error for KeyError {}