(deserializer: D)
| 354 | } |
| 355 | |
| 356 | fn numeric_type<'de, D>(deserializer: D) -> std::result::Result<TryParse<u64>, D::Error> |
| 357 | where |
| 358 | D: Deserializer<'de>, |
| 359 | { |
| 360 | struct NumericType(PhantomData<fn() -> TryParse<u64>>); |
| 361 | |
| 362 | impl Visitor<'_> for NumericType { |
| 363 | type Value = TryParse<u64>; |
| 364 | |
| 365 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 366 | formatter.write_str("A NumericType that can be reasonably coerced into a u64") |
| 367 | } |
| 368 | |
| 369 | fn visit_u64<E>(self, value: u64) -> std::result::Result<Self::Value, E> |
| 370 | where |
| 371 | E: de::Error, |
| 372 | { |
| 373 | Ok(TryParse::Parsed(value)) |
| 374 | } |
| 375 | |
| 376 | fn visit_f64<E>(self, value: f64) -> std::result::Result<Self::Value, E> |
| 377 | where |
| 378 | E: de::Error, |
| 379 | { |
| 380 | if value.is_finite() && value >= 0.0 && value < (u64::MAX as f64) { |
| 381 | Ok(TryParse::Parsed(value.round() as u64)) |
| 382 | } else { |
| 383 | Err(serde::de::Error::custom("NumericType must be representable as a u64")) |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | match deserializer.deserialize_any(NumericType(PhantomData)) { |
| 389 | Ok(ok) => Ok(ok), |
| 390 | Err(_) => Ok(TryParse::FailedToParse), |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | #[cfg(test)] |
| 395 | mod tests { |
nothing calls this directly
no test coverage detected