From 89cd3a60b826a752c0b2d0b83d2269ba468b5ef2 Mon Sep 17 00:00:00 2001 From: Andrew Weiss Date: Wed, 13 Mar 2019 23:14:56 -0700 Subject: [PATCH] clippy and fmt --- builder_derive/src/lib.rs | 6 ++++++ core/src/configuration.rs | 6 ++++++ core/src/lib.rs | 13 +++++++++---- core/src/main.rs | 5 +++-- core/src/transport.rs | 13 ++++++------- core/src/types.rs | 13 +++++++------ pyagent/src/configuration.rs | 18 ++++++++++-------- pyagent/src/main.rs | 4 ++-- 8 files changed, 49 insertions(+), 29 deletions(-) diff --git a/builder_derive/src/lib.rs b/builder_derive/src/lib.rs index ad9d121..c93c450 100644 --- a/builder_derive/src/lib.rs +++ b/builder_derive/src/lib.rs @@ -50,11 +50,17 @@ pub fn builder(input: TokenStream) -> TokenStream { }) .map(|(n, t, o)| { if let Some(ty) = o { + let maybe_name = Ident::new(&format!("maybe_{}", n.clone().unwrap()), name.span()); quote_spanned! { name.span() => pub fn #n>(mut self, val: T) -> Self { self.node.#n = Some(val.into()); self } + + pub fn #maybe_name(mut self, val: Option<#ty>) -> Self { + self.node.#n = val; + self + } } } else { quote_spanned! { name.span() => diff --git a/core/src/configuration.rs b/core/src/configuration.rs index 9de581e..02c4e2b 100644 --- a/core/src/configuration.rs +++ b/core/src/configuration.rs @@ -5,6 +5,9 @@ use crate::types::Level; pub struct Configuration { pub endpoint: String, pub access_token: Option, + pub environment: Option, + pub host: Option, + pub code_version: Option, pub log_level: Level, pub timeout: u64, } @@ -14,6 +17,9 @@ impl Default for Configuration { Configuration { endpoint: "https://api.rollbar.com/api/1/item/".to_owned(), access_token: None, + environment: None, + host: None, + code_version: None, log_level: Level::Info, timeout: 10, } diff --git a/core/src/lib.rs b/core/src/lib.rs index b10e462..14485d1 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,7 +1,11 @@ -#[macro_use] extern crate log; -#[macro_use] extern crate serde_derive; -#[macro_use] extern crate builder_derive; -#[macro_use] extern crate error_chain; +#[macro_use] +extern crate log; +#[macro_use] +extern crate serde_derive; +#[macro_use] +extern crate builder_derive; +#[macro_use] +extern crate error_chain; mod configuration; mod errors; @@ -14,6 +18,7 @@ pub use log::Level; pub use crate::configuration::Configuration; pub use crate::transport::{HttpTransport, Transport}; +#[derive(Default)] pub struct Uuid(uuid::Uuid); impl Uuid { diff --git a/core/src/main.rs b/core/src/main.rs index 0273ebf..f863faa 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -1,4 +1,5 @@ -#[macro_use] extern crate log; +#[macro_use] +extern crate log; use rollbar_rust::constants; use rollbar_rust::types::*; @@ -17,7 +18,7 @@ fn main() { fn make_configuration() -> Configuration { let mut conf = Configuration::default(); - conf.access_token = Some("a4ced289a17c42928fb4b7fdba5f2ce0".to_owned()); + conf.access_token = Some("POST_SERVER_ITEM_TOKEN".to_owned()); conf } diff --git a/core/src/transport.rs b/core/src/transport.rs index 29e9076..49c58b5 100644 --- a/core/src/transport.rs +++ b/core/src/transport.rs @@ -37,6 +37,7 @@ impl HttpTransport { .timeout(Duration::from_secs(configuration.timeout)) .build() .unwrap(); + #[allow(clippy::mutex_atomic)] let queue_depth = Arc::new(Mutex::new(0)); let endpoint = configuration.endpoint.clone(); let _handle = Some(spawn_sender( @@ -75,13 +76,11 @@ impl Transport for HttpTransport { sender.send(None).ok(); } return true; - } else { - if let Ok(sender) = self.sender.lock() { - match sender.try_send(None) { - Err(TrySendError::Full(_)) => {} - Ok(_) | Err(_) => { - return self.signal.wait_timeout(guard, timeout).is_ok(); - } + } else if let Ok(sender) = self.sender.lock() { + match sender.try_send(None) { + Err(TrySendError::Full(_)) => {} + Ok(_) | Err(_) => { + return self.signal.wait_timeout(guard, timeout).is_ok(); } } } diff --git a/core/src/types.rs b/core/src/types.rs index 68f1f4d..cefba8e 100644 --- a/core/src/types.rs +++ b/core/src/types.rs @@ -278,12 +278,12 @@ impl<'a> From<&'a str> for Level { impl ToString for Level { fn to_string(&self) -> String { - match self { - &Level::Critical => "critical", - &Level::Error => "error", - &Level::Warning => "warning", - &Level::Info => "info", - &Level::Debug => "debug", + match *self { + Level::Critical => "critical", + Level::Error => "error", + Level::Warning => "warning", + Level::Info => "info", + Level::Debug => "debug", } .to_string() } @@ -321,6 +321,7 @@ impl<'de> ::serde::Deserialize<'de> for Level { } } +#[derive(Default)] pub struct BodyBuilder { telemetry: Option>, } diff --git a/pyagent/src/configuration.rs b/pyagent/src/configuration.rs index 6949cba..15e77a9 100644 --- a/pyagent/src/configuration.rs +++ b/pyagent/src/configuration.rs @@ -5,7 +5,7 @@ use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; use std::mem; -const DEFAULT: &'static str = "DEFAULT"; +const DEFAULT: &str = "DEFAULT"; #[derive(Debug)] pub struct Configuration { @@ -140,7 +140,7 @@ impl App { pub fn validate(&self) -> Result<()> { if let Some(ts) = &self.targets { - if ts.len() == 0 { + if ts.is_empty() { bail!(ErrorKind::MissingTargets(self.name.clone())); } } else { @@ -239,14 +239,14 @@ impl Configuration { } } - pub fn to_toml(self) -> Result { + pub fn into_toml(self) -> Result { debug!("Starting conversion to TOML"); let conf = TomlConfiguration::from(self); toml::to_string(&conf).chain_err(|| "bad toml data") } pub fn validate(&self) -> Result<()> { - for (_name, app) in &self.apps { + for app in self.apps.values() { app.validate()?; } Ok(()) @@ -374,19 +374,19 @@ impl ConfigurationBuilder { Ok(()) } - fn convert_to_list(&self, value: &String) -> Vec { + fn convert_to_list(&self, value: &str) -> Vec { let parts: Vec<_> = value.split_whitespace().collect(); parts.iter().map(|s| s.to_string()).collect() } - fn convert_to_bool(&self, value: &String) -> bool { + fn convert_to_bool(&self, value: &str) -> bool { match &value[..] { "false" | "False" | "FALSE" | "no" | "No" | "NO" => false, _ => true, } } - fn convert_to_pair_list(&self, value: &String) -> Result>> { + fn convert_to_pair_list(&self, value: &str) -> Result>> { let mut iter = value.split_whitespace(); let mut result = Vec::new(); loop { @@ -395,7 +395,9 @@ impl ConfigurationBuilder { result.push(vec![regex.to_string(), name.to_string()]); } (None, None) => break, - _ => return Err(ErrorKind::BadInput("log_format.patterns", value.clone()).into()), + _ => { + return Err(ErrorKind::BadInput("log_format.patterns", value.to_owned()).into()); + } } } Ok(result) diff --git a/pyagent/src/main.rs b/pyagent/src/main.rs index fd1b324..329f504 100644 --- a/pyagent/src/main.rs +++ b/pyagent/src/main.rs @@ -5,7 +5,7 @@ extern crate error_chain; use std::fs; -const VERSION: &'static str = "0.4.3"; +const VERSION: &str = "0.4.3"; mod cli; mod configuration; @@ -39,7 +39,7 @@ fn run() -> Result<()> { let config = builder.build(); simple_logger::init_with_level(config.log_level()).chain_err(|| "simple logger failed?")?; config.validate()?; - write_config_to_file("converted.toml", config.to_toml()?) + write_config_to_file("converted.toml", config.into_toml()?) } fn write_config_to_file(filename: &str, config: String) -> Result<()> {