Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from jokeyrhyme/convert_max_duration_ms
Browse files Browse the repository at this point in the history
fix: convert raw number as Duration milliseconds
  • Loading branch information
jokeyrhyme authored Oct 13, 2023
2 parents 4b02a9b + 4849b45 commit 206ee7c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/deserialize.rs
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));
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(clippy::all, clippy::pedantic, unsafe_code)]

mod backend;
mod deserialize;
mod error;
mod nu;
use backend::Backend;
Expand Down
1 change: 1 addition & 0 deletions src/nu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub(crate) struct IdeSettings {
pub hints: IdeSettingsHints,
pub include_dirs: Vec<PathBuf>,
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,
}
Expand Down

0 comments on commit 206ee7c

Please sign in to comment.