| 1 |
use super::*; |
| 2 |
use core::time::Duration; |
| 3 |
use serde_with::{ |
| 4 |
DurationMicroSeconds, DurationMicroSecondsWithFrac, DurationMilliSeconds, |
| 5 |
DurationMilliSecondsWithFrac, DurationNanoSeconds, DurationNanoSecondsWithFrac, |
| 6 |
DurationSeconds, DurationSecondsWithFrac, TimestampMicroSeconds, TimestampMicroSecondsWithFrac, |
| 7 |
TimestampMilliSeconds, TimestampMilliSecondsWithFrac, TimestampNanoSeconds, |
| 8 |
TimestampNanoSecondsWithFrac, TimestampSeconds, TimestampSecondsWithFrac, |
| 9 |
}; |
| 10 |
use std::time::SystemTime; |
| 11 |
|
| 12 |
#[test] |
| 13 |
fn test_duration_seconds() { |
| 14 |
let zero = Duration::new(0, 0); |
| 15 |
let one_second = Duration::new(1, 0); |
| 16 |
let half_second = Duration::new(0, 500_000_000); |
| 17 |
|
| 18 |
#[serde_as] |
| 19 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 20 |
struct IntStrict(#[serde_as(as = "DurationSeconds")] Duration); |
| 21 |
|
| 22 |
is_equal(IntStrict(zero), expect![[r#"0"#]]); |
| 23 |
is_equal(IntStrict(one_second), expect![[r#"1"#]]); |
| 24 |
check_serialization(IntStrict(half_second), expect![[r#"1"#]]); |
| 25 |
check_error_deserialization::<IntStrict>( |
| 26 |
r#""1""#, |
| 27 |
expect![[r#"invalid type: string "1", expected u64 at line 1 column 3"#]], |
| 28 |
); |
| 29 |
check_error_deserialization::<IntStrict>( |
| 30 |
r#"-1"#, |
| 31 |
expect![[r#"invalid value: integer `-1`, expected u64 at line 1 column 2"#]], |
| 32 |
); |
| 33 |
|
| 34 |
#[serde_as] |
| 35 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 36 |
struct IntFlexible(#[serde_as(as = "DurationSeconds<u64, Flexible>")] Duration); |
| 37 |
|
| 38 |
is_equal(IntFlexible(zero), expect![[r#"0"#]]); |
| 39 |
is_equal(IntFlexible(one_second), expect![[r#"1"#]]); |
| 40 |
check_serialization(IntFlexible(half_second), expect![[r#"1"#]]); |
| 41 |
check_deserialization(IntFlexible(half_second), r#""0.5""#); |
| 42 |
check_deserialization(IntFlexible(one_second), r#""1""#); |
| 43 |
check_deserialization(IntFlexible(zero), r#""0""#); |
| 44 |
check_error_deserialization::<IntFlexible>( |
| 45 |
r#""a""#, |
| 46 |
expect![[ |
| 47 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 48 |
]], |
| 49 |
); |
| 50 |
check_error_deserialization::<IntFlexible>( |
| 51 |
r#"-1"#, |
| 52 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 53 |
); |
| 54 |
|
| 55 |
#[serde_as] |
| 56 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 57 |
struct F64Strict(#[serde_as(as = "DurationSeconds<f64>")] Duration); |
| 58 |
|
| 59 |
is_equal(F64Strict(zero), expect![[r#"0.0"#]]); |
| 60 |
is_equal(F64Strict(one_second), expect![[r#"1.0"#]]); |
| 61 |
check_serialization(F64Strict(half_second), expect![[r#"1.0"#]]); |
| 62 |
check_deserialization(F64Strict(one_second), r#"0.5"#); |
| 63 |
check_error_deserialization::<F64Strict>( |
| 64 |
r#""1""#, |
| 65 |
expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]], |
| 66 |
); |
| 67 |
check_error_deserialization::<F64Strict>( |
| 68 |
r#"-1.0"#, |
| 69 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 70 |
); |
| 71 |
|
| 72 |
#[serde_as] |
| 73 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 74 |
struct F64Flexible(#[serde_as(as = "DurationSeconds<f64, Flexible>")] Duration); |
| 75 |
|
| 76 |
is_equal(F64Flexible(zero), expect![[r#"0.0"#]]); |
| 77 |
is_equal(F64Flexible(one_second), expect![[r#"1.0"#]]); |
| 78 |
check_serialization(F64Flexible(half_second), expect![[r#"1.0"#]]); |
| 79 |
check_deserialization(F64Flexible(half_second), r#""0.5""#); |
| 80 |
check_deserialization(F64Flexible(one_second), r#""1""#); |
| 81 |
check_deserialization(F64Flexible(zero), r#""0""#); |
| 82 |
check_error_deserialization::<F64Flexible>( |
| 83 |
r#""a""#, |
| 84 |
expect![[ |
| 85 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 86 |
]], |
| 87 |
); |
| 88 |
check_error_deserialization::<F64Flexible>( |
| 89 |
r#"-1"#, |
| 90 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 91 |
); |
| 92 |
|
| 93 |
#[serde_as] |
| 94 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 95 |
struct StringStrict(#[serde_as(as = "DurationSeconds<String>")] Duration); |
| 96 |
|
| 97 |
is_equal(StringStrict(zero), expect![[r#""0""#]]); |
| 98 |
is_equal(StringStrict(one_second), expect![[r#""1""#]]); |
| 99 |
check_serialization(StringStrict(half_second), expect![[r#""1""#]]); |
| 100 |
check_error_deserialization::<StringStrict>( |
| 101 |
r#"1"#, |
| 102 |
expect![[ |
| 103 |
r#"invalid type: integer `1`, expected a string containing a number at line 1 column 1"# |
| 104 |
]], |
| 105 |
); |
| 106 |
check_error_deserialization::<StringStrict>( |
| 107 |
r#"-1"#, |
| 108 |
expect![[ |
| 109 |
r#"invalid type: integer `-1`, expected a string containing a number at line 1 column 2"# |
| 110 |
]], |
| 111 |
); |
| 112 |
|
| 113 |
#[serde_as] |
| 114 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 115 |
struct StringFlexible(#[serde_as(as = "DurationSeconds<String, Flexible>")] Duration); |
| 116 |
|
| 117 |
is_equal(StringFlexible(zero), expect![[r#""0""#]]); |
| 118 |
is_equal(StringFlexible(one_second), expect![[r#""1""#]]); |
| 119 |
check_serialization(StringFlexible(half_second), expect![[r#""1""#]]); |
| 120 |
check_deserialization(StringFlexible(half_second), r#""0.5""#); |
| 121 |
check_deserialization(StringFlexible(one_second), r#""1""#); |
| 122 |
check_deserialization(StringFlexible(zero), r#""0""#); |
| 123 |
check_error_deserialization::<StringFlexible>( |
| 124 |
r#""a""#, |
| 125 |
expect![[ |
| 126 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 127 |
]], |
| 128 |
); |
| 129 |
check_error_deserialization::<StringFlexible>( |
| 130 |
r#"-1"#, |
| 131 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_duration_seconds_with_frac() { |
| 137 |
let zero = Duration::new(0, 0); |
| 138 |
let one_second = Duration::new(1, 0); |
| 139 |
let half_second = Duration::new(0, 500_000_000); |
| 140 |
|
| 141 |
#[serde_as] |
| 142 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 143 |
struct F64Strict(#[serde_as(as = "DurationSecondsWithFrac<f64>")] Duration); |
| 144 |
|
| 145 |
is_equal(F64Strict(zero), expect![[r#"0.0"#]]); |
| 146 |
is_equal(F64Strict(one_second), expect![[r#"1.0"#]]); |
| 147 |
is_equal(F64Strict(half_second), expect![[r#"0.5"#]]); |
| 148 |
check_error_deserialization::<F64Strict>( |
| 149 |
r#""1""#, |
| 150 |
expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]], |
| 151 |
); |
| 152 |
check_error_deserialization::<F64Strict>( |
| 153 |
r#"-1.0"#, |
| 154 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 155 |
); |
| 156 |
|
| 157 |
#[serde_as] |
| 158 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 159 |
struct F64Flexible(#[serde_as(as = "DurationSecondsWithFrac<f64, Flexible>")] Duration); |
| 160 |
|
| 161 |
is_equal(F64Flexible(zero), expect![[r#"0.0"#]]); |
| 162 |
is_equal(F64Flexible(one_second), expect![[r#"1.0"#]]); |
| 163 |
is_equal(F64Flexible(half_second), expect![[r#"0.5"#]]); |
| 164 |
check_deserialization(F64Flexible(one_second), r#""1""#); |
| 165 |
check_deserialization(F64Flexible(zero), r#""0""#); |
| 166 |
check_error_deserialization::<F64Flexible>( |
| 167 |
r#""a""#, |
| 168 |
expect![[ |
| 169 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 170 |
]], |
| 171 |
); |
| 172 |
check_error_deserialization::<F64Flexible>( |
| 173 |
r#"-1"#, |
| 174 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 175 |
); |
| 176 |
|
| 177 |
#[serde_as] |
| 178 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 179 |
struct StringStrict(#[serde_as(as = "DurationSecondsWithFrac<String>")] Duration); |
| 180 |
|
| 181 |
is_equal(StringStrict(zero), expect![[r#""0""#]]); |
| 182 |
is_equal(StringStrict(one_second), expect![[r#""1""#]]); |
| 183 |
is_equal(StringStrict(half_second), expect![[r#""0.5""#]]); |
| 184 |
check_error_deserialization::<StringStrict>( |
| 185 |
r#"1"#, |
| 186 |
expect![[r#"invalid type: integer `1`, expected a string at line 1 column 1"#]], |
| 187 |
); |
| 188 |
check_error_deserialization::<StringStrict>( |
| 189 |
r#"-1"#, |
| 190 |
expect![[r#"invalid type: integer `-1`, expected a string at line 1 column 2"#]], |
| 191 |
); |
| 192 |
|
| 193 |
#[serde_as] |
| 194 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 195 |
struct StringFlexible(#[serde_as(as = "DurationSecondsWithFrac<String, Flexible>")] Duration); |
| 196 |
|
| 197 |
is_equal(StringFlexible(zero), expect![[r#""0""#]]); |
| 198 |
is_equal(StringFlexible(one_second), expect![[r#""1""#]]); |
| 199 |
is_equal(StringFlexible(half_second), expect![[r#""0.5""#]]); |
| 200 |
check_deserialization(StringFlexible(zero), r#""0""#); |
| 201 |
check_error_deserialization::<StringFlexible>( |
| 202 |
r#""a""#, |
| 203 |
expect![[ |
| 204 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 205 |
]], |
| 206 |
); |
| 207 |
check_error_deserialization::<StringFlexible>( |
| 208 |
r#"-1"#, |
| 209 |
expect![[r#"std::time::Duration cannot be negative"#]], |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn test_timestamp_seconds_systemtime() { |
| 215 |
let zero = SystemTime::UNIX_EPOCH; |
| 216 |
let one_second = SystemTime::UNIX_EPOCH |
| 217 |
.checked_add(Duration::new(1, 0)) |
| 218 |
.unwrap(); |
| 219 |
let half_second = SystemTime::UNIX_EPOCH |
| 220 |
.checked_add(Duration::new(0, 500_000_000)) |
| 221 |
.unwrap(); |
| 222 |
let minus_one_second = SystemTime::UNIX_EPOCH |
| 223 |
.checked_sub(Duration::new(1, 0)) |
| 224 |
.unwrap(); |
| 225 |
let minus_half_second = SystemTime::UNIX_EPOCH |
| 226 |
.checked_sub(Duration::new(0, 500_000_000)) |
| 227 |
.unwrap(); |
| 228 |
|
| 229 |
#[serde_as] |
| 230 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 231 |
struct StructIntStrict(#[serde_as(as = "TimestampSeconds")] SystemTime); |
| 232 |
|
| 233 |
is_equal(StructIntStrict(zero), expect![[r#"0"#]]); |
| 234 |
is_equal(StructIntStrict(one_second), expect![[r#"1"#]]); |
| 235 |
is_equal(StructIntStrict(minus_one_second), expect![[r#"-1"#]]); |
| 236 |
check_serialization(StructIntStrict(half_second), expect![[r#"1"#]]); |
| 237 |
check_serialization(StructIntStrict(minus_half_second), expect![[r#"-1"#]]); |
| 238 |
check_error_deserialization::<StructIntStrict>( |
| 239 |
r#""1""#, |
| 240 |
expect![[r#"invalid type: string "1", expected i64 at line 1 column 3"#]], |
| 241 |
); |
| 242 |
check_error_deserialization::<StructIntStrict>( |
| 243 |
r#"0.123"#, |
| 244 |
expect![[r#"invalid type: floating point `0.123`, expected i64 at line 1 column 5"#]], |
| 245 |
); |
| 246 |
|
| 247 |
#[serde_as] |
| 248 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 249 |
struct StructIntFlexible(#[serde_as(as = "TimestampSeconds<i64, Flexible>")] SystemTime); |
| 250 |
|
| 251 |
is_equal(StructIntFlexible(zero), expect![[r#"0"#]]); |
| 252 |
is_equal(StructIntFlexible(one_second), expect![[r#"1"#]]); |
| 253 |
is_equal(StructIntFlexible(minus_one_second), expect![[r#"-1"#]]); |
| 254 |
check_serialization(StructIntFlexible(half_second), expect![[r#"1"#]]); |
| 255 |
check_serialization(StructIntFlexible(minus_half_second), expect![[r#"-1"#]]); |
| 256 |
check_deserialization(StructIntFlexible(one_second), r#""1""#); |
| 257 |
check_deserialization(StructIntFlexible(one_second), r#"1.0"#); |
| 258 |
check_deserialization(StructIntFlexible(minus_half_second), r#""-0.5""#); |
| 259 |
check_deserialization(StructIntFlexible(half_second), r#"0.5"#); |
| 260 |
check_error_deserialization::<StructIntFlexible>( |
| 261 |
r#""a""#, |
| 262 |
expect![[ |
| 263 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 264 |
]], |
| 265 |
); |
| 266 |
|
| 267 |
#[serde_as] |
| 268 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 269 |
struct Structf64Strict(#[serde_as(as = "TimestampSeconds<f64>")] SystemTime); |
| 270 |
|
| 271 |
is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]); |
| 272 |
is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]); |
| 273 |
is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]); |
| 274 |
check_serialization(Structf64Strict(half_second), expect![[r#"1.0"#]]); |
| 275 |
check_serialization(Structf64Strict(minus_half_second), expect![[r#"-1.0"#]]); |
| 276 |
check_deserialization(Structf64Strict(one_second), r#"0.5"#); |
| 277 |
check_error_deserialization::<Structf64Strict>( |
| 278 |
r#""1""#, |
| 279 |
expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]], |
| 280 |
); |
| 281 |
|
| 282 |
#[serde_as] |
| 283 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 284 |
struct Structf64Flexible(#[serde_as(as = "TimestampSeconds<f64, Flexible>")] SystemTime); |
| 285 |
|
| 286 |
is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]); |
| 287 |
is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]); |
| 288 |
is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]); |
| 289 |
check_serialization(Structf64Flexible(half_second), expect![[r#"1.0"#]]); |
| 290 |
check_serialization(Structf64Flexible(minus_half_second), expect![[r#"-1.0"#]]); |
| 291 |
check_deserialization(Structf64Flexible(one_second), r#""1""#); |
| 292 |
check_deserialization(Structf64Flexible(one_second), r#"1.0"#); |
| 293 |
check_deserialization(Structf64Flexible(minus_half_second), r#""-0.5""#); |
| 294 |
check_deserialization(Structf64Flexible(half_second), r#"0.5"#); |
| 295 |
check_error_deserialization::<Structf64Flexible>( |
| 296 |
r#""a""#, |
| 297 |
expect![[ |
| 298 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 299 |
]], |
| 300 |
); |
| 301 |
|
| 302 |
#[serde_as] |
| 303 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 304 |
struct StructStringStrict(#[serde_as(as = "TimestampSeconds<String>")] SystemTime); |
| 305 |
|
| 306 |
is_equal(StructStringStrict(zero), expect![[r#""0""#]]); |
| 307 |
is_equal(StructStringStrict(one_second), expect![[r#""1""#]]); |
| 308 |
is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]); |
| 309 |
check_serialization(StructStringStrict(half_second), expect![[r#""1""#]]); |
| 310 |
check_serialization(StructStringStrict(minus_half_second), expect![[r#""-1""#]]); |
| 311 |
check_deserialization(StructStringStrict(one_second), r#""1""#); |
| 312 |
check_error_deserialization::<StructStringStrict>( |
| 313 |
r#""0.5""#, |
| 314 |
expect![[r#"invalid digit found in string at line 1 column 5"#]], |
| 315 |
); |
| 316 |
check_error_deserialization::<StructStringStrict>( |
| 317 |
r#""-0.5""#, |
| 318 |
expect![[r#"invalid digit found in string at line 1 column 6"#]], |
| 319 |
); |
| 320 |
check_error_deserialization::<StructStringStrict>( |
| 321 |
r#"1"#, |
| 322 |
expect![[ |
| 323 |
r#"invalid type: integer `1`, expected a string containing a number at line 1 column 1"# |
| 324 |
]], |
| 325 |
); |
| 326 |
check_error_deserialization::<StructStringStrict>( |
| 327 |
r#"0.1"#, |
| 328 |
expect![[ |
| 329 |
r#"invalid type: floating point `0.1`, expected a string containing a number at line 1 column 3"# |
| 330 |
]], |
| 331 |
); |
| 332 |
|
| 333 |
#[serde_as] |
| 334 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 335 |
struct StructStringFlexible(#[serde_as(as = "TimestampSeconds<String, Flexible>")] SystemTime); |
| 336 |
|
| 337 |
is_equal(StructStringFlexible(zero), expect![[r#""0""#]]); |
| 338 |
is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]); |
| 339 |
is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]); |
| 340 |
check_serialization(StructStringFlexible(half_second), expect![[r#""1""#]]); |
| 341 |
check_serialization( |
| 342 |
StructStringFlexible(minus_half_second), |
| 343 |
expect![[r#""-1""#]], |
| 344 |
); |
| 345 |
check_deserialization(StructStringFlexible(one_second), r#"1"#); |
| 346 |
check_deserialization(StructStringFlexible(one_second), r#"1.0"#); |
| 347 |
check_deserialization(StructStringFlexible(minus_half_second), r#""-0.5""#); |
| 348 |
check_deserialization(StructStringFlexible(half_second), r#"0.5"#); |
| 349 |
check_error_deserialization::<StructStringFlexible>( |
| 350 |
r#""a""#, |
| 351 |
expect![[ |
| 352 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 353 |
]], |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn test_timestamp_seconds_with_frac_systemtime() { |
| 359 |
let zero = SystemTime::UNIX_EPOCH; |
| 360 |
let one_second = SystemTime::UNIX_EPOCH |
| 361 |
.checked_add(Duration::new(1, 0)) |
| 362 |
.unwrap(); |
| 363 |
let half_second = SystemTime::UNIX_EPOCH |
| 364 |
.checked_add(Duration::new(0, 500_000_000)) |
| 365 |
.unwrap(); |
| 366 |
let minus_one_second = SystemTime::UNIX_EPOCH |
| 367 |
.checked_sub(Duration::new(1, 0)) |
| 368 |
.unwrap(); |
| 369 |
let minus_half_second = SystemTime::UNIX_EPOCH |
| 370 |
.checked_sub(Duration::new(0, 500_000_000)) |
| 371 |
.unwrap(); |
| 372 |
|
| 373 |
#[serde_as] |
| 374 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 375 |
struct Structf64Strict(#[serde_as(as = "TimestampSecondsWithFrac<f64>")] SystemTime); |
| 376 |
|
| 377 |
is_equal(Structf64Strict(zero), expect![[r#"0.0"#]]); |
| 378 |
is_equal(Structf64Strict(one_second), expect![[r#"1.0"#]]); |
| 379 |
is_equal(Structf64Strict(minus_one_second), expect![[r#"-1.0"#]]); |
| 380 |
is_equal(Structf64Strict(half_second), expect![[r#"0.5"#]]); |
| 381 |
is_equal(Structf64Strict(minus_half_second), expect![[r#"-0.5"#]]); |
| 382 |
check_error_deserialization::<Structf64Strict>( |
| 383 |
r#""1""#, |
| 384 |
expect![[r#"invalid type: string "1", expected f64 at line 1 column 3"#]], |
| 385 |
); |
| 386 |
|
| 387 |
#[serde_as] |
| 388 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 389 |
struct Structf64Flexible( |
| 390 |
#[serde_as(as = "TimestampSecondsWithFrac<f64, Flexible>")] SystemTime, |
| 391 |
); |
| 392 |
|
| 393 |
is_equal(Structf64Flexible(zero), expect![[r#"0.0"#]]); |
| 394 |
is_equal(Structf64Flexible(one_second), expect![[r#"1.0"#]]); |
| 395 |
is_equal(Structf64Flexible(minus_one_second), expect![[r#"-1.0"#]]); |
| 396 |
is_equal(Structf64Flexible(half_second), expect![[r#"0.5"#]]); |
| 397 |
is_equal(Structf64Flexible(minus_half_second), expect![[r#"-0.5"#]]); |
| 398 |
check_deserialization(Structf64Flexible(one_second), r#""1""#); |
| 399 |
check_deserialization(Structf64Flexible(one_second), r#"1.0"#); |
| 400 |
check_deserialization(Structf64Flexible(minus_half_second), r#""-0.5""#); |
| 401 |
check_deserialization(Structf64Flexible(half_second), r#"0.5"#); |
| 402 |
check_error_deserialization::<Structf64Flexible>( |
| 403 |
r#""a""#, |
| 404 |
expect![[ |
| 405 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 406 |
]], |
| 407 |
); |
| 408 |
|
| 409 |
#[serde_as] |
| 410 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 411 |
struct StructStringStrict(#[serde_as(as = "TimestampSecondsWithFrac<String>")] SystemTime); |
| 412 |
|
| 413 |
is_equal(StructStringStrict(zero), expect![[r#""0""#]]); |
| 414 |
is_equal(StructStringStrict(one_second), expect![[r#""1""#]]); |
| 415 |
is_equal(StructStringStrict(minus_one_second), expect![[r#""-1""#]]); |
| 416 |
is_equal(StructStringStrict(half_second), expect![[r#""0.5""#]]); |
| 417 |
is_equal( |
| 418 |
StructStringStrict(minus_half_second), |
| 419 |
expect![[r#""-0.5""#]], |
| 420 |
); |
| 421 |
check_error_deserialization::<StructStringStrict>( |
| 422 |
r#"1"#, |
| 423 |
expect![[r#"invalid type: integer `1`, expected a string at line 1 column 1"#]], |
| 424 |
); |
| 425 |
check_error_deserialization::<StructStringStrict>( |
| 426 |
r#"0.1"#, |
| 427 |
expect![[r#"invalid type: floating point `0.1`, expected a string at line 1 column 3"#]], |
| 428 |
); |
| 429 |
|
| 430 |
#[serde_as] |
| 431 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 432 |
struct StructStringFlexible( |
| 433 |
#[serde_as(as = "TimestampSecondsWithFrac<String, Flexible>")] SystemTime, |
| 434 |
); |
| 435 |
|
| 436 |
is_equal(StructStringFlexible(zero), expect![[r#""0""#]]); |
| 437 |
is_equal(StructStringFlexible(one_second), expect![[r#""1""#]]); |
| 438 |
is_equal(StructStringFlexible(minus_one_second), expect![[r#""-1""#]]); |
| 439 |
is_equal(StructStringFlexible(half_second), expect![[r#""0.5""#]]); |
| 440 |
is_equal( |
| 441 |
StructStringFlexible(minus_half_second), |
| 442 |
expect![[r#""-0.5""#]], |
| 443 |
); |
| 444 |
check_deserialization(StructStringFlexible(one_second), r#"1"#); |
| 445 |
check_deserialization(StructStringFlexible(one_second), r#"1.0"#); |
| 446 |
check_deserialization(StructStringFlexible(half_second), r#"0.5"#); |
| 447 |
check_error_deserialization::<StructStringFlexible>( |
| 448 |
r#""a""#, |
| 449 |
expect![[ |
| 450 |
r#"invalid value: string "a", expected an integer, a float, or a string containing a number at line 1 column 3"# |
| 451 |
]], |
| 452 |
); |
| 453 |
} |
| 454 |
|
| 455 |
macro_rules! smoketest { |
| 456 |
($($valuety:ty, $adapter:literal, $value:ident, $expect:tt;)*) => { |
| 457 |
$({ |
| 458 |
#[serde_as] |
| 459 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 460 |
struct S(#[serde_as(as = $adapter)] $valuety); |
| 461 |
#[allow(unused_braces)] |
| 462 |
is_equal(S($value), $expect); |
| 463 |
})* |
| 464 |
}; |
| 465 |
} |
| 466 |
|
| 467 |
#[test] |
| 468 |
fn test_duration_smoketest() { |
| 469 |
let one_second = Duration::new(1, 0); |
| 470 |
|
| 471 |
smoketest! { |
| 472 |
Duration, "DurationSeconds<u64>", one_second, {expect![[r#"1"#]]}; |
| 473 |
Duration, "DurationSeconds<f64>", one_second, {expect![[r#"1.0"#]]}; |
| 474 |
Duration, "DurationMilliSeconds<u64>", one_second, {expect![[r#"1000"#]]}; |
| 475 |
Duration, "DurationMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]}; |
| 476 |
Duration, "DurationMicroSeconds<u64>", one_second, {expect![[r#"1000000"#]]}; |
| 477 |
Duration, "DurationMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]}; |
| 478 |
Duration, "DurationNanoSeconds<u64>", one_second, {expect![[r#"1000000000"#]]}; |
| 479 |
Duration, "DurationNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]}; |
| 480 |
}; |
| 481 |
|
| 482 |
smoketest! { |
| 483 |
Duration, "DurationSecondsWithFrac", one_second, {expect![[r#"1.0"#]]}; |
| 484 |
Duration, "DurationSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]}; |
| 485 |
Duration, "DurationMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]}; |
| 486 |
Duration, "DurationMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]}; |
| 487 |
Duration, "DurationMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]}; |
| 488 |
Duration, "DurationMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]}; |
| 489 |
Duration, "DurationNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]}; |
| 490 |
Duration, "DurationNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]}; |
| 491 |
}; |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn test_timestamp_systemtime_smoketest() { |
| 496 |
let one_second = SystemTime::UNIX_EPOCH |
| 497 |
.checked_add(Duration::new(1, 0)) |
| 498 |
.unwrap(); |
| 499 |
|
| 500 |
smoketest! { |
| 501 |
SystemTime, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]}; |
| 502 |
SystemTime, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]}; |
| 503 |
SystemTime, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]}; |
| 504 |
SystemTime, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]}; |
| 505 |
SystemTime, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]}; |
| 506 |
SystemTime, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]}; |
| 507 |
SystemTime, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]}; |
| 508 |
SystemTime, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]}; |
| 509 |
}; |
| 510 |
|
| 511 |
smoketest! { |
| 512 |
SystemTime, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]}; |
| 513 |
SystemTime, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]}; |
| 514 |
SystemTime, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]}; |
| 515 |
SystemTime, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]}; |
| 516 |
SystemTime, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]}; |
| 517 |
SystemTime, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]}; |
| 518 |
SystemTime, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]}; |
| 519 |
SystemTime, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]}; |
| 520 |
}; |
| 521 |
} |