use super::*;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandErrorKind {
Failed {
code: String,
args: Vec<(&'static str, DiagArg)>,
},
IntegerOverflow,
NonFiniteFloat,
FloatOutOfRange,
DivisionByZero,
Interrupted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandError {
pub(super) kind: CommandErrorKind,
}
impl CommandError {
pub fn with_code<I>(code: impl Into<String>, args: I) -> Self
where
I: IntoIterator<Item = (&'static str, DiagArg)>,
{
Self {
kind: CommandErrorKind::Failed {
code: code.into(),
args: args.into_iter().collect(),
},
}
}
#[must_use]
pub fn arg(mut self, name: &'static str, value: impl Into<DiagArg>) -> Self {
if let CommandErrorKind::Failed { args, .. } = &mut self.kind {
args.push((name, value.into()));
}
self
}
pub fn integer_overflow() -> Self {
Self {
kind: CommandErrorKind::IntegerOverflow,
}
}
pub fn non_finite_float() -> Self {
Self {
kind: CommandErrorKind::NonFiniteFloat,
}
}
pub fn float_out_of_range() -> Self {
Self {
kind: CommandErrorKind::FloatOutOfRange,
}
}
pub fn division_by_zero() -> Self {
Self {
kind: CommandErrorKind::DivisionByZero,
}
}
pub fn interrupted() -> Self {
Self {
kind: CommandErrorKind::Interrupted,
}
}
pub fn kind(&self) -> &CommandErrorKind {
&self.kind
}
pub fn code(&self) -> &str {
match &self.kind {
CommandErrorKind::Failed { code, .. } => code,
CommandErrorKind::IntegerOverflow => "integer_overflow",
CommandErrorKind::NonFiniteFloat => "non_finite_float",
CommandErrorKind::FloatOutOfRange => "float_out_of_range",
CommandErrorKind::DivisionByZero => "division_by_zero",
CommandErrorKind::Interrupted => "interrupted",
}
}
pub fn args(&self) -> &[(&'static str, DiagArg)] {
match &self.kind {
CommandErrorKind::Failed { args, .. } => args,
_ => &[],
}
}
pub(crate) fn into_vm_kind(self) -> VmErrorKind {
match self.kind {
CommandErrorKind::Failed { code, args } => VmErrorKind::CommandFailed { code, args },
CommandErrorKind::IntegerOverflow => VmErrorKind::IntegerOverflow,
CommandErrorKind::NonFiniteFloat => VmErrorKind::NonFiniteFloat,
CommandErrorKind::FloatOutOfRange => VmErrorKind::FloatOutOfRange,
CommandErrorKind::DivisionByZero => VmErrorKind::DivisionByZero,
CommandErrorKind::Interrupted => VmErrorKind::Interrupted,
}
}
}
impl std::fmt::Display for CommandError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.code())?;
let args = self.args();
if !args.is_empty() {
write!(f, " (")?;
for (i, (name, value)) in args.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{name}={value}")?;
}
write!(f, ")")?;
}
Ok(())
}
}
impl std::error::Error for CommandError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MigrateError {
pub(super) message: String,
}
impl MigrateError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
pub fn unsupported_version(old_version: u32) -> Self {
Self::new(format!("unsupported slice version {old_version}"))
}
pub fn message(&self) -> &str {
&self.message
}
}
impl std::fmt::Display for MigrateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for MigrateError {}
pub fn decode_slice<T: DeserializeOwned>(raw: &[u8]) -> Result<T, MigrateError> {
postcard::from_bytes(raw).map_err(|e| MigrateError::new(format!("decode: {e}")))
}
pub fn encode_slice<T: Serialize>(value: &T) -> Result<Vec<u8>, MigrateError> {
postcard::to_stdvec(value).map_err(|e| MigrateError::new(format!("encode: {e}")))
}