use std::sync::Arc;
use crate::IEngineConfiguration;
use crate::exceptions::TemplateProcessingException;
use crate::util::Utf16String;
use super::{
IStandardConversionService, IStandardExpressionParser, IStandardVariableExpressionEvaluator,
StandardExpressionResult,
};
pub struct StandardExpressions;
impl StandardExpressions {
pub const STANDARD_VARIABLE_EXPRESSION_EVALUATOR_ATTRIBUTE_NAME: &'static str =
"StandardVariableExpressionEvaluator";
pub const STANDARD_EXPRESSION_PARSER_ATTRIBUTE_NAME: &'static str = "StandardExpressionParser";
pub const STANDARD_CONVERSION_SERVICE_ATTRIBUTE_NAME: &'static str =
"StandardConversionService";
pub fn get_expression_parser(
configuration: &dyn IEngineConfiguration,
) -> StandardExpressionResult<Arc<dyn IStandardExpressionParser>> {
get_registered_attribute::<Arc<dyn IStandardExpressionParser>>(
configuration,
Self::STANDARD_EXPRESSION_PARSER_ATTRIBUTE_NAME,
"No Standard Expression Parser has been registered as an execution argument. \
This is a requirement for using Standard Expressions, and might happen if neither \
the Standard or the SpringStandard dialects have been added to the Template Engine \
and none of the specified dialects registers an attribute of type \
org.thymeleaf.standard.expression.IStandardExpressionParser with name \
\"StandardExpressionParser\"",
)
}
pub fn get_variable_expression_evaluator(
configuration: &dyn IEngineConfiguration,
) -> StandardExpressionResult<Arc<dyn IStandardVariableExpressionEvaluator>> {
get_registered_attribute::<Arc<dyn IStandardVariableExpressionEvaluator>>(
configuration,
Self::STANDARD_VARIABLE_EXPRESSION_EVALUATOR_ATTRIBUTE_NAME,
"No Standard Variable Expression Evaluator has been registered as an execution \
argument. This is a requirement for using Standard Expressions, and might happen \
if neither the Standard or the SpringStandard dialects have been added to the \
Template Engine and none of the specified dialects registers an attribute of type \
org.thymeleaf.standard.expression.IStandardVariableExpressionEvaluator with name \
\"StandardVariableExpressionEvaluator\"",
)
}
pub fn get_conversion_service(
configuration: &dyn IEngineConfiguration,
) -> StandardExpressionResult<Arc<dyn IStandardConversionService>> {
get_registered_attribute::<Arc<dyn IStandardConversionService>>(
configuration,
Self::STANDARD_CONVERSION_SERVICE_ATTRIBUTE_NAME,
"No Standard Conversion Service has been registered as an execution argument. This \
is a requirement for using Standard Expressions, and might happen if neither the \
Standard or the SpringStandard dialects have been added to the Template Engine and \
none of the specified dialects registers an attribute of type \
org.thymeleaf.standard.expression.IStandardConversionService with name \
\"StandardConversionService\"",
)
}
}
fn get_registered_attribute<T>(
configuration: &dyn IEngineConfiguration,
name: &str,
error_message: &str,
) -> StandardExpressionResult<T>
where
T: Clone + Send + Sync + 'static,
{
let name = Utf16String::from_rust_str(name);
configuration
.get_execution_attributes()
.get(&Some(name))
.and_then(Option::as_deref)
.and_then(|attribute| attribute.downcast_ref::<T>())
.cloned()
.ok_or_else(|| {
Box::new(TemplateProcessingException::new(Some(
error_message.to_owned(),
))) as crate::expression::StandardExpressionError
})
}