#![expect(
dead_code,
reason = "解析节点将在同一批次后续 StandardExpressionParser 主链中消费"
)]
use std::sync::Arc;
use crate::util::Utf16String;
use super::{IStandardExpression, StandardExpressionResult};
pub(crate) struct ExpressionParsingNode {
input: Option<Utf16String>,
expression: Option<Arc<dyn IStandardExpression>>,
}
impl ExpressionParsingNode {
pub(crate) fn from_input(input: Utf16String) -> Self {
Self {
input: Some(trim(&input)),
expression: None,
}
}
pub(crate) fn from_expression(expression: Arc<dyn IStandardExpression>) -> Self {
Self {
input: None,
expression: Some(expression),
}
}
pub(crate) fn is_input(&self) -> bool {
self.input.is_some()
}
pub(crate) fn is_expression(&self) -> bool {
self.expression.is_some()
}
pub(crate) fn is_simple_expression(&self) -> bool {
self.expression
.as_ref()
.is_some_and(|expression| !expression.is_complex())
}
pub(crate) fn complex_expression(&self) -> bool {
self.expression
.as_ref()
.is_some_and(|expression| expression.is_complex())
}
pub(crate) fn get_input(&self) -> Option<&Utf16String> {
self.input.as_ref()
}
pub(crate) fn get_expression(&self) -> Option<&Arc<dyn IStandardExpression>> {
self.expression.as_ref()
}
pub(crate) fn to_utf16_string(&self) -> StandardExpressionResult<Utf16String> {
if let Some(expression) = &self.expression {
let mut units = vec![b'[' as u16];
units.extend_from_slice(expression.get_string_representation()?.as_utf16());
units.push(b']' as u16);
return Ok(Utf16String::from_utf16(units));
}
Ok(self
.input
.clone()
.unwrap_or_else(|| Utf16String::from_rust_str("null")))
}
}
fn trim(input: &Utf16String) -> Utf16String {
let units = input.as_utf16();
let mut start = 0;
while start < units.len() && units[start] <= 0x20 {
start += 1;
}
let mut end = units.len();
while end > start && units[end - 1] <= 0x20 {
end -= 1;
}
Utf16String::from_utf16(units[start..end].to_vec())
}