//! Named registry for custom Rust transforms referenced from YAML mappings.

use std::{collections::HashMap, sync::Arc};

use crate::{
    error::{BifrostError, Result},
    transform::Transform,
};

/// Maps `custom: <name>` mapping entries to Rust implementations.
#[derive(Default, Clone)]
pub struct TransformRegistry {
    transforms: HashMap<String, Arc<dyn Transform>>,
}

impl TransformRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn register(&mut self, name: impl Into<String>, transform: Arc<dyn Transform>) {
        self.transforms.insert(name.into(), transform);
    }

    pub fn get(&self, name: &str) -> Result<Arc<dyn Transform>> {
        self.transforms
            .get(name)
            .cloned()
            .ok_or_else(|| BifrostError::TransformNotRegistered(name.to_string()))
    }
}
