use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExchangeType {
Direct,
Fanout,
Topic,
Headers,
}
impl std::fmt::Display for ExchangeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExchangeType::Direct => write!(f, "direct"),
ExchangeType::Fanout => write!(f, "fanout"),
ExchangeType::Topic => write!(f, "topic"),
ExchangeType::Headers => write!(f, "headers"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExchangeConfig {
pub name: String,
pub exchange_type: ExchangeType,
pub durable: bool,
pub auto_delete: bool,
}
impl ExchangeConfig {
pub fn new(name: &str, exchange_type: ExchangeType) -> Self {
Self {
name: name.to_string(),
exchange_type,
durable: true,
auto_delete: false,
}
}
pub fn with_durable(mut self, durable: bool) -> Self {
self.durable = durable;
self
}
pub fn with_auto_delete(mut self, auto_delete: bool) -> Self {
self.auto_delete = auto_delete;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingConfig {
pub exchange: String,
pub queue: String,
pub routing_key: String,
}
impl BindingConfig {
pub fn new(exchange: &str, queue: &str, routing_key: &str) -> Self {
Self {
exchange: exchange.to_string(),
queue: queue.to_string(),
routing_key: routing_key.to_string(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueInfo {
pub name: String,
pub message_count: u64,
pub consumer_count: u32,
pub durable: bool,
pub auto_delete: bool,
}
#[async_trait]
pub trait Admin: Send + Sync {
async fn declare_exchange(&mut self, config: &ExchangeConfig) -> Result<()>;
async fn delete_exchange(&mut self, name: &str) -> Result<()>;
async fn list_exchanges(&mut self) -> Result<Vec<String>>;
async fn bind_queue(&mut self, binding: &BindingConfig) -> Result<()>;
async fn unbind_queue(&mut self, binding: &BindingConfig) -> Result<()>;
async fn queue_info(&mut self, queue: &str) -> Result<QueueInfo>;
async fn list_bindings(&mut self, queue: &str) -> Result<Vec<BindingConfig>>;
}