MCPcopy Create free account
hub / github.com/Keats/jsonwebtoken / numeric_type

Function numeric_type

src/validation.rs:346–382  ·  view source on GitHub ↗
(deserializer: D)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

NumericTypeClass · 0.85

Tested by

no test coverage detected