use async_trait::async_trait;
use std::sync::Arc;
use crate::generation::{GenerationError, Language};
use crate::infrastructure::{
Template, TemplateDiscovery as InfrastructureTemplateDiscovery, TemplateError,
};
use crate::protocols::{Protocol, Role};
#[async_trait]
pub trait TemplateDiscovery: Send + Sync {
async fn discover(
&self,
protocol: Protocol,
role: Role,
language: Language,
) -> Result<Template, GenerationError>;
}
pub struct TemplateDiscoveryAdapter<T: InfrastructureTemplateDiscovery> {
inner: Arc<T>,
}
impl<T: InfrastructureTemplateDiscovery> TemplateDiscoveryAdapter<T> {
pub fn new(inner: Arc<T>) -> Self {
Self { inner }
}
}
#[async_trait]
impl<T: InfrastructureTemplateDiscovery> TemplateDiscovery for TemplateDiscoveryAdapter<T> {
async fn discover(
&self,
protocol: Protocol,
role: Role,
language: Language,
) -> Result<Template, GenerationError> {
self.inner
.discover(protocol, role, language)
.await
.map_err(|e| match e {
TemplateError::TemplateNotFound(path) => {
GenerationError::DiscoveryError(format!("Template not found: {path}"))
}
TemplateError::InvalidManifest(msg) => {
GenerationError::LoadError(format!("Invalid manifest: {msg}"))
}
TemplateError::IoError(e) => {
GenerationError::LoadError(format!("Template IO error: {e}"))
}
TemplateError::YamlError(e) => {
GenerationError::LoadError(format!("YAML error: {e}"))
}
})
}
}