diidi-travel-common-queue 0.1.16

A collection of common utilities and types for the DiiDi project.
Documentation
use thiserror::Error;

pub type QueueResult<T> = Result<T, QueueError>;

#[derive(Debug, Error)]
pub enum QueueError {
  #[error("invalid queue configuration: {0}")]
  InvalidConfig(String),

  #[error("queue provider '{0}' is not registered with the factory")]
  ProviderNotRegistered(String),

  #[error("queue provider '{provider}' does not support feature '{feature}'")]
  Unsupported { provider: &'static str, feature: &'static str },

  #[error("queue serialization error: {0}")]
  Serialization(String),

  #[error("queue backend connection error: {0}")]
  Connection(String),

  #[error("queue backend error: {0}")]
  Backend(String),

  #[error("queue operation timed out")]
  Timeout,
}

impl QueueError {
  pub fn invalid_config(msg: impl Into<String>) -> Self {
    Self::InvalidConfig(msg.into())
  }

  pub fn backend(msg: impl Into<String>) -> Self {
    Self::Backend(msg.into())
  }

  pub fn connection(msg: impl Into<String>) -> Self {
    Self::Connection(msg.into())
  }

  pub fn serialization(msg: impl Into<String>) -> Self {
    Self::Serialization(msg.into())
  }

  pub fn unsupported(provider: &'static str, feature: &'static str) -> Self {
    Self::Unsupported { provider, feature }
  }

  pub fn is_transient(&self) -> bool {
    matches!(self, Self::Connection(_) | Self::Timeout)
  }
}