diff options
author | syn <isaqtm@gmail.com> | 2020-02-14 14:21:17 +0300 |
---|---|---|
committer | syn <isaqtm@gmail.com> | 2020-02-14 14:21:17 +0300 |
commit | 73e7e264cc996c42510d9c8e774dd46cfc160a74 (patch) | |
tree | 9416ff130da63809d0205b599a3e3ba3cf40e569 /src | |
parent | 3da19e8603c00c02ec7e24ea8910a7c386b09018 (diff) | |
download | evr-73e7e264cc996c42510d9c8e774dd46cfc160a74.tar.gz |
deserialize duration from floats
Diffstat (limited to 'src')
-rw-r--r-- | src/backends/clang.rs | 7 | ||||
-rw-r--r-- | src/backends/python.rs | 18 | ||||
-rw-r--r-- | src/conf.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/serde_duration.rs | 28 |
5 files changed, 48 insertions, 10 deletions
diff --git a/src/backends/clang.rs b/src/backends/clang.rs index 11c316a..361c759 100644 --- a/src/backends/clang.rs +++ b/src/backends/clang.rs @@ -1,4 +1,4 @@ -use serde_derive::{ Serialize, Deserialize }; +use serde_derive::Deserialize; use crate::backends::{ Backend, mk_tmp_dir, RunError }; use std::path::{ Path, PathBuf }; use std::io::{ Result as IoResult, Error, ErrorKind }; @@ -7,9 +7,10 @@ use std::collections::hash_map::DefaultHasher; use std::hash::{ Hash, Hasher }; use crate::wait::{ ChildExitStatus, wait_child }; use std::time::Duration; +use crate::serde_duration::deserialize_duration; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Deserialize, Default)] pub struct ClangBackend { template: Option<String>, @@ -19,7 +20,7 @@ pub struct ClangBackend { #[serde(default = "default_cc")] cc: String, - #[serde(default = "default_timeout")] + #[serde(default = "default_timeout", deserialize_with = "deserialize_duration")] timeout: Duration } diff --git a/src/backends/python.rs b/src/backends/python.rs index 3ee7b35..7e0dac1 100644 --- a/src/backends/python.rs +++ b/src/backends/python.rs @@ -1,14 +1,23 @@ -use serde_derive::{ Serialize, Deserialize }; +use serde_derive::Deserialize; use crate::backends::{ Backend, RunError }; use std::process::{ Command }; use std::path::Path; use crate::wait::{ wait_child, ChildExitStatus }; +use crate::serde_duration::deserialize_duration; +use std::time::Duration; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Deserialize, Default)] pub struct PythonBackend { template: Option<String>, version: Option<String>, - timeout: Option<f32>, + + #[serde(default = "default_timeout", deserialize_with = "deserialize_duration")] + timeout: Duration, +} + + +fn default_timeout() -> Duration { + Duration::from_secs(1) } @@ -39,7 +48,6 @@ impl Backend for PythonBackend { .arg(fname.as_os_str()) .spawn()?; - let timeout = std::time::Duration::from_secs_f32(self.timeout.unwrap_or(1.0)); - Ok(wait_child(child, timeout, timer)?) + Ok(wait_child(child, self.timeout, timer)?) } }
\ No newline at end of file diff --git a/src/conf.rs b/src/conf.rs index cc281f0..8aeb8e3 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,4 +1,4 @@ -use serde_derive::{ Serialize, Deserialize }; +use serde_derive::Deserialize; use std::path::{ PathBuf, Path }; use toml::de; use log::{ error }; @@ -9,7 +9,7 @@ use std::io::ErrorKind; use crate::backends::{ Backend, PythonBackend, ClangBackend }; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Deserialize, Default)] pub struct Conf { #[serde(skip)] path: Option<PathBuf>, diff --git a/src/main.rs b/src/main.rs index 4eeb298..1bb683b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::io::prelude::*; mod conf; mod backends; mod wait; +mod serde_duration; fn main() { let matches = App::new("evr") diff --git a/src/serde_duration.rs b/src/serde_duration.rs new file mode 100644 index 0000000..50076b0 --- /dev/null +++ b/src/serde_duration.rs @@ -0,0 +1,28 @@ +use serde::de::{ Visitor, Error, Deserializer }; +use std::time::Duration; +use std::fmt; + +pub fn deserialize_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error> +where + D: Deserializer<'de> +{ + struct DurationVisitor; + + impl<'de> Visitor<'de> for DurationVisitor { + type Value = Duration; + + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "duration in secs") + } + + fn visit_f32<E: Error> (self, v: f32) -> Result<Self::Value, E> { + Ok(Duration::from_secs_f32(v)) + } + + fn visit_f64<E: Error> (self, v: f64) -> Result<Self::Value, E> { + Ok(Duration::from_secs_f64(v)) + } + } + + deserializer.deserialize_any(DurationVisitor) +} |