diff --git a/src/deserialize.rs b/src/deserialize.rs new file mode 100644 index 0000000..952651d --- /dev/null +++ b/src/deserialize.rs @@ -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 +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)); + } +} diff --git a/src/main.rs b/src/main.rs index 08f1d60..d1e01e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![deny(clippy::all, clippy::pedantic, unsafe_code)] mod backend; +mod deserialize; mod error; mod nu; use backend::Backend; diff --git a/src/nu.rs b/src/nu.rs index ab703ac..c291118 100644 --- a/src/nu.rs +++ b/src/nu.rs @@ -178,6 +178,7 @@ pub(crate) struct IdeSettings { pub hints: IdeSettingsHints, pub include_dirs: Vec, pub max_number_of_problems: u32, + #[serde(deserialize_with = "crate::deserialize::into_duration_ms")] pub max_nushell_invocation_time: Duration, pub nushell_executable_path: PathBuf, }