Skip to main content

flow_toolkit/
key.rs

1//! A call's key: the run's id and the call's id together (`history.md`,
2//! *Keys*; `abi.md`, *Toolkits*).
3//!
4//! A key is an address, not a permission. A Tool hands it to the outside
5//! world as an idempotency key or a callback address, and [`reply`] answers
6//! the call it names. Its text form is the run's id, then `/`, then the
7//! call's id: `7f3a91c04be2…/root/1#1`.
8//!
9//! [`reply`]: crate::reply()
10
11use std::fmt;
12use std::str::FromStr;
13
14use flow_span::{CallRef, ParsePlaceError, RunId};
15
16/// The key of one call: which run, and which call in it.
17#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
18pub struct CallKey {
19    run: RunId,
20    call: CallRef,
21}
22
23impl CallKey {
24    /// The key of the call `call` of the run `run`.
25    pub fn new(run: RunId, call: CallRef) -> CallKey {
26        CallKey { run, call }
27    }
28
29    /// The run's id.
30    pub fn run(&self) -> &RunId {
31        &self.run
32    }
33
34    /// The call's id.
35    pub fn call(&self) -> &CallRef {
36        &self.call
37    }
38}
39
40impl fmt::Display for CallKey {
41    /// The key's text form: the run's id, `/`, the call's id.
42    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(formatter, "{}/{}", self.run, self.call)
44    }
45}
46
47impl FromStr for CallKey {
48    type Err = KeyError;
49
50    /// Reads a key's text form. A run's id is 32 hexadecimal digits and holds
51    /// no `/`, so the first `/` ends it.
52    fn from_str(text: &str) -> Result<CallKey, KeyError> {
53        let Some((run, call)) = text.split_once('/') else {
54            return Err(KeyError::NoSlash {
55                text: text.to_owned(),
56            });
57        };
58        let run = RunId::parse(run).map_err(KeyError::Run)?;
59        let call = CallRef::parse(call).map_err(KeyError::Call)?;
60        Ok(CallKey::new(run, call))
61    }
62}
63
64/// Text that is not a call's key, and which part of it is wrong.
65#[derive(Clone, PartialEq, Eq, Debug)]
66#[expect(
67    clippy::exhaustive_enums,
68    reason = "a key's text has three parts to get wrong, which callers match on"
69)]
70pub enum KeyError {
71    /// The text holds no `/`, so it has no run's id and call's id to part.
72    NoSlash {
73        /// The text, as it was given.
74        text: String,
75    },
76    /// What comes before the first `/` is not a run's id.
77    Run(ParsePlaceError),
78    /// What comes after the first `/` is not a call's id.
79    Call(ParsePlaceError),
80}
81
82impl fmt::Display for KeyError {
83    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            KeyError::NoSlash { text } => write!(
86                formatter,
87                "{text:?} is not a call's key: the run's id, '/', then the call's id"
88            ),
89            KeyError::Run(error) | KeyError::Call(error) => write!(formatter, "{error}"),
90        }
91    }
92}
93
94impl std::error::Error for KeyError {}