easy-rmq-rs 1.0.2

Easy-to-use RabbitMQ library for Rust
Documentation
use crate::{BuiltWorker, Result};
use std::future::Future;
use std::pin::Pin;

pub type HandlerFn = Box<dyn Fn(Vec<u8>) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> + Send + Sync + 'static>;

pub struct SubscriberRegistry {
    workers: Vec<BuiltWorker>,
}

impl SubscriberRegistry {
    pub fn new() -> Self {
        Self {
            workers: Vec::new(),
        }
    }

    pub fn register<F>(mut self, factory: F) -> Self
    where
        F: FnOnce(usize) -> BuiltWorker + Send + 'static,
    {
        let count = self.workers.len();
        let worker = factory(count);
        self.workers.push(worker);
        self
    }

    pub async fn run(self) -> Result<()> {
        let mut handles = Vec::new();

        for (idx, worker) in self.workers.into_iter().enumerate() {
            let handle = tokio::spawn(async move {
                if let Err(e) = worker.run().await {
                    tracing::error!("Worker {} error: {:?}", idx, e);
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle
                .await
                .map_err(|e| crate::error::AmqpError::ChannelError(e.to_string()))?;
        }

        Ok(())
    }
}

impl Default for SubscriberRegistry {
    fn default() -> Self {
        Self::new()
    }
}