diff --git a/Cargo.lock b/Cargo.lock index 13a435a4..9749049d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -428,7 +428,6 @@ dependencies = [ "env_logger", "failure", "flate2", - "futures", "hmac 0.7.1", "http 0.1.21", "hyper 0.12.35", @@ -438,7 +437,6 @@ dependencies = [ "mime 0.3.16", "minifier", "num_cpus", - "paste", "percent-encoding 2.1.0", "petgraph", "predicates", @@ -466,7 +464,6 @@ dependencies = [ "tar", "tempfile", "tera", - "tokio 0.1.22", "toml 0.4.10", "url 1.7.2", "walkdir", @@ -1981,25 +1978,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "paste" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" -dependencies = [ - "paste-impl", - "proc-macro-hack", -] - -[[package]] -name = "paste-impl" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" -dependencies = [ - "proc-macro-hack", -] - [[package]] name = "percent-encoding" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index fc128937..822657cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,13 +23,11 @@ csv = "1.0.2" dotenv = "0.13" failure = "0.1.3" flate2 = "1" -futures = "0.1.13" http = "0.1.10" hyper = "0.12.8" lazy_static = "1.0" mime = "0.3.1" minifier = { version = "0.0.20", features = ["html"] } -paste = "0.1.3" petgraph = "0.4.11" r2d2 = "0.8.2" r2d2_sqlite = "0.19.0" @@ -49,7 +47,6 @@ structopt-derive = "0.2" tar = "0.4.0" tempfile = "3.0.0" tera = "0.11.7" -tokio = "0.1.11" toml = "0.4.6" url = "1.1" walkdir = "2" diff --git a/src/agent/api.rs b/src/agent/api.rs index 6823a558..c2a35e93 100644 --- a/src/agent/api.rs +++ b/src/agent/api.rs @@ -11,7 +11,6 @@ use reqwest::header::AUTHORIZATION; use reqwest::{Method, StatusCode}; use serde::de::DeserializeOwned; use serde_json::json; -use std::error::Error as _; #[derive(Debug, Fail)] pub enum AgentApiError { @@ -93,17 +92,7 @@ impl AgentApi { let retry = if let Some(AgentApiError::ServerUnavailable) = err.downcast_ref() { true } else if let Some(err) = err.downcast_ref::<::reqwest::Error>() { - let reqwest_io = err - .source() - .map(|inner| inner.is::<::std::io::Error>()) - .unwrap_or(false); - let hyper_io = err - .source() - .and_then(|inner| inner.downcast_ref::<::hyper::Error>()) - .and_then(|inner| inner.source()) - .map(|inner| inner.is::<::std::io::Error>()) - .unwrap_or(false); - reqwest_io || hyper_io + err.is_timeout() || err.is_connect() } else { false }; diff --git a/src/experiments.rs b/src/experiments.rs index 4cc4c50a..4e5e8815 100644 --- a/src/experiments.rs +++ b/src/experiments.rs @@ -44,7 +44,8 @@ string_enum!(pub enum CapLints { const SMALL_RANDOM_COUNT: u32 = 20; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] pub enum CrateSelect { Full, Demo, @@ -55,6 +56,8 @@ pub enum CrateSelect { List(HashSet), } +from_into_string!(CrateSelect); + impl FromStr for CrateSelect { type Err = failure::Error; @@ -167,8 +170,6 @@ impl FromStr for DeferredCrateSelect { } } -impl_serde_from_parse!(CrateSelect, expecting = "A valid value of `CrateSelect`"); - #[cfg_attr(test, derive(Debug, PartialEq, Eq))] #[derive(Clone, Serialize, Deserialize)] pub enum Assignee { diff --git a/src/results/mod.rs b/src/results/mod.rs index 379bde4f..fee630ae 100644 --- a/src/results/mod.rs +++ b/src/results/mod.rs @@ -115,7 +115,8 @@ macro_rules! test_result_enum { with_reason { $($with_reason_name:ident($reason:ident) => $with_reason_repr:expr,)* } without_reason { $($reasonless_name:ident => $reasonless_repr:expr,)* } }) => { - #[derive(Debug, PartialEq, Eq, Hash, Clone)] + #[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)] + #[serde(try_from = "String", into = "String")] pub enum $name { $($with_reason_name($reason),)* $($reasonless_name,)* @@ -301,7 +302,7 @@ test_result_enum!(pub enum TestResult { } }); -impl_serde_from_parse!(TestResult, expecting = "a test result"); +from_into_string!(TestResult); #[cfg(test)] mod tests { diff --git a/src/utils/macros.rs b/src/utils/macros.rs index 5168cc95..39b32806 100644 --- a/src/utils/macros.rs +++ b/src/utils/macros.rs @@ -1,6 +1,7 @@ macro_rules! string_enum { ($vis:vis enum $name:ident { $($item:ident => $str:expr,)* }) => { - #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] + #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)] + #[serde(try_from = "String", into = "String")] $vis enum $name { $($item,)* } @@ -36,44 +37,22 @@ macro_rules! string_enum { } } - impl_serde_from_parse!($name, expecting="foo"); + from_into_string!($name); } } -macro_rules! impl_serde_from_parse { - ($for:ident, expecting=$expecting:expr) => { - paste::item! { - struct [<$for Visitor>]; - - impl<'de> ::serde::de::Visitor<'de> for [<$for Visitor>] { - type Value = $for; - - fn expecting(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { - f.write_str($expecting) - } - - fn visit_str(self, input: &str) -> Result<$for, E> { - use std::str::FromStr; - $for::from_str(input).map_err(E::custom) - } - } - } - - impl<'de> ::serde::de::Deserialize<'de> for $for { - fn deserialize(deserializer: D) -> Result - where - D: ::serde::de::Deserializer<'de>, - { - deserializer.deserialize_str(paste::expr! { [<$for Visitor>] }) +macro_rules! from_into_string { + ($for:ident) => { + impl std::convert::TryFrom for $for { + type Error = <$for as std::str::FromStr>::Err; + fn try_from(s: String) -> Result::Err> { + s.parse() } } - impl ::serde::ser::Serialize for $for { - fn serialize(&self, serializer: S) -> Result - where - S: ::serde::ser::Serializer, - { - serializer.serialize_str(&self.to_string()) + impl From<$for> for String { + fn from(s: $for) -> String { + s.to_string() } } }; diff --git a/src/utils/size.rs b/src/utils/size.rs index ee3da285..49c8ff78 100644 --- a/src/utils/size.rs +++ b/src/utils/size.rs @@ -2,7 +2,8 @@ use crate::prelude::*; use std::fmt; use std::str::FromStr; -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] pub enum Size { Bytes(usize), Kilobytes(usize), @@ -11,6 +12,8 @@ pub enum Size { Terabytes(usize), } +from_into_string!(Size); + impl Size { pub(crate) fn to_bytes(&self) -> usize { match self { @@ -61,8 +64,6 @@ impl FromStr for Size { } } -impl_serde_from_parse!(Size, expecting = "a size"); - #[cfg(test)] mod tests { use super::Size;