| 1 |
use rstest::rstest; |
| 2 |
use serde_test2::Compact; |
| 3 |
#[rustfmt::skip] // Tries to remove the leading `::`, which breaks compilation. |
| 4 |
use ::serde::{Deserialize, Serialize}; |
| 5 |
use serde_test2::{ |
| 6 |
Configure, Token, assert_de_tokens, assert_de_tokens_error, assert_ser_tokens_error, |
| 7 |
assert_tokens, |
| 8 |
}; |
| 9 |
use time::format_description::BorrowedFormatItem; |
| 10 |
use time::format_description::well_known::{Iso8601, iso8601}; |
| 11 |
use time::macros::{date, datetime, offset, time}; |
| 12 |
use time::{Date, OffsetDateTime, PlainDateTime, Time, UtcOffset, serde}; |
| 13 |
|
| 14 |
// Not used in the tests, but ensures that the macro compiles. |
| 15 |
#[expect(dead_code)] |
| 16 |
const ISO_FORMAT: Iso8601<{ iso8601::Config::DEFAULT.encode() }> = |
| 17 |
Iso8601::<{ iso8601::Config::DEFAULT.encode() }>; |
| 18 |
time::serde::format_description!(my_format, OffsetDateTime, ISO_FORMAT); |
| 19 |
time::serde::format_description!( |
| 20 |
my_format2, |
| 21 |
OffsetDateTime, |
| 22 |
Iso8601::<{ iso8601::Config::DEFAULT.encode() }> |
| 23 |
); |
| 24 |
|
| 25 |
mod nested { |
| 26 |
time::serde::format_description!( |
| 27 |
pub(super) offset_dt_format, |
| 28 |
OffsetDateTime, |
| 29 |
"custom format: [year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \ |
| 30 |
sign:mandatory]:[offset_minute]" |
| 31 |
); |
| 32 |
time::serde::format_description!( |
| 33 |
pub plain_dt_format, |
| 34 |
PlainDateTime, |
| 35 |
"custom format: [year]-[month]-[day] [hour]:[minute]:[second]" |
| 36 |
); |
| 37 |
time::serde::format_description!( |
| 38 |
pub(in crate::serde::macros) time_format, |
| 39 |
Time, |
| 40 |
"custom format: [minute]:[second]" |
| 41 |
); |
| 42 |
} |
| 43 |
serde::format_description!(date_format, Date, "custom format: [year]-[month]-[day]"); |
| 44 |
serde::format_description!( |
| 45 |
offset_format, |
| 46 |
UtcOffset, |
| 47 |
"custom format: [offset_hour sign:mandatory]:[offset_minute]" |
| 48 |
); |
| 49 |
|
| 50 |
const TIME_FORMAT_ALT: &[BorrowedFormatItem<'_>] = |
| 51 |
time::macros::format_description!("[hour]:[minute]"); |
| 52 |
serde::format_description!(time_format_alt, Time, TIME_FORMAT_ALT); |
| 53 |
serde::format_description!( |
| 54 |
time_format_alt2, |
| 55 |
Time, |
| 56 |
time::macros::format_description!("[hour]:[minute]") |
| 57 |
); |
| 58 |
|
| 59 |
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] |
| 60 |
struct TestCustomFormat { |
| 61 |
#[serde(with = "nested::offset_dt_format")] |
| 62 |
offset_dt: OffsetDateTime, |
| 63 |
#[serde(with = "nested::plain_dt_format::option")] |
| 64 |
plain_dt: Option<PlainDateTime>, |
| 65 |
#[serde(with = "date_format")] |
| 66 |
date: Date, |
| 67 |
#[serde(with = "nested::time_format::option")] |
| 68 |
time: Option<Time>, |
| 69 |
#[serde(with = "offset_format")] |
| 70 |
offset: UtcOffset, |
| 71 |
#[serde(with = "time_format_alt")] |
| 72 |
time_alt: Time, |
| 73 |
} |
| 74 |
|
| 75 |
#[rstest] |
| 76 |
#[case( |
| 77 |
TestCustomFormat { |
| 78 |
offset_dt: datetime!(2000-01-01 00:00 -4:00), |
| 79 |
plain_dt: Some(datetime!(2000-01-01 00:00)), |
| 80 |
date: date!(2000-01-01), |
| 81 |
time: None, |
| 82 |
offset: offset!(-4), |
| 83 |
time_alt: time!(12:34), |
| 84 |
}.compact(), |
| 85 |
&[ |
| 86 |
Token::Struct { |
| 87 |
name: "TestCustomFormat", |
| 88 |
len: 6, |
| 89 |
}, |
| 90 |
Token::Str("offset_dt"), |
| 91 |
Token::BorrowedStr("custom format: 2000-01-01 00:00:00 -04:00"), |
| 92 |
Token::Str("plain_dt"), |
| 93 |
Token::Some, |
| 94 |
Token::BorrowedStr("custom format: 2000-01-01 00:00:00"), |
| 95 |
Token::Str("date"), |
| 96 |
Token::BorrowedStr("custom format: 2000-01-01"), |
| 97 |
Token::Str("time"), |
| 98 |
Token::None, |
| 99 |
Token::Str("offset"), |
| 100 |
Token::BorrowedStr("custom format: -04:00"), |
| 101 |
Token::Str("time_alt"), |
| 102 |
Token::BorrowedStr("12:34"), |
| 103 |
Token::StructEnd, |
| 104 |
] |
| 105 |
)] |
| 106 |
fn custom_serialize(#[case] value: Compact<TestCustomFormat>, #[case] tokens: &[Token]) { |
| 107 |
assert_tokens(&value, tokens); |
| 108 |
} |
| 109 |
|
| 110 |
#[rstest] |
| 111 |
#[case( |
| 112 |
&[ |
| 113 |
Token::Struct { |
| 114 |
name: "TestCustomFormat", |
| 115 |
len: 6, |
| 116 |
}, |
| 117 |
Token::Str("offset_dt"), |
| 118 |
Token::BorrowedStr("custom format: 2000-01-01 0:00:00 -04:00"), |
| 119 |
], |
| 120 |
"the 'hour' component could not be parsed", |
| 121 |
)] |
| 122 |
#[case( |
| 123 |
&[ |
| 124 |
Token::Struct { |
| 125 |
name: "TestCustomFormat", |
| 126 |
len: 6, |
| 127 |
}, |
| 128 |
Token::Str("offset_dt"), |
| 129 |
Token::BorrowedStr("custom format: 2000-01-01 00:00:00 -04:00"), |
| 130 |
Token::Str("plain_dt"), |
| 131 |
Token::Some, |
| 132 |
Token::BorrowedStr("custom format: 2000-01-01 0:00:00 -04:00"), |
| 133 |
], |
| 134 |
"the 'hour' component could not be parsed" |
| 135 |
)] |
| 136 |
#[case( |
| 137 |
&[ |
| 138 |
Token::Struct { |
| 139 |
name: "TestCustomFormat", |
| 140 |
len: 6, |
| 141 |
}, |
| 142 |
Token::Str("offset_dt"), |
| 143 |
Token::Bool(false), |
| 144 |
], |
| 145 |
"invalid type: boolean `false`, expected a(n) `OffsetDateTime` in the format \"custom format: \ |
| 146 |
[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]\"" |
| 147 |
)] |
| 148 |
#[case( |
| 149 |
&[ |
| 150 |
Token::Struct { |
| 151 |
name: "TestCustomFormat", |
| 152 |
len: 6, |
| 153 |
}, |
| 154 |
Token::Str("offset_dt"), |
| 155 |
Token::BorrowedStr("custom format: 2000-01-01 00:00:00 -04:00"), |
| 156 |
Token::Str("plain_dt"), |
| 157 |
Token::Bool(false), |
| 158 |
], |
| 159 |
"invalid type: boolean `false`, expected an `Option<PlainDateTime>` in the format \"custom \ |
| 160 |
format: [year]-[month]-[day] [hour]:[minute]:[second]\"" |
| 161 |
)] |
| 162 |
fn custom_deserialize_error(#[case] tokens: &[Token], #[case] error: &str) { |
| 163 |
assert_de_tokens_error::<TestCustomFormat>(tokens, error); |
| 164 |
} |
| 165 |
|
| 166 |
// This format string has offset_hour and offset_minute, but is for formatting PlainDateTime. |
| 167 |
serde::format_description!( |
| 168 |
plain_date_time_format_bad, |
| 169 |
PlainDateTime, |
| 170 |
"[offset_hour]:[offset_minute]" |
| 171 |
); |
| 172 |
|
| 173 |
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] |
| 174 |
struct TestCustomFormatPlainDateTimeBad { |
| 175 |
#[serde(with = "plain_date_time_format_bad")] |
| 176 |
dt: PlainDateTime, |
| 177 |
} |
| 178 |
|
| 179 |
#[rstest] |
| 180 |
#[case( |
| 181 |
TestCustomFormatPlainDateTimeBad { |
| 182 |
dt: datetime!(2000-01-01 00:00), |
| 183 |
}, |
| 184 |
&[ |
| 185 |
Token::Struct { |
| 186 |
name: "TestCustomFormatPlainDateTimeBad", |
| 187 |
len: 1, |
| 188 |
}, |
| 189 |
Token::Str("dt"), |
| 190 |
], |
| 191 |
"The type being formatted does not contain sufficient information to format a component.", |
| 192 |
)] |
| 193 |
fn custom_serialize_bad_type_error( |
| 194 |
#[case] value: TestCustomFormatPlainDateTimeBad, |
| 195 |
#[case] tokens: &[Token], |
| 196 |
#[case] error: &str, |
| 197 |
) { |
| 198 |
assert_ser_tokens_error::<TestCustomFormatPlainDateTimeBad>(&value, tokens, error); |
| 199 |
} |
| 200 |
|
| 201 |
// Test the behavior of versioning. |
| 202 |
serde::format_description!(version = 1, version_test_1, Time, "[[ [hour]:[minute]"); |
| 203 |
serde::format_description!(version = 1, version_test_2, Time, r"\\ [hour]:[minute]"); |
| 204 |
serde::format_description!(version = 2, version_test_3, Time, r"\\ [hour]:[minute]"); |
| 205 |
serde::format_description!(version, Time, "[hour]:[minute]"); |
| 206 |
|
| 207 |
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] |
| 208 |
struct TestVersioning { |
| 209 |
#[serde(with = "version_test_1")] |
| 210 |
time_1: Time, |
| 211 |
#[serde(with = "version_test_2")] |
| 212 |
time_2: Time, |
| 213 |
#[serde(with = "version_test_3")] |
| 214 |
time_3: Time, |
| 215 |
#[serde(with = "version")] |
| 216 |
time_4: Time, |
| 217 |
} |
| 218 |
|
| 219 |
#[rstest] |
| 220 |
fn versioning() { |
| 221 |
let value = TestVersioning { |
| 222 |
time_1: Time::MIDNIGHT, |
| 223 |
time_2: Time::MIDNIGHT, |
| 224 |
time_3: Time::MIDNIGHT, |
| 225 |
time_4: Time::MIDNIGHT, |
| 226 |
}; |
| 227 |
assert_tokens( |
| 228 |
&value, |
| 229 |
&[ |
| 230 |
Token::Struct { |
| 231 |
name: "TestVersioning", |
| 232 |
len: 4, |
| 233 |
}, |
| 234 |
Token::Str("time_1"), |
| 235 |
Token::Str("[ 00:00"), |
| 236 |
Token::Str("time_2"), |
| 237 |
Token::Str(r"\\ 00:00"), |
| 238 |
Token::Str("time_3"), |
| 239 |
Token::Str(r"\ 00:00"), |
| 240 |
Token::Str("time_4"), |
| 241 |
Token::Str("00:00"), |
| 242 |
Token::StructEnd, |
| 243 |
], |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
serde::format_description!( |
| 248 |
version = 1, |
| 249 |
nested_v1, |
| 250 |
Time, |
| 251 |
"[hour]:[minute][optional [:[second]]]" |
| 252 |
); |
| 253 |
serde::format_description!( |
| 254 |
version = 2, |
| 255 |
nested_v2, |
| 256 |
Time, |
| 257 |
"[hour]:[minute][optional [:[second]]]" |
| 258 |
); |
| 259 |
|
| 260 |
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] |
| 261 |
struct TestNested { |
| 262 |
#[serde(with = "nested_v1")] |
| 263 |
time_1: Time, |
| 264 |
#[serde(with = "nested_v2")] |
| 265 |
time_2: Time, |
| 266 |
} |
| 267 |
|
| 268 |
#[rstest] |
| 269 |
#[case( |
| 270 |
TestNested { |
| 271 |
time_1: time!(12:34:56), |
| 272 |
time_2: time!(12:34:56), |
| 273 |
}, |
| 274 |
&[ |
| 275 |
Token::Struct { |
| 276 |
name: "TestNested", |
| 277 |
len: 2, |
| 278 |
}, |
| 279 |
Token::Str("time_1"), |
| 280 |
Token::Str("12:34:56"), |
| 281 |
Token::Str("time_2"), |
| 282 |
Token::Str("12:34:56"), |
| 283 |
Token::StructEnd, |
| 284 |
] |
| 285 |
)] |
| 286 |
fn nested_roundtrip(#[case] value: TestNested, #[case] tokens: &[Token]) { |
| 287 |
assert_tokens(&value, tokens); |
| 288 |
} |
| 289 |
|
| 290 |
#[rstest] |
| 291 |
#[case( |
| 292 |
TestNested { |
| 293 |
time_1: time!(12:34), |
| 294 |
time_2: time!(12:34), |
| 295 |
}, |
| 296 |
&[ |
| 297 |
Token::Struct { |
| 298 |
name: "TestNested", |
| 299 |
len: 2, |
| 300 |
}, |
| 301 |
Token::Str("time_1"), |
| 302 |
Token::Str("12:34"), |
| 303 |
Token::Str("time_2"), |
| 304 |
Token::Str("12:34"), |
| 305 |
Token::StructEnd, |
| 306 |
] |
| 307 |
)] |
| 308 |
fn nested_deserialize(#[case] value: TestNested, #[case] tokens: &[Token]) { |
| 309 |
assert_de_tokens(&value, tokens); |
| 310 |
} |