diidi-travel-common-queue 0.1.16

A collection of common utilities and types for the DiiDi project.
Documentation
use std::collections::HashSet;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum QueueFeature {
  Delay,
  Headers,
  Keyed,
  Partition,
  BatchPublish,
  BatchConsume,
  DeadLetter,
  Transactions,
}

impl QueueFeature {
  pub fn as_str(&self) -> &'static str {
    match self {
      QueueFeature::Delay => "delay",
      QueueFeature::Headers => "headers",
      QueueFeature::Keyed => "keyed",
      QueueFeature::Partition => "partition",
      QueueFeature::BatchPublish => "batch_publish",
      QueueFeature::BatchConsume => "batch_consume",
      QueueFeature::DeadLetter => "dead_letter",
      QueueFeature::Transactions => "transactions",
    }
  }
}

#[derive(Debug, Clone, Default)]
pub struct QueueFeatures {
  supported: HashSet<QueueFeature>,
}

impl QueueFeatures {
  pub fn new(features: impl IntoIterator<Item = QueueFeature>) -> Self {
    Self { supported: features.into_iter().collect() }
  }

  pub fn with(mut self, feature: QueueFeature) -> Self {
    self.supported.insert(feature);
    self
  }

  pub fn supports(&self, feature: QueueFeature) -> bool {
    self.supported.contains(&feature)
  }

  pub fn is_empty(&self) -> bool {
    self.supported.is_empty()
  }
}