| 1 |
use core::num::NonZero; |
| 2 |
|
| 3 |
use rstest::rstest; |
| 4 |
use rstest_reuse::{apply, template}; |
| 5 |
use time::error::InvalidFormatDescription; |
| 6 |
use time::format_description::modifier::*; |
| 7 |
use time::format_description::{ |
| 8 |
self, BorrowedFormatItem, Component, OwnedFormatItem, StaticFormatDescription, |
| 9 |
}; |
| 10 |
use time::macros::format_description; |
| 11 |
|
| 12 |
/// A modifier with its value and string representation. |
| 13 |
/// |
| 14 |
/// This alias is used to avoid repeating the tuple in countless locations. |
| 15 |
type M<T> = (T, &'static str); |
| 16 |
|
| 17 |
#[rustfmt::skip] // does not format well |
| 18 |
#[template] |
| 19 |
#[rstest] |
| 20 |
fn modifiers( |
| 21 |
#[values( |
| 22 |
(Padding::Space, "padding:space"), |
| 23 |
(Padding::Zero, "padding:zero"), |
| 24 |
(Padding::None, "padding:none"), |
| 25 |
)] |
| 26 |
padding: _, |
| 27 |
#[values( |
| 28 |
(false, "repr:24"), |
| 29 |
(true, "repr:12"), |
| 30 |
)] |
| 31 |
hour_is_12_hour_clock: _, |
| 32 |
#[values( |
| 33 |
(true, "case:upper"), |
| 34 |
(false, "case:lower"), |
| 35 |
)] |
| 36 |
period_is_uppercase: _, |
| 37 |
#[values( |
| 38 |
(MonthRepr::Numerical, "repr:numerical"), |
| 39 |
(MonthRepr::Long, "repr:long"), |
| 40 |
(MonthRepr::Short, "repr:short"), |
| 41 |
)] |
| 42 |
month_repr: _, |
| 43 |
#[values( |
| 44 |
(SubsecondDigits::One, "digits:1"), |
| 45 |
(SubsecondDigits::Two, "digits:2"), |
| 46 |
(SubsecondDigits::Three, "digits:3"), |
| 47 |
(SubsecondDigits::Four, "digits:4"), |
| 48 |
(SubsecondDigits::Five, "digits:5"), |
| 49 |
(SubsecondDigits::Six, "digits:6"), |
| 50 |
(SubsecondDigits::Seven, "digits:7"), |
| 51 |
(SubsecondDigits::Eight, "digits:8"), |
| 52 |
(SubsecondDigits::Nine, "digits:9"), |
| 53 |
(SubsecondDigits::OneOrMore, "digits:1+"), |
| 54 |
)] |
| 55 |
subsecond_digits: _, |
| 56 |
#[values( |
| 57 |
(WeekdayRepr::Short, "repr:short"), |
| 58 |
(WeekdayRepr::Long, "repr:long"), |
| 59 |
(WeekdayRepr::Sunday, "repr:sunday"), |
| 60 |
(WeekdayRepr::Monday, "repr:monday"), |
| 61 |
)] |
| 62 |
weekday_repr: _, |
| 63 |
#[values( |
| 64 |
(WeekNumberRepr::Iso, "repr:iso"), |
| 65 |
(WeekNumberRepr::Sunday, "repr:sunday"), |
| 66 |
(WeekNumberRepr::Monday, "repr:monday"), |
| 67 |
)] |
| 68 |
week_number_repr: _, |
| 69 |
#[values( |
| 70 |
(YearRepr::Full, "repr:full"), |
| 71 |
(YearRepr::Century, "repr:century"), |
| 72 |
(YearRepr::LastTwo, "repr:last_two"), |
| 73 |
)] |
| 74 |
year_repr: _, |
| 75 |
#[values( |
| 76 |
(YearRange::Standard, "range:standard"), |
| 77 |
(YearRange::Extended, "range:extended"), |
| 78 |
)] |
| 79 |
year_range: _, |
| 80 |
#[values( |
| 81 |
(false, "base:calendar"), |
| 82 |
(true, "base:iso_week"), |
| 83 |
)] |
| 84 |
year_is_iso_week_based: _, |
| 85 |
#[values( |
| 86 |
(false, "sign:automatic"), |
| 87 |
(true, "sign:mandatory"), |
| 88 |
)] |
| 89 |
sign_is_mandatory: _, |
| 90 |
#[values( |
| 91 |
(true, "one_indexed:true"), |
| 92 |
(false, "one_indexed:false"), |
| 93 |
)] |
| 94 |
weekday_is_one_indexed: _, |
| 95 |
#[values( |
| 96 |
(true, "case_sensitive:true"), |
| 97 |
(false, "case_sensitive:false"), |
| 98 |
)] |
| 99 |
case_sensitive: _, |
| 100 |
#[values( |
| 101 |
(NonZero::new(1).unwrap(), "count:1"), |
| 102 |
(NonZero::new(2).unwrap(), "count:2"), |
| 103 |
(NonZero::new(3).unwrap(), "count:3"), |
| 104 |
(NonZero::new(10).unwrap(), "count:10"), |
| 105 |
(NonZero::new(100).unwrap(), "count:100"), |
| 106 |
(NonZero::new(1_000).unwrap(), "count:1000"), |
| 107 |
)] |
| 108 |
ignore_count: _, |
| 109 |
#[values( |
| 110 |
(UnixTimestampPrecision::Second, "precision:second"), |
| 111 |
(UnixTimestampPrecision::Millisecond, "precision:millisecond"), |
| 112 |
(UnixTimestampPrecision::Microsecond, "precision:microsecond"), |
| 113 |
(UnixTimestampPrecision::Nanosecond, "precision:nanosecond"), |
| 114 |
)] |
| 115 |
unix_timestamp_precision: _, |
| 116 |
#[values( |
| 117 |
(TrailingInput::Prohibit, "trailing_input:prohibit"), |
| 118 |
(TrailingInput::Discard, "trailing_input:discard"), |
| 119 |
)] |
| 120 |
trailing_input: _, |
| 121 |
) {} |
| 122 |
|
| 123 |
#[rstest] |
| 124 |
fn empty() { |
| 125 |
assert_eq!(format_description::parse_borrowed::<2>(""), Ok(vec![])); |
| 126 |
assert_eq!( |
| 127 |
format_description::parse_owned::<2>(""), |
| 128 |
Ok(OwnedFormatItem::Compound(Box::new([]))) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
#[rstest] |
| 133 |
#[case("foo bar", [b"foo bar".as_slice()])] |
| 134 |
#[case(" leading spaces", [b" leading spaces".as_slice()])] |
| 135 |
#[case("trailing spaces ", [b"trailing spaces ".as_slice()])] |
| 136 |
#[case(" ", [b" ".as_slice()])] |
| 137 |
#[case("[[", [b"[".as_slice()])] |
| 138 |
#[case("foo[[bar", [b"foo".as_slice(), b"[".as_slice(), b"bar".as_slice()])] |
| 139 |
fn only_literal<const N: usize>(#[case] format_description: &str, #[case] expected: [&[u8]; N]) { |
| 140 |
assert_eq!( |
| 141 |
format_description::parse(format_description), |
| 142 |
Ok(expected |
| 143 |
.into_iter() |
| 144 |
.map(BorrowedFormatItem::Literal) |
| 145 |
.collect()) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
#[rstest] |
| 150 |
#[case("[day]", Component::Day(Day::default()))] |
| 151 |
#[case("[end]", Component::End(End::default()))] |
| 152 |
#[case("[hour]", Component::Hour(Hour::default()))] |
| 153 |
#[case("[minute]", Component::Minute(Minute::default()))] |
| 154 |
#[case("[month]", Component::Month(Month::default()))] |
| 155 |
#[case("[offset_hour]", Component::OffsetHour(OffsetHour::default()))] |
| 156 |
#[case("[offset_minute]", Component::OffsetMinute(OffsetMinute::default()))] |
| 157 |
#[case("[offset_second]", Component::OffsetSecond(OffsetSecond::default()))] |
| 158 |
#[case("[ordinal]", Component::Ordinal(Ordinal::default()))] |
| 159 |
#[case("[period]", Component::Period(Period::default()))] |
| 160 |
#[case("[second]", Component::Second(Second::default()))] |
| 161 |
#[case("[subsecond]", Component::Subsecond(Subsecond::default()))] |
| 162 |
#[case("[unix_timestamp]", Component::UnixTimestamp(UnixTimestamp::default()))] |
| 163 |
#[case("[weekday]", Component::Weekday(Weekday::default()))] |
| 164 |
#[case("[week_number]", Component::WeekNumber(WeekNumber::default()))] |
| 165 |
#[case("[year]", Component::Year(Year::default()))] |
| 166 |
fn simple_component(#[case] format_description: &str, #[case] component: Component) { |
| 167 |
assert_eq!( |
| 168 |
format_description::parse(format_description), |
| 169 |
Ok(vec![BorrowedFormatItem::Component(component)]) |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
#[rstest] |
| 174 |
fn errors() { |
| 175 |
use InvalidFormatDescription::*; |
| 176 |
|
| 177 |
macro_rules! assert_errs { |
| 178 |
($($format_description:literal, $error:pat $(if $condition:expr)?,)*) => {$( |
| 179 |
assert!(matches!( |
| 180 |
format_description::parse($format_description), |
| 181 |
Err($error) $(if $condition)? |
| 182 |
)); |
| 183 |
assert!(matches!( |
| 184 |
format_description::parse_owned::<2>($format_description), |
| 185 |
Err($error) $(if $condition)? |
| 186 |
)); |
| 187 |
)*}; |
| 188 |
} |
| 189 |
|
| 190 |
assert_errs! { |
| 191 |
"[ invalid ]", InvalidComponentName { name, index: 2, .. } if name == "invalid", |
| 192 |
"[", MissingComponentName { index: 0, .. }, |
| 193 |
"[ ", MissingComponentName { index: 1, .. }, |
| 194 |
"[]", MissingComponentName { index: 0, .. }, |
| 195 |
"[day sign:mandatory]", InvalidModifier { value, index: 5, .. } if value == "sign", |
| 196 |
"[day sign:]", InvalidModifier { value, index: 9,.. } if value.is_empty(), |
| 197 |
"[day :mandatory]", InvalidModifier { value, index: 5,.. } if value.is_empty(), |
| 198 |
"[day sign:mandatory", UnclosedOpeningBracket { index: 0, .. }, |
| 199 |
"[day padding:invalid]", InvalidModifier { value, index: 13, .. } if value == "invalid", |
| 200 |
"[ignore]", MissingRequiredModifier { name: "count", index: 1, .. }, |
| 201 |
"[ignore count:70000]", InvalidModifier { value, index: 14, .. } if value == "70000", |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
macro_rules! placeholder { |
| 206 |
($($x:tt)*) => { |
| 207 |
" {}" |
| 208 |
}; |
| 209 |
} |
| 210 |
|
| 211 |
macro_rules! parse_with_modifiers { |
| 212 |
($modifier_name:literal, $($modifier:ident),+) => { |
| 213 |
format_description::parse( |
| 214 |
&format!( |
| 215 |
concat!( |
| 216 |
"[", |
| 217 |
$modifier_name, |
| 218 |
$(placeholder!($modifier),)+ |
| 219 |
"]", |
| 220 |
), |
| 221 |
$($modifier.1),+ |
| 222 |
) |
| 223 |
) |
| 224 |
}; |
| 225 |
} |
| 226 |
|
| 227 |
#[apply(modifiers)] |
| 228 |
fn day_component(padding: M<Padding>) { |
| 229 |
assert_eq!( |
| 230 |
parse_with_modifiers!("day", padding), |
| 231 |
Ok(vec![BorrowedFormatItem::Component(Component::Day( |
| 232 |
Day::default().with_padding(padding.0) |
| 233 |
))]) |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
#[apply(modifiers)] |
| 238 |
fn end_component(trailing_input: M<TrailingInput>) { |
| 239 |
assert_eq!( |
| 240 |
parse_with_modifiers!("end", trailing_input), |
| 241 |
Ok(vec![BorrowedFormatItem::Component(Component::End( |
| 242 |
End::default().with_trailing_input(trailing_input.0) |
| 243 |
))]) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
#[apply(modifiers)] |
| 248 |
fn minute_component(padding: M<Padding>) { |
| 249 |
assert_eq!( |
| 250 |
parse_with_modifiers!("minute", padding), |
| 251 |
Ok(vec![BorrowedFormatItem::Component(Component::Minute( |
| 252 |
Minute::default().with_padding(padding.0) |
| 253 |
))]) |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
#[apply(modifiers)] |
| 258 |
fn offset_minute_component(padding: M<Padding>) { |
| 259 |
assert_eq!( |
| 260 |
parse_with_modifiers!("offset_minute", padding), |
| 261 |
Ok(vec![BorrowedFormatItem::Component( |
| 262 |
Component::OffsetMinute(OffsetMinute::default().with_padding(padding.0)) |
| 263 |
)]) |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
#[apply(modifiers)] |
| 268 |
fn offset_second_component(padding: M<Padding>) { |
| 269 |
assert_eq!( |
| 270 |
parse_with_modifiers!("offset_second", padding), |
| 271 |
Ok(vec![BorrowedFormatItem::Component( |
| 272 |
Component::OffsetSecond(OffsetSecond::default().with_padding(padding.0)) |
| 273 |
)]) |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
#[apply(modifiers)] |
| 278 |
fn ordinal_component(padding: M<Padding>) { |
| 279 |
assert_eq!( |
| 280 |
parse_with_modifiers!("ordinal", padding), |
| 281 |
Ok(vec![BorrowedFormatItem::Component(Component::Ordinal( |
| 282 |
Ordinal::default().with_padding(padding.0) |
| 283 |
))]) |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
#[apply(modifiers)] |
| 288 |
fn second_component(padding: M<Padding>) { |
| 289 |
assert_eq!( |
| 290 |
parse_with_modifiers!("second", padding), |
| 291 |
Ok(vec![BorrowedFormatItem::Component(Component::Second( |
| 292 |
Second::default().with_padding(padding.0) |
| 293 |
))]) |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
#[apply(modifiers)] |
| 298 |
fn hour_component(padding: M<Padding>, hour_is_12_hour_clock: M<bool>) { |
| 299 |
assert_eq!( |
| 300 |
parse_with_modifiers!("hour", padding, hour_is_12_hour_clock), |
| 301 |
Ok(vec![BorrowedFormatItem::Component(Component::Hour( |
| 302 |
Hour::default() |
| 303 |
.with_padding(padding.0) |
| 304 |
.with_is_12_hour_clock(hour_is_12_hour_clock.0) |
| 305 |
))]) |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
#[apply(modifiers)] |
| 310 |
fn month_component(padding: M<Padding>, case_sensitive: M<bool>, month_repr: M<MonthRepr>) { |
| 311 |
assert_eq!( |
| 312 |
parse_with_modifiers!("month", padding, case_sensitive, month_repr), |
| 313 |
Ok(vec![BorrowedFormatItem::Component(Component::Month( |
| 314 |
Month::default() |
| 315 |
.with_padding(padding.0) |
| 316 |
.with_repr(month_repr.0) |
| 317 |
.with_case_sensitive(case_sensitive.0) |
| 318 |
))]) |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
#[apply(modifiers)] |
| 323 |
fn period_component(case_sensitive: M<bool>, period_is_uppercase: M<bool>) { |
| 324 |
assert_eq!( |
| 325 |
parse_with_modifiers!("period", period_is_uppercase, case_sensitive), |
| 326 |
Ok(vec![BorrowedFormatItem::Component(Component::Period( |
| 327 |
Period::default() |
| 328 |
.with_is_uppercase(period_is_uppercase.0) |
| 329 |
.with_case_sensitive(case_sensitive.0) |
| 330 |
))]) |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
#[apply(modifiers)] |
| 335 |
fn weekday_component( |
| 336 |
case_sensitive: M<bool>, |
| 337 |
weekday_is_one_indexed: M<bool>, |
| 338 |
weekday_repr: M<WeekdayRepr>, |
| 339 |
) { |
| 340 |
assert_eq!( |
| 341 |
parse_with_modifiers!( |
| 342 |
"weekday", |
| 343 |
case_sensitive, |
| 344 |
weekday_is_one_indexed, |
| 345 |
weekday_repr |
| 346 |
), |
| 347 |
Ok(vec![BorrowedFormatItem::Component(Component::Weekday( |
| 348 |
Weekday::default() |
| 349 |
.with_repr(weekday_repr.0) |
| 350 |
.with_one_indexed(weekday_is_one_indexed.0) |
| 351 |
.with_case_sensitive(case_sensitive.0) |
| 352 |
))]) |
| 353 |
); |
| 354 |
} |
| 355 |
|
| 356 |
#[apply(modifiers)] |
| 357 |
fn week_number_component(padding: M<Padding>, week_number_repr: M<WeekNumberRepr>) { |
| 358 |
assert_eq!( |
| 359 |
parse_with_modifiers!("week_number", padding, week_number_repr), |
| 360 |
Ok(vec![BorrowedFormatItem::Component(Component::WeekNumber( |
| 361 |
WeekNumber::default() |
| 362 |
.with_padding(padding.0) |
| 363 |
.with_repr(week_number_repr.0) |
| 364 |
))]) |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
#[apply(modifiers)] |
| 369 |
fn offset_hour_component(padding: M<Padding>, sign_is_mandatory: M<bool>) { |
| 370 |
assert_eq!( |
| 371 |
parse_with_modifiers!("offset_hour", padding, sign_is_mandatory), |
| 372 |
Ok(vec![BorrowedFormatItem::Component(Component::OffsetHour( |
| 373 |
OffsetHour::default() |
| 374 |
.with_padding(padding.0) |
| 375 |
.with_sign_is_mandatory(sign_is_mandatory.0) |
| 376 |
))]) |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
#[apply(modifiers)] |
| 381 |
fn year_component( |
| 382 |
padding: M<Padding>, |
| 383 |
year_repr: M<YearRepr>, |
| 384 |
year_range: M<YearRange>, |
| 385 |
year_is_iso_week_based: M<bool>, |
| 386 |
sign_is_mandatory: M<bool>, |
| 387 |
) { |
| 388 |
assert_eq!( |
| 389 |
parse_with_modifiers!( |
| 390 |
"year", |
| 391 |
padding, |
| 392 |
year_repr, |
| 393 |
year_range, |
| 394 |
year_is_iso_week_based, |
| 395 |
sign_is_mandatory |
| 396 |
), |
| 397 |
Ok(vec![BorrowedFormatItem::Component(Component::Year( |
| 398 |
Year::default() |
| 399 |
.with_padding(padding.0) |
| 400 |
.with_repr(year_repr.0) |
| 401 |
.with_range(year_range.0) |
| 402 |
.with_iso_week_based(year_is_iso_week_based.0) |
| 403 |
.with_sign_is_mandatory(sign_is_mandatory.0) |
| 404 |
))]) |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
#[apply(modifiers)] |
| 409 |
fn unix_timestamp_component( |
| 410 |
sign_is_mandatory: M<bool>, |
| 411 |
unix_timestamp_precision: M<UnixTimestampPrecision>, |
| 412 |
) { |
| 413 |
assert_eq!( |
| 414 |
parse_with_modifiers!( |
| 415 |
"unix_timestamp", |
| 416 |
sign_is_mandatory, |
| 417 |
unix_timestamp_precision |
| 418 |
), |
| 419 |
Ok(vec![BorrowedFormatItem::Component( |
| 420 |
Component::UnixTimestamp( |
| 421 |
UnixTimestamp::default() |
| 422 |
.with_sign_is_mandatory(sign_is_mandatory.0) |
| 423 |
.with_precision(unix_timestamp_precision.0) |
| 424 |
) |
| 425 |
)]) |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
#[apply(modifiers)] |
| 430 |
fn subsecond_component(subsecond_digits: M<SubsecondDigits>) { |
| 431 |
assert_eq!( |
| 432 |
parse_with_modifiers!("subsecond", subsecond_digits), |
| 433 |
Ok(vec![BorrowedFormatItem::Component(Component::Subsecond( |
| 434 |
Subsecond::default().with_digits(subsecond_digits.0) |
| 435 |
))]), |
| 436 |
); |
| 437 |
} |
| 438 |
|
| 439 |
#[apply(modifiers)] |
| 440 |
fn ignore_component(ignore_count: M<NonZero<u16>>) { |
| 441 |
assert_eq!( |
| 442 |
parse_with_modifiers!("ignore", ignore_count), |
| 443 |
Ok(vec![BorrowedFormatItem::Component(Component::Ignore( |
| 444 |
Ignore::count(ignore_count.0) |
| 445 |
))]) |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
#[rstest] |
| 450 |
fn optional() { |
| 451 |
assert_eq!( |
| 452 |
format_description::parse_owned::<2>("[optional [:[year]]]"), |
| 453 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 454 |
OwnedFormatItem::Compound(Box::new([ |
| 455 |
OwnedFormatItem::Literal(Box::new(*b":")), |
| 456 |
OwnedFormatItem::Component(Component::Year(Default::default())) |
| 457 |
])) |
| 458 |
))) |
| 459 |
); |
| 460 |
assert_eq!( |
| 461 |
format_description::parse_owned::<2>("[optional [[year]]]"), |
| 462 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 463 |
OwnedFormatItem::Component(Component::Year(Default::default())) |
| 464 |
))) |
| 465 |
); |
| 466 |
assert_eq!( |
| 467 |
format_description::parse_owned::<2>(r"[optional [\[]]"), |
| 468 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 469 |
OwnedFormatItem::Literal(Box::new(*b"[")) |
| 470 |
))) |
| 471 |
); |
| 472 |
assert_eq!( |
| 473 |
format_description::parse_owned::<2>(r"[optional [ \[ ]]"), |
| 474 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 475 |
OwnedFormatItem::Compound(Box::new([ |
| 476 |
OwnedFormatItem::Literal(Box::new(*b" ")), |
| 477 |
OwnedFormatItem::Literal(Box::new(*b"[")), |
| 478 |
OwnedFormatItem::Literal(Box::new(*b" ")), |
| 479 |
])) |
| 480 |
))) |
| 481 |
); |
| 482 |
} |
| 483 |
|
| 484 |
#[rstest] |
| 485 |
fn first() { |
| 486 |
assert_eq!( |
| 487 |
format_description::parse_owned::<2>("[first [a]]"), |
| 488 |
Ok(OwnedFormatItem::First(Box::new([ |
| 489 |
OwnedFormatItem::Literal(Box::new(*b"a")) |
| 490 |
]))) |
| 491 |
); |
| 492 |
assert_eq!( |
| 493 |
format_description::parse_owned::<2>("[first [a] [b]]"), |
| 494 |
Ok(OwnedFormatItem::First(Box::new([ |
| 495 |
OwnedFormatItem::Literal(Box::new(*b"a")), |
| 496 |
OwnedFormatItem::Literal(Box::new(*b"b")), |
| 497 |
]))) |
| 498 |
); |
| 499 |
assert_eq!( |
| 500 |
format_description::parse_owned::<2>("[first [a][b]]"), |
| 501 |
Ok(OwnedFormatItem::First(Box::new([ |
| 502 |
OwnedFormatItem::Literal(Box::new(*b"a")), |
| 503 |
OwnedFormatItem::Literal(Box::new(*b"b")), |
| 504 |
]))) |
| 505 |
); |
| 506 |
assert_eq!( |
| 507 |
format_description::parse_owned::<2>(r"[first [a][\[]]"), |
| 508 |
Ok(OwnedFormatItem::First(Box::new([ |
| 509 |
OwnedFormatItem::Literal(Box::new(*b"a")), |
| 510 |
OwnedFormatItem::Literal(Box::new(*b"[")), |
| 511 |
]))) |
| 512 |
); |
| 513 |
assert_eq!( |
| 514 |
format_description::parse_owned::<2>(r"[first [a][\[\[]]"), |
| 515 |
Ok(OwnedFormatItem::First(Box::new([ |
| 516 |
OwnedFormatItem::Literal(Box::new(*b"a")), |
| 517 |
OwnedFormatItem::Compound(Box::new([ |
| 518 |
OwnedFormatItem::Literal(Box::new(*b"[")), |
| 519 |
OwnedFormatItem::Literal(Box::new(*b"[")), |
| 520 |
])) |
| 521 |
]))) |
| 522 |
); |
| 523 |
assert_eq!( |
| 524 |
format_description::parse_owned::<2>( |
| 525 |
"[first [[period case:upper]] [[period case:lower]] ]" |
| 526 |
), |
| 527 |
Ok(OwnedFormatItem::First(Box::new([ |
| 528 |
OwnedFormatItem::Component(Component::Period( |
| 529 |
Period::default() |
| 530 |
.with_is_uppercase(true) |
| 531 |
.with_case_sensitive(true) |
| 532 |
)), |
| 533 |
OwnedFormatItem::Component(Component::Period( |
| 534 |
Period::default() |
| 535 |
.with_is_uppercase(false) |
| 536 |
.with_case_sensitive(true) |
| 537 |
)), |
| 538 |
]))) |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
#[rstest] |
| 543 |
fn backslash_escape() { |
| 544 |
assert_eq!( |
| 545 |
format_description::parse_owned::<2>(r"[optional [\]]]"), |
| 546 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 547 |
OwnedFormatItem::Literal(Box::new(*b"]")) |
| 548 |
))) |
| 549 |
); |
| 550 |
assert_eq!( |
| 551 |
format_description::parse_owned::<2>(r"[optional [\[]]"), |
| 552 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 553 |
OwnedFormatItem::Literal(Box::new(*b"[")) |
| 554 |
))) |
| 555 |
); |
| 556 |
assert_eq!( |
| 557 |
format_description::parse_owned::<2>(r"[optional [\\]]"), |
| 558 |
Ok(OwnedFormatItem::Optional(Box::new( |
| 559 |
OwnedFormatItem::Literal(Box::new(*br"\")) |
| 560 |
))) |
| 561 |
); |
| 562 |
assert_eq!( |
| 563 |
format_description::parse_owned::<2>(r"\\"), |
| 564 |
Ok(OwnedFormatItem::Literal(Box::new(*br"\"))) |
| 565 |
); |
| 566 |
assert_eq!( |
| 567 |
format_description::parse_owned::<2>(r"\["), |
| 568 |
Ok(OwnedFormatItem::Literal(Box::new(*br"["))) |
| 569 |
); |
| 570 |
assert_eq!( |
| 571 |
format_description::parse_owned::<2>(r"\]"), |
| 572 |
Ok(OwnedFormatItem::Literal(Box::new(*br"]"))) |
| 573 |
); |
| 574 |
assert_eq!( |
| 575 |
format_description::parse_owned::<2>(r"foo\\"), |
| 576 |
Ok(OwnedFormatItem::Compound(Box::new([ |
| 577 |
OwnedFormatItem::Literal(Box::new(*b"foo")), |
| 578 |
OwnedFormatItem::Literal(Box::new(*br"\")), |
| 579 |
]))) |
| 580 |
); |
| 581 |
assert_eq!( |
| 582 |
format_description::parse_borrowed::<2>(r"\\"), |
| 583 |
Ok(vec![BorrowedFormatItem::Literal(br"\")]) |
| 584 |
); |
| 585 |
assert_eq!( |
| 586 |
format_description::parse_borrowed::<2>(r"\["), |
| 587 |
Ok(vec![BorrowedFormatItem::Literal(br"[")]) |
| 588 |
); |
| 589 |
assert_eq!( |
| 590 |
format_description::parse_borrowed::<2>(r"\]"), |
| 591 |
Ok(vec![BorrowedFormatItem::Literal(br"]")]) |
| 592 |
); |
| 593 |
assert_eq!( |
| 594 |
format_description::parse_borrowed::<2>(r"foo\\"), |
| 595 |
Ok(vec![ |
| 596 |
BorrowedFormatItem::Literal(b"foo"), |
| 597 |
BorrowedFormatItem::Literal(br"\"), |
| 598 |
]) |
| 599 |
); |
| 600 |
} |
| 601 |
|
| 602 |
#[rstest] |
| 603 |
#[case(r"\a", 1)] |
| 604 |
#[case(r"\", 0)] |
| 605 |
fn backslash_escape_error(#[case] format_description: &str, #[case] expected_index: usize) { |
| 606 |
assert!(matches!( |
| 607 |
format_description::parse_owned::<2>(format_description), |
| 608 |
Err(InvalidFormatDescription::Expected { |
| 609 |
what: "valid escape sequence", |
| 610 |
index, |
| 611 |
.. |
| 612 |
}) if index == expected_index |
| 613 |
)); |
| 614 |
assert!(matches!( |
| 615 |
format_description::parse_borrowed::<2>(format_description), |
| 616 |
Err(InvalidFormatDescription::Expected { |
| 617 |
what: "valid escape sequence", |
| 618 |
index, |
| 619 |
.. |
| 620 |
}) if index == expected_index |
| 621 |
)); |
| 622 |
} |
| 623 |
|
| 624 |
#[rstest] |
| 625 |
fn nested_v1_error() { |
| 626 |
assert!(matches!( |
| 627 |
format_description::parse_owned::<2>("[optional [[[]]"), |
| 628 |
Err(InvalidFormatDescription::MissingComponentName { index: 11, .. }) |
| 629 |
)); |
| 630 |
assert!(matches!( |
| 631 |
format_description::parse_owned::<2>("[optional [ [[ ]]"), |
| 632 |
Err(InvalidFormatDescription::MissingComponentName { index: 12, .. }) |
| 633 |
)); |
| 634 |
assert!(matches!( |
| 635 |
format_description::parse_owned::<2>("[first [a][[[]]"), |
| 636 |
Err(InvalidFormatDescription::UnclosedOpeningBracket { index: 0, .. }) |
| 637 |
)); |
| 638 |
} |
| 639 |
|
| 640 |
#[rstest] |
| 641 |
fn nested_error() { |
| 642 |
use InvalidFormatDescription::*; |
| 643 |
|
| 644 |
assert!(matches!( |
| 645 |
format_description::parse("[optional []]"), |
| 646 |
Err(NotSupported { |
| 647 |
what: "optional item", |
| 648 |
context: "runtime-parsed format descriptions", |
| 649 |
index: 0, |
| 650 |
.. |
| 651 |
}) |
| 652 |
)); |
| 653 |
assert!(matches!( |
| 654 |
format_description::parse("[first []]"), |
| 655 |
Err(NotSupported { |
| 656 |
what: "'first' item", |
| 657 |
context: "runtime-parsed format descriptions", |
| 658 |
index: 0, |
| 659 |
.. |
| 660 |
}) |
| 661 |
)); |
| 662 |
assert!(matches!( |
| 663 |
format_description::parse_owned::<2>("[year [month]]"), |
| 664 |
Err(InvalidModifier { value, index: 6, .. }) if value == "[" |
| 665 |
)); |
| 666 |
assert!(matches!( |
| 667 |
format_description::parse_owned::<2>("[optional[]]"), |
| 668 |
Err(Expected { |
| 669 |
what: "whitespace after `optional`", |
| 670 |
index: 8, |
| 671 |
.. |
| 672 |
}) |
| 673 |
)); |
| 674 |
assert!(matches!( |
| 675 |
format_description::parse_owned::<2>("[first[]]"), |
| 676 |
Err(Expected { |
| 677 |
what: "whitespace after `first`", |
| 678 |
index: 5, |
| 679 |
.. |
| 680 |
}) |
| 681 |
)); |
| 682 |
assert!(matches!( |
| 683 |
format_description::parse_owned::<2>("[optional []"), |
| 684 |
Err(UnclosedOpeningBracket { index: 0, .. }) |
| 685 |
)); |
| 686 |
assert!(matches!( |
| 687 |
format_description::parse_owned::<2>("[first []"), |
| 688 |
Err(UnclosedOpeningBracket { index: 0, .. }) |
| 689 |
)); |
| 690 |
assert!(matches!( |
| 691 |
format_description::parse_owned::<2>("[optional ["), |
| 692 |
Err(UnclosedOpeningBracket { index: 10, .. }) |
| 693 |
)); |
| 694 |
assert!(matches!( |
| 695 |
format_description::parse_owned::<2>("[optional [[year"), |
| 696 |
Err(UnclosedOpeningBracket { index: 11, .. }) |
| 697 |
)); |
| 698 |
assert!(matches!( |
| 699 |
format_description::parse_owned::<2>("[optional "), |
| 700 |
Err(Expected { |
| 701 |
what: "opening bracket", |
| 702 |
index: 9, |
| 703 |
.. |
| 704 |
}) |
| 705 |
)); |
| 706 |
} |
| 707 |
|
| 708 |
#[rstest] |
| 709 |
#[case("[", "missing component name at byte index 0")] |
| 710 |
#[case("[foo", "unclosed opening bracket at byte index 0")] |
| 711 |
#[case("[foo]", "invalid component name `foo` at byte index 1")] |
| 712 |
#[case("[day bar]", "invalid modifier `bar` at byte index 5")] |
| 713 |
#[case("[]", "missing component name at byte index 0")] |
| 714 |
#[case( |
| 715 |
"[optional []]", |
| 716 |
"optional item is not supported in runtime-parsed format descriptions at byte index 0" |
| 717 |
)] |
| 718 |
#[case( |
| 719 |
"[ignore]", |
| 720 |
"missing required modifier `count` for component at byte index 1" |
| 721 |
)] |
| 722 |
fn error_display(#[case] format_description: &str, #[case] error: &str) { |
| 723 |
// la10736/rstest#217 |
| 724 |
#[expect(clippy::unwrap_used, reason = "purpose of the test")] |
| 725 |
let test = || { |
| 726 |
assert_eq!( |
| 727 |
format_description::parse(format_description) |
| 728 |
.unwrap_err() |
| 729 |
.to_string(), |
| 730 |
error |
| 731 |
); |
| 732 |
}; |
| 733 |
|
| 734 |
test(); |
| 735 |
} |
| 736 |
|
| 737 |
#[rstest] |
| 738 |
#[case("[optional ", "expected opening bracket at byte index 9")] |
| 739 |
fn error_display_owned(#[case] format_description: &str, #[case] error: &str) { |
| 740 |
// la10736/rstest#217 |
| 741 |
#[expect(clippy::unwrap_used, reason = "purpose of the test")] |
| 742 |
let test = || { |
| 743 |
assert_eq!( |
| 744 |
format_description::parse_owned::<2>(format_description) |
| 745 |
.unwrap_err() |
| 746 |
.to_string(), |
| 747 |
error |
| 748 |
) |
| 749 |
}; |
| 750 |
|
| 751 |
test(); |
| 752 |
} |
| 753 |
|
| 754 |
#[rstest] |
| 755 |
fn rfc_3339() { |
| 756 |
assert_eq!( |
| 757 |
format_description::parse( |
| 758 |
"[year]-[month repr:numerical]-[day]T[hour]:[minute]:[second].[subsecond][offset_hour \ |
| 759 |
sign:mandatory]:[offset_minute]" |
| 760 |
), |
| 761 |
Ok(vec![ |
| 762 |
BorrowedFormatItem::Component(Component::Year( |
| 763 |
Year::default() |
| 764 |
.with_padding(Padding::Zero) |
| 765 |
.with_repr(YearRepr::Full) |
| 766 |
.with_iso_week_based(false) |
| 767 |
.with_sign_is_mandatory(false) |
| 768 |
)), |
| 769 |
BorrowedFormatItem::Literal(b"-"), |
| 770 |
BorrowedFormatItem::Component(Component::Month( |
| 771 |
Month::default().with_padding(Padding::Zero) |
| 772 |
)), |
| 773 |
BorrowedFormatItem::Literal(b"-"), |
| 774 |
BorrowedFormatItem::Component(Component::Day( |
| 775 |
Day::default().with_padding(Padding::Zero) |
| 776 |
)), |
| 777 |
BorrowedFormatItem::Literal(b"T"), |
| 778 |
BorrowedFormatItem::Component(Component::Hour( |
| 779 |
Hour::default() |
| 780 |
.with_padding(Padding::Zero) |
| 781 |
.with_is_12_hour_clock(false) |
| 782 |
)), |
| 783 |
BorrowedFormatItem::Literal(b":"), |
| 784 |
BorrowedFormatItem::Component(Component::Minute( |
| 785 |
Minute::default().with_padding(Padding::Zero) |
| 786 |
)), |
| 787 |
BorrowedFormatItem::Literal(b":"), |
| 788 |
BorrowedFormatItem::Component(Component::Second( |
| 789 |
Second::default().with_padding(Padding::Zero) |
| 790 |
)), |
| 791 |
BorrowedFormatItem::Literal(b"."), |
| 792 |
BorrowedFormatItem::Component(Component::Subsecond( |
| 793 |
Subsecond::default().with_digits(SubsecondDigits::OneOrMore) |
| 794 |
)), |
| 795 |
BorrowedFormatItem::Component(Component::OffsetHour( |
| 796 |
OffsetHour::default() |
| 797 |
.with_padding(Padding::Zero) |
| 798 |
.with_sign_is_mandatory(true) |
| 799 |
)), |
| 800 |
BorrowedFormatItem::Literal(b":"), |
| 801 |
BorrowedFormatItem::Component(Component::OffsetMinute( |
| 802 |
OffsetMinute::default().with_padding(Padding::Zero) |
| 803 |
)) |
| 804 |
]) |
| 805 |
); |
| 806 |
} |
| 807 |
|
| 808 |
#[rstest] |
| 809 |
#[case("foo", format_description!("foo"))] |
| 810 |
#[case("%a", format_description!("[weekday repr:short]"))] |
| 811 |
#[case("%A", format_description!("[weekday]"))] |
| 812 |
#[case("%b", format_description!("[month repr:short]"))] |
| 813 |
#[case("%B", format_description!("[month repr:long]"))] |
| 814 |
#[case("%C", format_description!("[year repr:century]"))] |
| 815 |
#[case("%d", format_description!("[day]"))] |
| 816 |
#[case("%e", format_description!("[day padding:space]"))] |
| 817 |
#[case("%g", format_description!("[year repr:last_two base:iso_week]"))] |
| 818 |
#[case("%G", format_description!("[year base:iso_week]"))] |
| 819 |
#[case("%h", format_description!("[month repr:short]"))] |
| 820 |
#[case("%H", format_description!("[hour]"))] |
| 821 |
#[case("%I", format_description!("[hour repr:12]"))] |
| 822 |
#[case("%j", format_description!("[ordinal]"))] |
| 823 |
#[case("%k", format_description!("[hour padding:space]"))] |
| 824 |
#[case("%l", format_description!("[hour repr:12 padding:space]"))] |
| 825 |
#[case("%m", format_description!("[month]"))] |
| 826 |
#[case("%M", format_description!("[minute]"))] |
| 827 |
#[case("%n", format_description!("\n"))] |
| 828 |
#[case("%p", format_description!("[period]"))] |
| 829 |
#[case("%P", format_description!("[period case:lower]"))] |
| 830 |
#[case("%s", format_description!("[unix_timestamp]"))] |
| 831 |
#[case("%S", format_description!("[second]"))] |
| 832 |
#[case("%t", format_description!("\t"))] |
| 833 |
#[case("%u", format_description!("[weekday repr:monday]"))] |
| 834 |
#[case("%U", format_description!("[week_number repr:sunday]"))] |
| 835 |
#[case("%V", format_description!("[week_number]"))] |
| 836 |
#[case("%w", format_description!("[weekday repr:sunday]"))] |
| 837 |
#[case("%W", format_description!("[week_number repr:monday]"))] |
| 838 |
#[case("%y", format_description!("[year repr:last_two]"))] |
| 839 |
#[case("%Y", format_description!("[year]"))] |
| 840 |
#[case("%%", format_description!("%"))] |
| 841 |
fn strftime_equivalence( |
| 842 |
#[case] strftime: &str, |
| 843 |
#[case] custom: StaticFormatDescription, |
| 844 |
) -> time::Result<()> { |
| 845 |
let borrowed = format_description::parse_strftime_borrowed(strftime)?; |
| 846 |
let owned = format_description::parse_strftime_owned(strftime)?; |
| 847 |
|
| 848 |
assert_eq!(borrowed, custom); |
| 849 |
assert_eq!(owned, OwnedFormatItem::from(custom)); |
| 850 |
|
| 851 |
Ok(()) |
| 852 |
} |
| 853 |
|
| 854 |
#[rstest] |
| 855 |
#[case( |
| 856 |
"%c", |
| 857 |
"[weekday repr:short] [month repr:short] [day padding:space] [hour]:[minute]:[second] [year]" |
| 858 |
)] |
| 859 |
#[case("%D", "[month]/[day]/[year repr:last_two]")] |
| 860 |
#[case("%F", "[year]-[month repr:numerical]-[day]")] |
| 861 |
#[case("%r", "[hour repr:12]:[minute]:[second] [period]")] |
| 862 |
#[case("%R", "[hour]:[minute]")] |
| 863 |
#[case("%T", "[hour]:[minute]:[second]")] |
| 864 |
#[case("%x", "[month]/[day]/[year repr:last_two]")] |
| 865 |
#[case("%X", "[hour]:[minute]:[second]")] |
| 866 |
#[case("%z", "[offset_hour sign:mandatory][offset_minute]")] |
| 867 |
fn strftime_compound_equivalence(#[case] strftime: &str, #[case] custom: &str) -> time::Result<()> { |
| 868 |
let borrowed = format_description::parse_strftime_borrowed(strftime)?; |
| 869 |
let owned = format_description::parse_strftime_owned(strftime)?; |
| 870 |
let custom = format_description::parse(custom)?; |
| 871 |
// Until equality is implemented better, we need to convert to a compound. |
| 872 |
let custom = vec![BorrowedFormatItem::Compound(&custom)]; |
| 873 |
|
| 874 |
assert_eq!(borrowed, custom); |
| 875 |
assert_eq!(owned, OwnedFormatItem::from(custom)); |
| 876 |
|
| 877 |
Ok(()) |
| 878 |
} |