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
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io;
use std::sync::{Arc, Mutex};

use super::{
    CharSequenceValue, IWritableCharSequence, TemplateWriter, TextUtilsError, Utf16String,
};

/// 可被线程安全聚合字符序列持有的组件。
pub type AggregateComponent = Arc<dyn CharSequenceValue + Send + Sync>;

/// 聚合字符序列构造错误。
/// 对应 Java 语义:`AggregateCharSequence` 的 Rust 侧类型 `AggregateCharSequenceError`。
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AggregateCharSequenceError {
    /// 单组件重载收到 null。
    NullComponent,
    /// 多个独立参数中至少一个为 null。
    NullComponentArgument,
    /// 数组或 List 参数为 null。
    NullComponents,
    /// 数组或 List 中包含 null。
    NullContainedComponent,
    /// 组件访问失败。
    Sequence(TextUtilsError),
}

impl AggregateCharSequenceError {
    /// 返回对应 Java 异常全限定名。
    #[must_use]
    pub fn class_name(&self) -> &str {
        match self {
            Self::NullComponent
            | Self::NullComponentArgument
            | Self::NullComponents
            | Self::NullContainedComponent => "java.lang.IllegalArgumentException",
            Self::Sequence(error) => error.class_name(),
        }
    }
}

impl Display for AggregateCharSequenceError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NullComponent => {
                formatter.write_str("Component argument is null, which is forbidden")
            }
            Self::NullComponentArgument => {
                formatter.write_str("At least one component argument is null, which is forbidden")
            }
            Self::NullComponents => formatter.write_str("Components argument array cannot be null"),
            Self::NullContainedComponent => formatter
                .write_str("Components argument contains at least a null, which is forbidden"),
            Self::Sequence(error) => Display::fmt(error, formatter),
        }
    }
}

impl Error for AggregateCharSequenceError {}

impl From<TextUtilsError> for AggregateCharSequenceError {
    fn from(value: TextUtilsError) -> Self {
        Self::Sequence(value)
    }
}

/// 不复制组件即可提供统一 UTF-16 视图的聚合字符序列。
///
/// 对应 Java: `org.thymeleaf.util.AggregateCharSequence`。
///
/// 组件在构造后不得修改。偏移和总长度按构造时的 Java `length()` 调用结果缓存。
pub struct AggregateCharSequence {
    values: Vec<AggregateComponent>,
    offsets: Vec<i32>,
    length: i32,
    hash: Mutex<i32>,
}

impl AggregateCharSequence {
    /// 创建单组件聚合。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_one` 行为(Rust 侧辅助/私有路径)。
    pub fn from_one(
        component: Option<AggregateComponent>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let component = component.ok_or(AggregateCharSequenceError::NullComponent)?;
        let length = component.length()?;
        Ok(Self::new_parts(vec![component], vec![0], length))
    }

    /// 创建双组件聚合。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_two` 行为(Rust 侧辅助/私有路径)。
    pub fn from_two(
        component0: Option<AggregateComponent>,
        component1: Option<AggregateComponent>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let values = require_arguments([component0, component1])?;
        let length0 = values[0].length()?;
        let length = length0.wrapping_add(values[1].length()?);
        Ok(Self::new_parts(values, vec![0, length0], length))
    }

    /// 创建三组件聚合。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_three` 行为(Rust 侧辅助/私有路径)。
    pub fn from_three(
        component0: Option<AggregateComponent>,
        component1: Option<AggregateComponent>,
        component2: Option<AggregateComponent>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let values = require_arguments([component0, component1, component2])?;
        let offset1 = values[0].length()?;
        let offset2 = values[0].length()?.wrapping_add(values[1].length()?);
        let length = offset2.wrapping_add(values[2].length()?);
        Ok(Self::new_parts(values, vec![0, offset1, offset2], length))
    }

    /// 创建四组件聚合。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_four` 行为(Rust 侧辅助/私有路径)。
    pub fn from_four(
        component0: Option<AggregateComponent>,
        component1: Option<AggregateComponent>,
        component2: Option<AggregateComponent>,
        component3: Option<AggregateComponent>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let values = require_arguments([component0, component1, component2, component3])?;
        let offset1 = values[0].length()?;
        let offset2 = values[0].length()?.wrapping_add(values[1].length()?);
        let offset3 = values[0]
            .length()?
            .wrapping_add(values[1].length()?)
            .wrapping_add(values[2].length()?);
        let length = offset3.wrapping_add(values[3].length()?);
        Ok(Self::new_parts(
            values,
            vec![0, offset1, offset2, offset3],
            length,
        ))
    }

    /// 创建五组件聚合。
    ///
    /// 保留上游 3.1.5 的可观察缺陷:总长度使用第四组件长度而非第五组件。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_five` 行为(Rust 侧辅助/私有路径)。
    pub fn from_five(
        component0: Option<AggregateComponent>,
        component1: Option<AggregateComponent>,
        component2: Option<AggregateComponent>,
        component3: Option<AggregateComponent>,
        component4: Option<AggregateComponent>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let values =
            require_arguments([component0, component1, component2, component3, component4])?;
        let offset1 = values[0].length()?;
        let offset2 = values[0].length()?.wrapping_add(values[1].length()?);
        let offset3 = values[0]
            .length()?
            .wrapping_add(values[1].length()?)
            .wrapping_add(values[2].length()?);
        let offset4 = values[0]
            .length()?
            .wrapping_add(values[1].length()?)
            .wrapping_add(values[2].length()?)
            .wrapping_add(values[3].length()?);
        let length = offset4.wrapping_add(values[3].length()?);
        Ok(Self::new_parts(
            values,
            vec![0, offset1, offset2, offset3, offset4],
            length,
        ))
    }

    /// 从 Java 数组或 List 语义的组件集合创建聚合。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `from_components` 行为(Rust 侧辅助/私有路径)。
    pub fn from_components(
        components: Option<Vec<Option<AggregateComponent>>>,
    ) -> Result<Self, AggregateCharSequenceError> {
        let components = components.ok_or(AggregateCharSequenceError::NullComponents)?;
        if components.is_empty() {
            let empty: AggregateComponent = Arc::new(Utf16String::from_utf16(Vec::new()));
            return Ok(Self::new_parts(vec![empty], vec![0], 0));
        }
        let mut values: Vec<AggregateComponent> = Vec::with_capacity(components.len());
        let mut offsets: Vec<i32> = Vec::with_capacity(components.len());
        let mut total_length = 0_i32;
        for (index, component) in components.into_iter().enumerate() {
            let component = component.ok_or(AggregateCharSequenceError::NullContainedComponent)?;
            let component_length = component.length()?;
            let offset = if index == 0 {
                0
            } else {
                offsets[index - 1].wrapping_add(values[index - 1].length()?)
            };
            values.push(component);
            offsets.push(offset);
            total_length = total_length.wrapping_add(component_length);
        }
        Ok(Self::new_parts(values, offsets, total_length))
    }

    fn new_parts(values: Vec<AggregateComponent>, offsets: Vec<i32>, length: i32) -> Self {
        Self {
            values,
            offsets,
            length,
            hash: Mutex::new(0),
        }
    }

    /// 返回构造时缓存的 Java `int` 总长度。
    #[must_use]
    pub const fn length(&self) -> i32 {
        self.length
    }

    /// 返回指定聚合 UTF-16 位置的代码单元。
    /// 对应 Java: `AggregateCharSequence#charAt()`。
    pub fn char_at(&self, index: i32) -> Result<u16, TextUtilsError> {
        if index < 0 || index >= self.length {
            return Err(range_error(index, self.length));
        }
        for component_index in (0..self.values.len()).rev() {
            if self.offsets[component_index] <= index {
                return self.values[component_index].char_at(index - self.offsets[component_index]);
            }
        }
        Err(TextUtilsError::SequenceAccess {
            class_name: "java.lang.IllegalStateException".into(),
            message: Some(Utf16String::from_rust_str(
                "Bad computing of charAt at AggregatedString",
            )),
        })
    }

    /// 返回指定范围的 Java String 子序列。
    /// 对应 Java: `AggregateCharSequence#subSequence()`。
    pub fn sub_sequence(
        &self,
        begin_index: i32,
        end_index: i32,
    ) -> Result<Utf16String, TextUtilsError> {
        if begin_index < 0 {
            return Err(range_error(begin_index, self.length));
        }
        if end_index > self.length {
            return Err(range_error(end_index, self.length));
        }
        let sub_length = end_index.wrapping_sub(begin_index);
        if sub_length < 0 {
            return Err(range_error(sub_length, self.length));
        }
        let mut result = Vec::with_capacity(sub_length as usize);
        for index in begin_index..end_index {
            result.push(self.char_at(index)?);
        }
        Ok(Utf16String::from_utf16(result))
    }

    /// 按聚合内容比较另一个同类对象。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `equals_java` 行为(Rust 侧辅助/私有路径)。
    pub fn equals_java(&self, other: &Self) -> Result<bool, TextUtilsError> {
        if std::ptr::eq(self, other) {
            return Ok(true);
        }
        if self.length != other.length {
            return Ok(false);
        }
        if self.length == 0 {
            return Ok(true);
        }
        let own_hash = *lock(&self.hash);
        let other_hash = *lock(&other.hash);
        if own_hash != 0 && other_hash != 0 && own_hash != other_hash {
            return Ok(false);
        }
        for index in 0..self.length {
            if self.char_at(index)? != other.char_at(index)? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    /// 返回并缓存 Java hashCode。
    /// 对应 Java: `AggregateCharSequence#hashCode()`。
    pub fn hash_code(&self) -> Result<i32, TextUtilsError> {
        let cached = *lock(&self.hash);
        if cached != 0 || self.length <= 0 {
            return Ok(cached);
        }
        let mut hash = if self.values.len() == 1 {
            self.values[0].sequence_hash_code()?
        } else {
            0
        };
        if self.values.len() != 1 {
            for component in &self.values {
                for index in 0..component.length()? {
                    hash = hash
                        .wrapping_mul(31)
                        .wrapping_add(i32::from(component.char_at(index)?));
                }
            }
        }
        *lock(&self.hash) = hash;
        Ok(hash)
    }

    /// 按字符内容比较任意 Java CharSequence。
    /// 对应 Java: `AggregateCharSequence#contentEquals()`。
    pub fn content_equals(&self, other: &dyn CharSequenceValue) -> Result<bool, TextUtilsError> {
        if self.length != other.length()? {
            return Ok(false);
        }
        if self.length == 0 || other.sequence_equals(self)? {
            return Ok(true);
        }
        for index in 0..self.length {
            if self.char_at(index)? != other.char_at(index)? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    /// 将所有组件的 `toString()` 结果串联。
    /// 对应 Java 语义:`AggregateCharSequence` 的 `to_utf16_string` 行为(Rust 侧辅助/私有路径)。
    pub fn to_utf16_string(&self) -> Result<Utf16String, TextUtilsError> {
        if self.length == 0 {
            return Ok(Utf16String::from_utf16(Vec::new()));
        }
        if self.values.len() == 1 {
            return self.values[0].to_utf16_string();
        }
        let result_length =
            usize::try_from(self.length).map_err(|_| TextUtilsError::SequenceAccess {
                class_name: "java.lang.NegativeArraySizeException".into(),
                message: Some(Utf16String::from_rust_str(&self.length.to_string())),
            })?;
        let mut result = vec![0_u16; result_length];
        for (component_index, component) in self.values.iter().enumerate() {
            let component_length = component.length()?;
            for source_index in 0..component_length {
                let destination_index = self.offsets[component_index].wrapping_add(source_index);
                let destination_index = usize::try_from(destination_index).map_err(|_| {
                    TextUtilsError::ArrayIndexOutOfBounds {
                        index: destination_index,
                        length: result.len(),
                    }
                })?;
                let Some(slot) = result.get_mut(destination_index) else {
                    return Err(TextUtilsError::ArrayIndexOutOfBounds {
                        index: destination_index as i32,
                        length: result.len(),
                    });
                };
                *slot = component.char_at(source_index)?;
            }
        }
        Ok(Utf16String::from_utf16(result))
    }
}

impl CharSequenceValue for AggregateCharSequence {
    fn sequence_class_name(&self) -> &str {
        "org.thymeleaf.util.AggregateCharSequence"
    }

    fn length(&self) -> Result<i32, TextUtilsError> {
        Ok(self.length)
    }

    fn char_at(&self, index: i32) -> Result<u16, TextUtilsError> {
        self.char_at(index)
    }

    fn as_utf16_string(&self) -> Option<&Utf16String> {
        None
    }

    fn to_utf16_string(&self) -> Result<Utf16String, TextUtilsError> {
        self.to_utf16_string()
    }

    fn sub_sequence(&self, start: i32, end: i32) -> Result<Utf16String, TextUtilsError> {
        self.sub_sequence(start, end)
    }

    fn write_direct(&self, writer: &mut dyn TemplateWriter) -> Option<io::Result<()>> {
        Some(IWritableCharSequence::write(self, writer))
    }

    fn sequence_hash_code(&self) -> Result<i32, TextUtilsError> {
        self.hash_code()
    }

    fn sequence_equals(&self, other: &dyn CharSequenceValue) -> Result<bool, TextUtilsError> {
        if other.sequence_class_name() != "org.thymeleaf.util.AggregateCharSequence"
            || self.length != other.length()?
        {
            return Ok(false);
        }
        for index in 0..self.length {
            if self.char_at(index)? != other.char_at(index)? {
                return Ok(false);
            }
        }
        Ok(true)
    }
}

impl IWritableCharSequence for AggregateCharSequence {
    fn write(&self, writer: &mut dyn TemplateWriter) -> io::Result<()> {
        for component in &self.values {
            let value = component
                .to_utf16_string()
                .map_err(|error| io::Error::other(error.to_string()))?;
            writer.write_utf16(value.as_utf16())?;
        }
        Ok(())
    }
}

fn require_arguments<const N: usize>(
    components: [Option<AggregateComponent>; N],
) -> Result<Vec<AggregateComponent>, AggregateCharSequenceError> {
    components
        .into_iter()
        .map(|component| component.ok_or(AggregateCharSequenceError::NullComponentArgument))
        .collect()
}

fn range_error(index: i32, length: i32) -> TextUtilsError {
    TextUtilsError::StringIndexOutOfBounds {
        index,
        length: usize::try_from(length).unwrap_or_default(),
    }
}

fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}