use std::fmt;
use std::str::FromStr;
use flow_span::{CallRef, ParsePlaceError, RunId};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct CallKey {
run: RunId,
call: CallRef,
}
impl CallKey {
pub fn new(run: RunId, call: CallRef) -> CallKey {
CallKey { run, call }
}
pub fn run(&self) -> &RunId {
&self.run
}
pub fn call(&self) -> &CallRef {
&self.call
}
}
impl fmt::Display for CallKey {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}/{}", self.run, self.call)
}
}
impl FromStr for CallKey {
type Err = KeyError;
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))
}
}
#[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 {
NoSlash {
text: String,
},
Run(ParsePlaceError),
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 {}