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
use chrono::Timelike;
use chrono_tz::Tz;
use thiserror::Error;

use crate::util::{Locale, Utf16String};

use super::temporal_creation_utils::process_pattern;
use super::temporal_objects::java_short_zone;
use super::{TemporalObjects, TemporalValue};

/// Java 8 Time 对象格式化及字段读取工具。
///
/// 对应 Java: `org.thymeleaf.util.temporal.TemporalFormattingUtils`。
pub struct TemporalFormattingUtils {
    locale: Locale,
    default_zone_id: Tz,
}

impl TemporalFormattingUtils {
    /// 使用 Locale 与默认 ZoneId 创建格式化工具。
    /// 对应 Java 语义:`TemporalFormattingUtils` 的 `new` 行为(Rust 侧辅助/私有路径)。
    pub fn new(locale: Locale, default_zone_id: Tz) -> Result<Self, TemporalFormattingError> {
        Ok(Self {
            locale,
            default_zone_id,
        })
    }

    /// 使用默认格式或指定 Java DateTimeFormatter pattern 格式化 temporal。
    /// 对应 Java: `TemporalFormattingUtils#format()`。
    pub fn format(
        &self,
        target: Option<&TemporalValue>,
        pattern: Option<&str>,
        locale: Option<&Locale>,
        zone_id: Option<Tz>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        let Some(target) = target else {
            return Ok(None);
        };
        if pattern.is_some_and(|pattern| pattern.trim().is_empty()) {
            return Err(invalid("Pattern cannot be null or empty"));
        }
        let locale = locale.unwrap_or(&self.locale);
        let has_explicit_pattern = pattern.is_some();
        let pattern = match pattern {
            None => TemporalObjects::formatter_for(target, locale)?,
            Some("SHORT") => localized_pattern(target, locale, "SHORT", &self.default_zone_id),
            Some("MEDIUM") => localized_pattern(target, locale, "MEDIUM", &self.default_zone_id),
            Some("LONG") => localized_pattern(target, locale, "LONG", &self.default_zone_id),
            Some("FULL") => localized_pattern(target, locale, "FULL", &self.default_zone_id),
            Some(pattern) => process_pattern(pattern),
        };

        let formatted = match target {
            TemporalValue::Instant(value) if !has_explicit_pattern && pattern.contains('Z') => {
                value.format(&pattern).to_string()
            }
            TemporalValue::LocalDate(value) if !has_explicit_pattern && zone_id.is_none() => {
                value.format(&pattern).to_string()
            }
            TemporalValue::LocalDateTime(value) if !has_explicit_pattern && zone_id.is_none() => {
                value.format(&pattern).to_string()
            }
            TemporalValue::LocalTime(value) if !has_explicit_pattern && zone_id.is_none() => {
                value.format(&pattern).to_string()
            }
            TemporalValue::OffsetDateTime(value) if !has_explicit_pattern && zone_id.is_none() => {
                // Java formatterFor(OffsetDateTime) = appendLocalized(LONG, MEDIUM)
                // + appendLocalizedOffset(FULL):偏移段为 "GMT"(零偏移)或
                // "GMT+HH:MM",由 format() 按目标自身偏移追加。
                format!(
                    "{}{}",
                    value.format(&pattern),
                    gmt_offset(value.offset().local_minus_utc())
                )
            }
            TemporalValue::OffsetTime(value, offset)
                if !has_explicit_pattern && zone_id.is_none() =>
            {
                // Java formatterFor(OffsetTime) = `HH:mm:ss` + appendLocalizedOffset(FULL)。
                format!(
                    "{}{}",
                    value.format(&pattern),
                    gmt_offset(offset.local_minus_utc())
                )
            }
            TemporalValue::Year(value) if !has_explicit_pattern && zone_id.is_none() => {
                format_year(*value, &pattern)
            }
            TemporalValue::YearMonth(year, month) if !has_explicit_pattern && zone_id.is_none() => {
                let date = chrono::NaiveDate::from_ymd_opt(*year, *month, 1)
                    .ok_or_else(|| invalid("Invalid YearMonth"))?;
                date.format(&pattern).to_string()
            }
            TemporalValue::ZonedDateTime(value) if !has_explicit_pattern && zone_id.is_none() => {
                value.format(&pattern).to_string()
            }
            // Java: `DateTimeFormatter.ofPattern(pattern).withZone(zoneId)`
            // 对 `zonedTime(target, defaultZoneId)` 按同一瞬时重定区;
            // 显式 zone 时先以默认区建立带区时间,再保持瞬时换算。
            _ => {
                let zoned = TemporalObjects::zoned_time(target, self.default_zone_id)?;
                let converted = match zone_id {
                    Some(zone) => zoned.with_timezone(&zone),
                    None => zoned,
                };
                converted.format(&pattern).to_string()
            }
        };
        let formatted = replace_java_fraction_markers(formatted, target)?;
        // Java CLDR 按 locale 本地化月份/星期名;chrono 只输出英文,德语
        // locale 下替换为 Java `Locale.GERMANY` 的名称。
        let formatted = if locale.get_language().to_string_lossy() == "de" {
            localize_german_names(formatted)
        } else {
            formatted
        };
        Ok(Some(Utf16String::from_rust_str(&formatted)))
    }

    /// 返回一个月中的日期。
    /// 对应 Java: `TemporalFormattingUtils#day()`。
    pub fn day(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        target
            .map(|target| TemporalObjects::date_fields(target).map(|(_, _, day, _)| day as i32))
            .transpose()
            .map_err(Into::into)
    }

    /// 返回一月为 1 的月份。
    /// 对应 Java: `TemporalFormattingUtils#month()`。
    pub fn month(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        target
            .map(|target| TemporalObjects::date_fields(target).map(|(_, month, _, _)| month as i32))
            .transpose()
            .map_err(Into::into)
    }

    /// 返回当前 Locale 下的完整月份名称。
    ///
    /// 对应 Java: `TemporalFormattingUtils#monthName(Object)`。
    pub fn month_name(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        self.format(target, Some("MMMM"), None, None)
    }

    /// 返回当前 Locale 下的短月份名称。
    ///
    /// 对应 Java: `TemporalFormattingUtils#monthNameShort(Object)`。
    pub fn month_name_short(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        self.format(target, Some("MMM"), None, None)
    }

    /// 返回年份。
    /// 对应 Java: `TemporalFormattingUtils#year()`。
    pub fn year(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        target
            .map(|target| TemporalObjects::date_fields(target).map(|(year, _, _, _)| year))
            .transpose()
            .map_err(Into::into)
    }

    /// 返回 ISO 周一为 1 的星期编号。
    /// 对应 Java: `TemporalFormattingUtils#dayOfWeek()`。
    pub fn day_of_week(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        target
            .map(|target| {
                TemporalObjects::date_fields(target).map(|(_, _, _, weekday)| weekday as i32)
            })
            .transpose()
            .map_err(Into::into)
    }

    /// 返回当前 Locale 下的完整星期名称。
    ///
    /// 对应 Java: `TemporalFormattingUtils#dayOfWeekName(Object)`。
    pub fn day_of_week_name(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        self.format(target, Some("EEEE"), None, None)
    }

    /// 返回当前 Locale 下的短星期名称。
    ///
    /// 对应 Java: `TemporalFormattingUtils#dayOfWeekNameShort(Object)`。
    pub fn day_of_week_name_short(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        self.format(target, Some("EEE"), None, None)
    }

    /// 返回小时。
    /// 对应 Java: `TemporalFormattingUtils#hour()`。
    pub fn hour(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        self.time_field(target, 0)
    }

    /// 返回分钟。
    /// 对应 Java: `TemporalFormattingUtils#minute()`。
    pub fn minute(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        self.time_field(target, 1)
    }

    /// 返回秒。
    /// 对应 Java: `TemporalFormattingUtils#second()`。
    pub fn second(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        self.time_field(target, 2)
    }

    /// 返回纳秒。
    /// 对应 Java: `TemporalFormattingUtils#nanosecond()`。
    pub fn nanosecond(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        self.time_field(target, 3)
    }

    /// 按 Thymeleaf 固定 ISO pattern 格式化并补齐缺失字段。
    ///
    /// 对应 Java `TemporalFormattingUtils#formatISO`:
    /// `DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZ")` —— 偏移使用
    /// `ZZZ` 的 `+HHmm` 无冒号形态;带偏移的类型(OffsetDateTime/OffsetTime/
    /// ZonedDateTime)保留原始偏移,其余在默认 ZoneId 下组装。
    pub fn format_iso(
        &self,
        target: Option<&TemporalValue>,
    ) -> Result<Option<Utf16String>, TemporalFormattingError> {
        let Some(target) = target else {
            return Ok(None);
        };
        let formatted = match target {
            TemporalValue::OffsetDateTime(value) => {
                value.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string()
            }
            // OffsetTime 与其余类型一致走 `zonedTime`:Java 在默认 ZoneId 下组装
            // (`ZonedDateTime.of(LocalDate.now(), localTime, defaultZoneId)`),
            // 偏移本身不参与。
            TemporalValue::ZonedDateTime(value) => {
                value.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string()
            }
            _ => TemporalObjects::zoned_time(target, self.default_zone_id)?
                .format("%Y-%m-%dT%H:%M:%S%.3f%z")
                .to_string(),
        };
        Ok(Some(Utf16String::from_rust_str(&formatted)))
    }

    fn time_field(
        &self,
        target: Option<&TemporalValue>,
        index: usize,
    ) -> Result<Option<i32>, TemporalFormattingError> {
        target
            .map(|target| {
                let fields = TemporalObjects::time_fields(target)?;
                Ok([fields.0, fields.1, fields.2, fields.3][index] as i32)
            })
            .transpose()
    }
}

fn replace_java_fraction_markers(
    mut formatted: String,
    target: &TemporalValue,
) -> Result<String, TemporalFormattingError> {
    let nanosecond = match target {
        TemporalValue::Instant(value) => value.nanosecond(),
        TemporalValue::LocalDate(_) | TemporalValue::Year(_) | TemporalValue::YearMonth(_, _) => 0,
        TemporalValue::LocalDateTime(value) => value.nanosecond(),
        TemporalValue::LocalTime(value) | TemporalValue::OffsetTime(value, _) => value.nanosecond(),
        TemporalValue::OffsetDateTime(value) => value.nanosecond(),
        TemporalValue::ZonedDateTime(value) => value.nanosecond(),
    };
    const PREFIX: &str = "__THYMELEAF_FRACTION_";
    const SUFFIX: &str = "__";
    while let Some(start) = formatted.find(PREFIX) {
        let marker_end = formatted[start + PREFIX.len()..]
            .find(SUFFIX)
            .map(|offset| start + PREFIX.len() + offset)
            .ok_or_else(|| invalid("Invalid Java fraction marker"))?;
        let count = formatted[start + PREFIX.len()..marker_end]
            .parse::<usize>()
            .map_err(|_| invalid("Invalid Java fraction width"))?;
        if !(1..=9).contains(&count) {
            return Err(invalid("Fraction width must be between 1 and 9"));
        }
        let digits = format!("{nanosecond:09}");
        formatted.replace_range(start..marker_end + SUFFIX.len(), &digits[..count]);
    }
    Ok(formatted)
}

/// Temporal 格式化或字段读取错误。
#[derive(Debug, Error)]
#[error("{message}")]
/// 对应 Java 语义:`TemporalFormattingUtils` 的 Rust 侧类型 `TemporalFormattingError`。
pub struct TemporalFormattingError {
    message: String,
}

impl From<super::temporal_objects::TemporalError> for TemporalFormattingError {
    fn from(error: super::temporal_objects::TemporalError) -> Self {
        invalid(error.to_string())
    }
}

fn invalid(message: impl Into<String>) -> TemporalFormattingError {
    TemporalFormattingError {
        message: message.into(),
    }
}

fn localized_pattern(
    target: &TemporalValue,
    locale: &Locale,
    style: &str,
    default_zone: &Tz,
) -> String {
    let date_only = matches!(target, TemporalValue::LocalDate(_));
    let time_only = matches!(
        target,
        TemporalValue::LocalTime(_) | TemporalValue::OffsetTime(_, _)
    );
    let language = locale.get_language().to_string_lossy();
    let zh = language == "zh";
    // Java CLDR(DateTimeFormatter.ofLocalizedDate/ofLocalizedDateTime):
    // 德语日期样式为 `dd.MM.yy` / `dd.MM.y` / `d. MMMM y` /
    // `EEEE, d. MMMM y`(chrono 等价 %d.%m.%y 等)。
    let de = language == "de";
    // Java `z`(LONG/FULL 的通用时区名):ZoneOffset.UTC → "Z";固定偏移 →
    // "+18:00";命名时区 → 缩写(EST/PST 等)。
    let zone = java_short_zone(target, default_zone);
    match (date_only, time_only, zh, de, style) {
        // ---- 日期(Java DateTimeFormatter.ofLocalizedDate)----
        (true, _, true, _, "FULL") => "%Y年%m月%d日 %A".to_owned(),
        (true, _, true, _, _) => "%Y年%m月%d日".to_owned(),
        (true, _, false, true, "SHORT") => "%d.%m.%y".to_owned(),
        (true, _, false, true, "MEDIUM") => "%d.%m.%Y".to_owned(),
        (true, _, false, true, "FULL") => "%A, %-d. %B %Y".to_owned(),
        (true, _, false, true, _) => "%-d. %B %Y".to_owned(),
        (true, _, false, false, "SHORT") => "%-m/%-d/%y".to_owned(),
        (true, _, false, false, "MEDIUM") => "%b %-d, %Y".to_owned(),
        (true, _, false, false, "FULL") => "%A, %B %-d, %Y".to_owned(),
        (true, _, false, false, _) => "%B %-d, %Y".to_owned(),
        // ---- 时间(Java DateTimeFormatter.ofLocalizedTime)----
        // zh/de 为 24 小时制;zh 的 LONG/FULL 时区名在时间之前(`z HH:mm:ss`)。
        (_, true, true, _, "SHORT") | (_, true, true, _, "MEDIUM") => "%H:%M:%S".to_owned(),
        (_, true, true, _, _) => format!("{zone} %H:%M:%S"),
        (_, true, false, true, "SHORT") => "%H:%M".to_owned(),
        (_, true, false, true, "MEDIUM") => "%H:%M:%S".to_owned(),
        (_, true, false, true, _) => format!("%H:%M:%S {zone}"),
        (_, true, false, false, "SHORT") => "%-I:%M %p".to_owned(),
        (_, true, false, false, "MEDIUM") => "%-I:%M:%S %p".to_owned(),
        (_, true, false, false, _) => format!("%-I:%M:%S %p {zone}"),
        // ---- 日期时间(Java DateTimeFormatter.ofLocalizedDateTime)----
        // zh SHORT 为斜杠分隔,LONG/FULL 时区名位于日期与时间之间。
        (_, _, true, _, "SHORT") => "%Y/%m/%d %H:%M".to_owned(),
        (_, _, true, _, "MEDIUM") => "%Y年%m月%d日 %H:%M:%S".to_owned(),
        (_, _, true, _, _) => format!("%Y年%m月%d日 {zone} %H:%M:%S"),
        (_, _, false, true, "SHORT") => "%d.%m.%y, %H:%M".to_owned(),
        (_, _, false, true, "MEDIUM") => "%d.%m.%Y, %H:%M:%S".to_owned(),
        (_, _, false, true, "FULL") => format!("%A, %-d. %B %Y, %H:%M:%S {zone}"),
        (_, _, false, true, _) => format!("%-d. %B %Y, %H:%M:%S {zone}"),
        (_, _, false, false, "SHORT") => "%-m/%-d/%y, %-I:%M %p".to_owned(),
        (_, _, false, false, "MEDIUM") => "%b %-d, %Y, %-I:%M:%S %p".to_owned(),
        // Java en_US CLDR(JDK 9+)LONG/FULL datetime:`MMMM d, y, h:mm:ss a z`。
        (_, _, false, false, "LONG") => format!("%B %-d, %Y, %-I:%M:%S %p {zone}"),
        (_, _, false, false, "FULL") => format!("%A, %B %-d, %Y, %-I:%M:%S %p {zone}"),
        _ => "%B %-d, %Y, %-I:%M:%S %p".to_owned(),
    }
}

/// 把英文月份/星期名替换为德语名(Java CLDR `Locale.GERMANY`)。
fn localize_german_names(formatted: String) -> String {
    const MONTHS: [(&str, &str); 12] = [
        ("January", "Januar"),
        ("February", "Februar"),
        ("March", "März"),
        ("April", "April"),
        ("May", "Mai"),
        ("June", "Juni"),
        ("July", "Juli"),
        ("August", "August"),
        ("September", "September"),
        ("October", "Oktober"),
        ("November", "November"),
        ("December", "Dezember"),
    ];
    const DAYS: [(&str, &str); 7] = [
        ("Monday", "Montag"),
        ("Tuesday", "Dienstag"),
        ("Wednesday", "Mittwoch"),
        ("Thursday", "Donnerstag"),
        ("Friday", "Freitag"),
        ("Saturday", "Samstag"),
        ("Sunday", "Sonntag"),
    ];
    let mut out = formatted;
    for (english, german) in MONTHS {
        out = out.replace(english, german);
    }
    for (english, german) in DAYS {
        out = out.replace(english, german);
    }
    out
}

fn format_year(year: i32, pattern: &str) -> String {
    pattern
        .replace("%Y", &format!("{year:04}"))
        .replace("%y", &format!("{:02}", year.rem_euclid(100)))
}

/// Java `appendLocalizedOffset(TextStyle.FULL)`:零偏移 → "GMT",否则 "GMT+HH:MM"。
fn gmt_offset(seconds: i32) -> String {
    if seconds == 0 {
        "GMT".to_owned()
    } else {
        let sign = if seconds < 0 { '-' } else { '+' };
        let seconds = seconds.unsigned_abs();
        format!("GMT{sign}{:02}:{:02}", seconds / 3600, seconds % 3600 / 60)
    }
}