1use std::fmt;
12use std::str::FromStr;
13
14use flow_span::{CallRef, ParsePlaceError, RunId};
15
16#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
18pub struct CallKey {
19 run: RunId,
20 call: CallRef,
21}
22
23impl CallKey {
24 pub fn new(run: RunId, call: CallRef) -> CallKey {
26 CallKey { run, call }
27 }
28
29 pub fn run(&self) -> &RunId {
31 &self.run
32 }
33
34 pub fn call(&self) -> &CallRef {
36 &self.call
37 }
38}
39
40impl fmt::Display for CallKey {
41 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 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#[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 NoSlash {
73 text: String,
75 },
76 Run(ParsePlaceError),
78 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 {}