This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert raw number as Duration milliseconds
- Loading branch information
1 parent
4b02a9b
commit 4849b45
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::time::Duration; | ||
|
||
use serde::{de::Error, Deserialize, Deserializer}; | ||
|
||
pub(crate) fn into_duration_ms<'de, D>(deserializer: D) -> std::result::Result<Duration, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let value = serde_json::Number::deserialize(deserializer)?; | ||
match value.as_u64() { | ||
Some(i) => Ok(Duration::from_millis(i)), | ||
None => Err(Error::custom("cannot convert value to u64")), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn into_duration_ms_ok() { | ||
let input = json!(123); | ||
|
||
let got = into_duration_ms(input).expect("value should be deserialized"); | ||
|
||
assert_eq!(got, Duration::from_millis(123)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters