backbone-bucket 0.4.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Bucket Integration Layer
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! This module provides the Anti-Corruption Layer (ACL) for integrating
//! with other modules. All external module dependencies should go through
//! adapters defined here.
//!
//! ## Why Anti-Corruption Layer?
//!
//! - Isolates internal domain from external changes
//! - Translates external concepts to internal language
//! - Provides single point for external dependencies
//!
//! ## Usage
//!
//! ```text
//! use crate::integration::ExternalModuleAdapter;
//!
//! let adapter = ExternalModuleAdapter::new(external_service);
//! let internal_entity = adapter.translate_to_internal(external_dto)?;
//! ```

// Adapters for external modules
// Add adapter modules here as dependencies are declared

// ============================================================================
// CORE INTEGRATION TYPES
// ============================================================================

use std::sync::Arc;
use anyhow::Result;
use async_trait::async_trait;

/// Error type for integration operations
#[derive(Debug, thiserror::Error)]
pub enum IntegrationError {
    #[error("External service unavailable: {0}")]
    ServiceUnavailable(String),

    #[error("Translation failed: {0}")]
    TranslationFailed(String),

    #[error("External entity not found: {0}")]
    NotFound(String),

    #[error("Integration error: {0}")]
    Other(#[from] anyhow::Error),
}

/// Trait for module adapters
///
/// Implement this trait to adapt external module types to internal domain.
#[async_trait]
pub trait ModuleAdapter: Send + Sync {
    /// External type from the other module
    type External;
    /// Internal type in this module's domain
    type Internal;

    /// Translate external type to internal type
    fn to_internal(&self, external: Self::External) -> Result<Self::Internal, IntegrationError>;

    /// Translate internal type to external type (for publishing)
    fn to_external(&self, internal: Self::Internal) -> Result<Self::External, IntegrationError>;
}

/// Trait for module facades
///
/// Facades provide simplified access to external module functionality.
#[async_trait]
pub trait ModuleFacade: Send + Sync {
    /// Check if the external module is available
    async fn is_available(&self) -> bool;

    /// Get the module name
    fn module_name(&self) -> &'static str;
}

// ============================================================================
// INTEGRATION REGISTRY
// ============================================================================

/// Registry for external module integrations
///
/// Use this to register and retrieve adapters for external modules.
pub struct IntegrationRegistry {
    adapters: std::collections::HashMap<String, Arc<dyn std::any::Any + Send + Sync>>,
}

impl IntegrationRegistry {
    /// Create a new empty registry
    pub fn new() -> Self {
        Self {
            adapters: std::collections::HashMap::new(),
        }
    }

    /// Register an adapter for a module
    pub fn register<T: Send + Sync + 'static>(&mut self, module_name: &str, adapter: Arc<T>) {
        self.adapters.insert(module_name.to_string(), adapter);
    }

    /// Get an adapter for a module
    pub fn get<T: Send + Sync + 'static>(&self, module_name: &str) -> Option<Arc<T>> {
        self.adapters
            .get(module_name)
            .and_then(|a| Arc::clone(a).downcast::<T>().ok())
    }

    /// Check if a module adapter is registered
    pub fn has(&self, module_name: &str) -> bool {
        self.adapters.contains_key(module_name)
    }
}

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

// ============================================================================
// CUSTOM INTEGRATIONS
// ============================================================================
// <<< CUSTOM INTEGRATIONS START >>>

// <<< CUSTOM INTEGRATIONS START >>>
// Add custom module adapters here
// Example:
//
// mod billing_adapter;
// pub use billing_adapter::BillingAdapter;
// <<< CUSTOM INTEGRATIONS END >>>