use astrs_wire::IdError;
pub type OpResult<T> = Result<T, OpError>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum OpError {
#[error("output {id:?} is not a valid data id: {source}")]
InvalidOutputId {
id: String,
#[source]
source: IdError,
},
#[error(transparent)]
Encode(#[from] astrs_data::DataError),
#[error(transparent)]
Ipc(#[from] astrs_data::ipc::IpcError),
#[error("operator {name:?} is already registered")]
DuplicateOperator {
name: String,
},
#[error("no operator registered as {name:?}")]
UnknownOperator {
name: String,
},
#[error("operator failed: {message}")]
Failed {
message: String,
},
}
impl OpError {
#[must_use]
pub fn invalid_output_id(id: impl Into<String>, source: IdError) -> Self {
Self::InvalidOutputId {
id: id.into(),
source,
}
}
#[must_use]
pub fn failed(message: impl Into<String>) -> Self {
Self::Failed {
message: message.into(),
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
#[test]
fn invalid_output_id_reports_the_offending_name() {
let source = astrs_wire::DataId::new("bad id").unwrap_err();
let err = OpError::invalid_output_id("bad id", source);
let message = err.to_string();
assert!(message.starts_with("output \"bad id\" is not a valid data id: "));
assert!(matches!(err, OpError::InvalidOutputId { .. }));
}
#[test]
fn duplicate_and_unknown_operator_messages() {
assert_eq!(
OpError::DuplicateOperator {
name: "crop".to_owned()
}
.to_string(),
"operator \"crop\" is already registered"
);
assert_eq!(
OpError::UnknownOperator {
name: "nope".to_owned()
}
.to_string(),
"no operator registered as \"nope\""
);
}
#[test]
fn encode_and_ipc_errors_convert_via_from() {
let data_err: OpError = astrs_data::DataError::MessageRowCount { actual: 2 }.into();
assert!(matches!(data_err, OpError::Encode(_)));
}
#[test]
fn failed_builds_a_readable_message() {
assert_eq!(OpError::failed("boom").to_string(), "operator failed: boom");
}
}