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
use std::collections::HashMap;

use async_trait::async_trait;
use serde::Serialize;
use serde::de::DeserializeOwned;
use thiserror::Error;

use crate::config::Config;

#[derive(Debug, Error)]
pub enum InMemoryConfigError {
    #[error("Key not found in configuration")]
    KeyNotFound,
}

pub struct InMemoryConfig<KeyType, ValueType> {
    config: HashMap<KeyType, ValueType>,
}

#[async_trait]
impl<KeyType, ValueType> Config for InMemoryConfig<KeyType, ValueType>
where
    KeyType: std::hash::Hash + Eq + Send + Sync + Clone + 'static,
    ValueType: Send + Sync + Serialize + DeserializeOwned + Clone + 'static,
{
    type Error = InMemoryConfigError;
    type KeyType = KeyType;
    type StaticConfigHandle = ();
    type ValueType = ValueType;

    async fn new(_handle: Self::StaticConfigHandle) -> Result<Self, Self::Error> {
        Ok(InMemoryConfig {
            config: HashMap::new(),
        })
    }

    async fn set(&mut self, key: Self::KeyType, value: Self::ValueType) -> Result<(), Self::Error> {
        self.config.insert(key, value);
        Ok(())
    }

    async fn set_bulk(
        &mut self,
        values: Vec<(Self::KeyType, Self::ValueType)>,
    ) -> Result<(), Self::Error> {
        for (key, value) in values {
            self.config.insert(key, value);
        }
        Ok(())
    }

    async fn get(&self, key: Self::KeyType) -> Result<Self::ValueType, Self::Error> {
        self.config
            .get(&key)
            .cloned()
            .ok_or_else(|| InMemoryConfigError::KeyNotFound)
    }
}