use std::sync::Arc;
use crate::context::IExpressionContext;
use crate::util::Utf16String;
use super::{
IStandardExpression, StandardExpressionExecutionContext, StandardExpressionResult,
TemplateValue, Token,
};
pub struct GenericTokenExpression {
value: Arc<Utf16String>,
}
impl GenericTokenExpression {
fn new(value: Utf16String) -> Self {
Self {
value: Arc::new(value),
}
}
pub fn parse_generic_token_expression(input: Option<&Utf16String>) -> Option<Self> {
let input = input?;
for position in 0..input.len() {
let position = i32::try_from(position).ok()?;
if !Token::<Utf16String>::is_token_char(Some(input), position).ok()? {
return None;
}
}
Some(Self::new(input.clone()))
}
pub fn get_value(&self) -> &Utf16String {
self.value.as_ref()
}
}
impl IStandardExpression for GenericTokenExpression {
fn get_string_representation(&self) -> StandardExpressionResult<Utf16String> {
Ok(self.value.as_ref().clone())
}
fn execute_with_context(
&self,
_context: &dyn IExpressionContext,
_expression_context: &'static StandardExpressionExecutionContext,
) -> StandardExpressionResult<Option<Arc<TemplateValue>>> {
Ok(Some(Arc::new(TemplateValue::String(Arc::clone(
&self.value,
)))))
}
fn is_token_expression(&self) -> bool {
true
}
fn is_generic_token_expression(&self) -> bool {
true
}
}
impl super::SimpleExpression for GenericTokenExpression {}