use crate::cache::TemplateConfigurationFactory;
use crate::cache::TemplateConfigurationFactoryException;
use crate::cache::TemplateSourceMatcher;
use crate::core::TemplateConfiguration;
use std::sync::Arc;
pub struct ConditionalTemplateConfigurationFactory {
matcher: Box<dyn TemplateSourceMatcher>,
template_configuration: Option<Arc<TemplateConfiguration>>,
template_configuration_factory: Option<Box<dyn TemplateConfigurationFactory>>,
}
impl ConditionalTemplateConfigurationFactory {
pub fn with_factory(
matcher: Box<dyn TemplateSourceMatcher>,
template_configuration_factory: Box<dyn TemplateConfigurationFactory>,
) -> Self {
ConditionalTemplateConfigurationFactory {
matcher,
template_configuration: None,
template_configuration_factory: Some(template_configuration_factory),
}
}
pub fn with_configuration(
matcher: Box<dyn TemplateSourceMatcher>,
template_configuration: Arc<TemplateConfiguration>,
) -> Self {
ConditionalTemplateConfigurationFactory {
matcher,
template_configuration: Some(template_configuration),
template_configuration_factory: None,
}
}
}
impl TemplateConfigurationFactory for ConditionalTemplateConfigurationFactory {
fn get(
&self,
source_name: &str,
) -> Result<Option<Arc<TemplateConfiguration>>, TemplateConfigurationFactoryException> {
if self.matcher.matches(source_name) {
if let Some(f) = &self.template_configuration_factory {
f.get(source_name)
} else {
Ok(self.template_configuration.clone())
}
} else {
Ok(None)
}
}
}