thymeleaf 0.1.0-beta.1

A framework-neutral Thymeleaf-compatible dynamic template engine for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use std::sync::{Arc, RwLock};

use indexmap::IndexMap;

use crate::context::{IExpressionContext, ITemplateContext};
use crate::exceptions::{TemplateInputException, TemplateProcessingException};
use crate::model::IModel;
use crate::util::{Utf16String, ValidateError};

use super::fragment::FragmentParameterMap;
use super::{
    Assignation, AssignationSequence, AssignationUtils, ExpressionSequenceUtils, Fragment,
    IStandardExpression, StandardExpressionExecutionContext, StandardExpressionResult,
    TemplateValue, TextLiteralExpression, expression_parsing_util::ExpressionParsingUtil,
};

/// `~{template :: selector(parameters)}` Fragment 表达式。
///
/// 对应 Java: `org.thymeleaf.standard.expression.FragmentExpression`。
#[derive(Clone)]
pub struct FragmentExpression {
    template_name: Option<Arc<dyn IStandardExpression>>,
    fragment_selector: Option<Arc<dyn IStandardExpression>>,
    parameters: Option<Arc<AssignationSequence>>,
    synthetic_parameters: bool,
    empty: bool,
}

impl FragmentExpression {
    /// Fragment 表达式选择器。
    pub const SELECTOR: u16 = b'~' as u16;
    const UNNAMED_PARAMETERS_PREFIX: &'static str = "_arg";

    /// 创建非空 Fragment 表达式。
    /// 对应 Java 语义:`FragmentExpression` 的 `new` 行为(Rust 侧辅助/私有路径)。
    pub fn new(
        template_name: Option<Arc<dyn IStandardExpression>>,
        fragment_selector: Option<Arc<dyn IStandardExpression>>,
        parameters: Option<Arc<AssignationSequence>>,
        synthetic_parameters: bool,
    ) -> Result<Self, ValidateError> {
        if template_name.is_none() && fragment_selector.is_none() {
            return Err(ValidateError::IllegalArgument {
                message: Some(
                    "Fragment Expression cannot have null template name and null fragment selector"
                        .to_owned(),
                ),
            });
        }
        let synthetic_parameters = parameters
            .as_ref()
            .is_some_and(|values| values.size() > 0 && synthetic_parameters);
        Ok(Self {
            template_name,
            fragment_selector,
            parameters,
            synthetic_parameters,
            empty: false,
        })
    }

    fn empty() -> Self {
        Self {
            template_name: None,
            fragment_selector: None,
            parameters: None,
            synthetic_parameters: false,
            empty: true,
        }
    }

    /// 返回模板名称表达式。
    /// 对应 Java: `FragmentExpression#getTemplateName()`。
    pub fn get_template_name(&self) -> Option<&dyn IStandardExpression> {
        self.template_name.as_deref()
    }

    /// 返回 Fragment selector 表达式。
    /// 对应 Java: `FragmentExpression#getFragmentSelector()`。
    pub fn get_fragment_selector(&self) -> Option<&dyn IStandardExpression> {
        self.fragment_selector.as_deref()
    }

    /// 判断是否具有 Fragment selector。
    /// 对应 Java: `FragmentExpression#hasFragmentSelector()`。
    pub fn has_fragment_selector(&self) -> bool {
        self.fragment_selector.is_some()
    }

    /// 返回参数赋值序列。
    /// 对应 Java: `FragmentExpression#getParameters()`。
    pub fn get_parameters(&self) -> Option<&AssignationSequence> {
        self.parameters.as_deref()
    }

    /// 判断是否具有至少一个参数。
    /// 对应 Java: `FragmentExpression#hasParameters()`。
    pub fn has_parameters(&self) -> bool {
        self.parameters
            .as_ref()
            .is_some_and(|values| values.size() > 0)
    }

    /// 判断参数名是否由引擎按位置合成。
    /// 对应 Java: `FragmentExpression#hasSyntheticParameters()`。
    pub fn has_synthetic_parameters(&self) -> bool {
        self.synthetic_parameters
    }

    /// 解析完整 Fragment 表达式。
    /// 对应 Java: `FragmentExpression#parseFragmentExpression()`。
    pub fn parse_fragment_expression(input: Option<&Utf16String>) -> Option<Self> {
        let input = input?;
        let trimmed = trim(input.as_utf16());
        if trimmed.len() < 3
            || trimmed[0] != b'~' as u16
            || trimmed[1] != b'{' as u16
            || trimmed.last() != Some(&(b'}' as u16))
        {
            return None;
        }
        let content = trim(&trimmed[2..trimmed.len() - 1]);
        if content.is_empty() {
            return Some(Self::empty());
        }
        Self::parse_fragment_expression_content(&Utf16String::from_utf16(content.to_vec()))
    }

    fn parse_fragment_expression_content(input: &Utf16String) -> Option<Self> {
        let trimmed = trim(input.as_utf16());
        if trimmed.is_empty() {
            return Some(Self::empty());
        }
        let parameter_start = index_of_last_parentheses_group(trimmed);
        let (without_parameters, mut parameters) = match parameter_start {
            Some(position) => (
                trim(&trimmed[..position]),
                Some(trim(&trimmed[position + 1..trimmed.len() - 1])),
            ),
            None => (trimmed, None),
        };
        let separator = find_double_colon(without_parameters);
        let (mut template_name, mut fragment_spec) = match separator {
            None => (trim(without_parameters), None),
            Some(position) => (
                trim(&without_parameters[..position]),
                Some(trim(&without_parameters[position + 2..])),
            ),
        };
        if separator.is_none() && template_name.is_empty() {
            template_name = parameters.take()?;
        } else if separator.is_some() && fragment_spec.is_some_and(<[u16]>::is_empty) {
            fragment_spec = Some(parameters.take()?);
        }

        let template_name = if template_name.is_empty() {
            None
        } else {
            Some(parse_template_name_default_as_literal(template_name)?)
        };
        let fragment_selector = match fragment_spec.filter(|value| !value.is_empty()) {
            Some(value) => Some(parse_default_as_literal(value)?),
            None => None,
        };

        let mut synthetic = false;
        let parameter_sequence = match parameters.filter(|value| !trim(value).is_empty()) {
            None => None,
            Some(value) => {
                let value = Utf16String::from_utf16(value.to_vec());
                if let Some(assignations) =
                    AssignationUtils::internal_parse_assignation_sequence(&value, false)
                {
                    Some(Arc::new(assignations))
                } else {
                    let expressions =
                        ExpressionSequenceUtils::internal_parse_expression_sequence(&value)?;
                    synthetic = true;
                    Some(Arc::new(create_synthetic_parameters(&expressions)?))
                }
            }
        };

        Self::new(
            template_name,
            fragment_selector,
            parameter_sequence,
            synthetic,
        )
        .ok()
    }

    /// 在 RESTRICTED 上下文中执行模板名、selector 和参数。
    /// 对应 Java: `FragmentExpression#createExecutedFragmentExpression()`。
    pub fn create_executed_fragment_expression(
        context: &dyn IExpressionContext,
        expression: &FragmentExpression,
    ) -> StandardExpressionResult<ExecutedFragmentExpression> {
        Self::do_create_executed_fragment_expression(
            context,
            expression,
            StandardExpressionExecutionContext::RESTRICTED,
        )
    }

    fn do_create_executed_fragment_expression(
        context: &dyn IExpressionContext,
        expression: &FragmentExpression,
        expression_context: &'static StandardExpressionExecutionContext,
    ) -> StandardExpressionResult<ExecutedFragmentExpression> {
        if expression.empty {
            return Ok(ExecutedFragmentExpression::empty());
        }
        let template_name_expression_result = match &expression.template_name {
            Some(value) => value
                .execute_with_context(context, StandardExpressionExecutionContext::RESTRICTED)?,
            None => None,
        };
        let fragment_parameters = create_executed_parameters(
            context,
            expression.parameters.as_deref(),
            expression_context,
        )?;
        let fragment_selector_expression_result = match &expression.fragment_selector {
            Some(value) => value.execute_with_context(context, expression_context)?,
            None => None,
        };
        Ok(ExecutedFragmentExpression {
            fragment_expression: expression.clone(),
            expression_representation: expression.get_string_representation()?,
            template_name_expression_result,
            fragment_selector_expression_result,
            fragment_parameters,
            synthetic_parameters: expression.synthetic_parameters,
            empty: false,
        })
    }

    /// 将已执行表达式解析为模板模型 Fragment。
    /// 对应 Java: `FragmentExpression#resolveExecutedFragmentExpression()`。
    pub fn resolve_executed_fragment_expression(
        context: &dyn ITemplateContext,
        executed: &ExecutedFragmentExpression,
        fail_if_not_exists: bool,
    ) -> StandardExpressionResult<Option<Arc<Fragment>>> {
        if executed.empty {
            return Ok(Some(Arc::new(Fragment::EMPTY_FRAGMENT)));
        }
        let configuration = context.get_configuration();
        let fragments = Self::resolve_fragments(executed);
        let mut template_name = Self::resolve_template_name(executed);
        let mut template_name_stack = Vec::new();
        if template_name
            .as_ref()
            .is_none_or(|value| trim(value.as_utf16()).is_empty())
        {
            if fragments.as_ref().is_none_or(Vec::is_empty) {
                return Ok(None);
            }
            template_name_stack = context
                .get_template_stack()
                .into_iter()
                .rev()
                .filter_map(|data| data.get_template().cloned())
                .collect();
            template_name = template_name_stack.first().cloned();
        }
        let Some(mut current_template) = template_name else {
            return Ok(None);
        };
        let mut stack_index = 0;
        loop {
            let model = configuration
                .get_template_manager()
                .parse_standalone(
                    context,
                    &current_template,
                    fragments.as_deref(),
                    None,
                    true,
                    fail_if_not_exists,
                )
                .map_err(|error| Box::new(error) as super::StandardExpressionError)?;
            let Some(model) = model else {
                return Ok(None);
            };
            if model.size() > 2 {
                let model: Arc<dyn IModel> = Arc::from(model);
                let fragment = Fragment::new(
                    Some(model),
                    executed.fragment_parameters.clone(),
                    executed.synthetic_parameters,
                )
                .map_err(|error| Box::new(error) as super::StandardExpressionError)?;
                return Ok(Some(Arc::new(fragment)));
            }
            stack_index += 1;
            if stack_index >= template_name_stack.len() {
                if fail_if_not_exists {
                    return Err(Box::new(TemplateInputException::new(Some(format!(
                        "Error resolving fragment: \"{}\": template or fragment could not be resolved",
                        executed.expression_representation.to_string_lossy()
                    )))));
                }
                return Ok(None);
            }
            current_template = template_name_stack[stack_index].clone();
        }
    }

    /// 将模板名结果转换为名称;`this` 和 null 表示当前模板。
    /// 对应 Java: `FragmentExpression#resolveTemplateName()`。
    pub fn resolve_template_name(executed: &ExecutedFragmentExpression) -> Option<Utf16String> {
        let result = executed.template_name_expression_result.as_deref()?;
        let value = result.to_utf16_string()?;
        (value != Utf16String::from_rust_str("this")).then_some(value)
    }

    /// 将 selector 结果规范化为单元素 selector 集合。
    /// 对应 Java: `FragmentExpression#resolveFragments()`。
    pub fn resolve_fragments(executed: &ExecutedFragmentExpression) -> Option<Vec<Utf16String>> {
        let value = executed
            .fragment_selector_expression_result
            .as_deref()?
            .to_utf16_string()?;
        let units = value.as_utf16();
        let normalized = if units.len() > 3
            && units.first() == Some(&(b'[' as u16))
            && units.last() == Some(&(b']' as u16))
            && units[units.len() - 2] != b'\'' as u16
        {
            Utf16String::from_utf16(trim(&units[1..units.len() - 1]).to_vec())
        } else {
            value
        };
        (!trim(normalized.as_utf16()).is_empty()).then(|| vec![normalized])
    }
}

impl IStandardExpression for FragmentExpression {
    fn is_fragment_expression(&self) -> bool {
        true
    }

    fn as_fragment_expression(&self) -> Option<&FragmentExpression> {
        Some(self)
    }

    fn get_string_representation(&self) -> StandardExpressionResult<Utf16String> {
        let mut units = vec![b'~' as u16, b'{' as u16];
        if let Some(template_name) = &self.template_name {
            units.extend_from_slice(template_name.get_string_representation()?.as_utf16());
        }
        if let Some(fragment_selector) = &self.fragment_selector {
            units.extend(" :: ".encode_utf16());
            units.extend_from_slice(fragment_selector.get_string_representation()?.as_utf16());
        }
        if let Some(parameters) = &self.parameters
            && parameters.size() > 0
        {
            units.extend_from_slice(&[b' ' as u16, b'(' as u16]);
            units.extend_from_slice(parameters.get_string_representation()?.as_utf16());
            units.push(b')' as u16);
        }
        units.push(b'}' as u16);
        Ok(Utf16String::from_utf16(units))
    }

    fn execute_with_context(
        &self,
        context: &dyn IExpressionContext,
        _expression_context: &'static StandardExpressionExecutionContext,
    ) -> StandardExpressionResult<Option<Arc<TemplateValue>>> {
        let template_context = context.as_template_context().ok_or_else(|| {
            Box::new(TemplateProcessingException::new(Some(format!(
                "Cannot evaluate expression \"{}\". Fragment expressions can only be evaluated in a template-processing environment",
                self.get_string_representation()
                    .map_or_else(|_| String::new(), |value| value.to_string_lossy())
            )))) as super::StandardExpressionError
        })?;
        if self.empty {
            return Ok(Some(Arc::new(TemplateValue::Object(Arc::new(
                Fragment::EMPTY_FRAGMENT,
            )))));
        }
        let executed = Self::create_executed_fragment_expression(context, self)?;
        Ok(
            Self::resolve_executed_fragment_expression(template_context, &executed, false)?
                .map(|fragment| Arc::new(TemplateValue::Object(fragment))),
        )
    }
}

impl super::SimpleExpression for FragmentExpression {}

/// Fragment 表达式各子表达式执行后的中间值。
///
/// 对应 Java: `FragmentExpression.ExecutedFragmentExpression`。
pub struct ExecutedFragmentExpression {
    fragment_expression: FragmentExpression,
    expression_representation: Utf16String,
    template_name_expression_result: Option<Arc<TemplateValue>>,
    fragment_selector_expression_result: Option<Arc<TemplateValue>>,
    fragment_parameters: Option<Arc<RwLock<FragmentParameterMap>>>,
    synthetic_parameters: bool,
    empty: bool,
}

impl ExecutedFragmentExpression {
    fn empty() -> Self {
        Self {
            fragment_expression: FragmentExpression::empty(),
            expression_representation: Utf16String::from_rust_str("~{}"),
            template_name_expression_result: None,
            fragment_selector_expression_result: None,
            fragment_parameters: None,
            synthetic_parameters: false,
            empty: true,
        }
    }

    /// 返回产生此中间值的原 Fragment 表达式。
    ///
    /// 对应 Java: `ExecutedFragmentExpression#getFragmentExpression()`。
    pub fn get_fragment_expression(&self) -> &FragmentExpression {
        &self.fragment_expression
    }

    /// 返回模板名表达式执行结果。
    /// 对应 Java: `FragmentExpression#getTemplateNameExpressionResult()`。
    pub fn get_template_name_expression_result(&self) -> Option<&TemplateValue> {
        self.template_name_expression_result.as_deref()
    }

    /// 返回模板名表达式执行结果的共享身份。
    /// 对应 Java 语义:`FragmentExpression` 的 `get_template_name_expression_result_arc` 行为(Rust 侧辅助/私有路径)。
    pub fn get_template_name_expression_result_arc(&self) -> Option<Arc<TemplateValue>> {
        self.template_name_expression_result.clone()
    }

    /// 返回 selector 表达式执行结果。
    /// 对应 Java: `FragmentExpression#getFragmentSelectorExpressionResult()`。
    pub fn get_fragment_selector_expression_result(&self) -> Option<&TemplateValue> {
        self.fragment_selector_expression_result.as_deref()
    }

    /// 返回参数 Map 的共享视图。
    /// 对应 Java: `FragmentExpression#getFragmentParameters()`。
    pub fn get_fragment_parameters(&self) -> Option<&Arc<RwLock<FragmentParameterMap>>> {
        self.fragment_parameters.as_ref()
    }

    /// 判断参数名是否为合成位置参数。
    /// 对应 Java: `FragmentExpression#hasSyntheticParameters()`。
    pub fn has_synthetic_parameters(&self) -> bool {
        self.synthetic_parameters
    }
}

fn create_executed_parameters(
    context: &dyn IExpressionContext,
    parameters: Option<&AssignationSequence>,
    expression_context: &'static StandardExpressionExecutionContext,
) -> StandardExpressionResult<Option<Arc<RwLock<FragmentParameterMap>>>> {
    let Some(parameters) = parameters.filter(|values| values.size() > 0) else {
        return Ok(None);
    };
    let mut values = IndexMap::with_capacity(parameters.size() as usize + 2);
    for assignation in parameters.get_assignations().iter().flatten() {
        let parameter_name = assignation
            .get_left()
            .execute_with_context(context, expression_context)?
            .as_deref()
            .and_then(TemplateValue::to_utf16_string);
        let parameter_value = assignation
            .get_right()
            .ok_or_else(|| {
                Box::new(TemplateProcessingException::new(Some(
                    "Fragment parameter value cannot be null".to_owned(),
                ))) as super::StandardExpressionError
            })?
            .execute_with_context(context, expression_context)?;
        values.insert(parameter_name, parameter_value);
    }
    Ok(Some(Arc::new(RwLock::new(values))))
}

fn create_synthetic_parameters(
    expressions: &super::ExpressionSequence,
) -> Option<AssignationSequence> {
    let mut assignations = Vec::with_capacity(expressions.size() as usize + 2);
    for (index, expression) in expressions.get_expressions().iter().flatten().enumerate() {
        let name = Utf16String::from_rust_str(&format!(
            "{}{index}",
            FragmentExpression::UNNAMED_PARAMETERS_PREFIX
        ));
        let wrapped = TextLiteralExpression::wrap_string_into_literal(Some(&name))?;
        let left: Arc<dyn IStandardExpression> = Arc::new(
            TextLiteralExpression::parse_text_literal_expression(&wrapped),
        );
        assignations.push(Some(Arc::new(
            Assignation::new(Some(left), Some(Arc::clone(expression))).ok()?,
        )));
    }
    AssignationSequence::new(Some(Arc::new(RwLock::new(assignations)))).ok()
}

fn parse_default_as_literal(input: &[u16]) -> Option<Arc<dyn IStandardExpression>> {
    let input = Utf16String::from_utf16(trim(input).to_vec());
    if let Ok(expression) = ExpressionParsingUtil::parse_expression(&input) {
        return Some(expression);
    }
    let wrapped = TextLiteralExpression::wrap_string_into_literal(Some(&input))?;
    Some(Arc::new(
        TextLiteralExpression::parse_text_literal_expression(&wrapped),
    ))
}

fn parse_template_name_default_as_literal(input: &[u16]) -> Option<Arc<dyn IStandardExpression>> {
    let input = trim(input);
    let contains_standard_expression = input.windows(2).any(|window| {
        matches!(
            window,
            [selector, open]
                if *open == b'{' as u16
                    && matches!(*selector, value if value == b'$' as u16
                        || value == b'*' as u16
                        || value == b'#' as u16
                        || value == b'@' as u16
                        || value == b'~' as u16)
        )
    });
    if input.contains(&(b'/' as u16)) && !contains_standard_expression {
        let input = Utf16String::from_utf16(input.to_vec());
        let wrapped = TextLiteralExpression::wrap_string_into_literal(Some(&input))?;
        return Some(Arc::new(
            TextLiteralExpression::parse_text_literal_expression(&wrapped),
        ));
    }
    parse_default_as_literal(input)
}

fn index_of_last_parentheses_group(input: &[u16]) -> Option<usize> {
    if input.last() != Some(&(b')' as u16)) {
        return None;
    }
    let mut in_literal = false;
    let mut level = 1_i32;
    for index in (0..input.len() - 1).rev() {
        match input[index] {
            value if value == b'\'' as u16 => in_literal = !in_literal,
            value if value == b')' as u16 && !in_literal => level += 1,
            value if value == b'(' as u16 && !in_literal => {
                level -= 1;
                if level == 0 {
                    return (index != input.len() - 2).then_some(index);
                }
            }
            _ => {}
        }
    }
    None
}

fn find_double_colon(input: &[u16]) -> Option<usize> {
    input
        .windows(2)
        .position(|window| window == [b':' as u16, b':' as u16])
}

fn trim(input: &[u16]) -> &[u16] {
    let start = input
        .iter()
        .position(|unit| *unit > 0x20)
        .unwrap_or(input.len());
    let end = input
        .iter()
        .rposition(|unit| *unit > 0x20)
        .map_or(start, |position| position + 1);
    &input[start..end]
}