shepherd-rs 0.2.0

Shepherd is a resilient, non-blocking orchestrator that persistently transforms and delivers data—built for remote, compute-heavy workloads.
Documentation
//! # Emitter Trait
//!
//! This trait defines the behavior of emitters in the shepherd framework.
//!
//! ## Overview
//! - **Emitter**: Handles emission of transformation requests.
//! - **State Management**: Manages operational and halt states.
//!
//! ## Example
//! ```rust
//! struct MyEmitter;
//!
//! impl Emitter for MyEmitter {
//!     // Implementation details...
//! }
//! ```

use std::error::Error;
use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::Mutex;
use tokio::sync::mpsc::{Receiver, Sender};

use crate::config::Config;
use crate::transform::TransformRequest;

/// An emitter that emits transformation requests to a channel.
/// The emitter is envisioned as a *Sequential* entity, meaining that it would
/// always produce transformation requests in a strict order. It has to be
/// ensured that two emission sequences `[TR1, TR2]` and `[TR2, TR1]` are not
/// both valid. The last seen `TransformRequest` at the database end dictates
/// the current dynamic state / configuration of the emitter.
#[async_trait]
pub trait Emitter: Send + Sync {
    type TransformRequest: TransformRequest;
    type Error: Error + Send;
    type Config: Config;

    async fn new(
        init_config: Arc<Mutex<Self::Config>>,
        send_channel: Sender<Self::TransformRequest>,
        recv_channel: Receiver<EmissionState>,
    ) -> Result<Self, Self::Error>
    where
        Self: Sized;

    /// The running loop of the emitter.
    /// This loop should run indefinitely, processing incoming requests in a
    /// sequential manner. See [`Emitter`] for more details.
    async fn emitter_loop(&mut self) -> Result<(), Self::Error>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EmissionState {
    Halt,
    Operational,
}