From 041c1109e037d034508d86baf907bcc4fd01f964 Mon Sep 17 00:00:00 2001 From: jannikac Date: Sun, 18 Dec 2022 06:53:48 +0100 Subject: [PATCH] Fix linter suggestions (#68) * removed unneeded lifetimes * removed unneccessary imports * removed unneccessary return statements * added derive eq -> linter suggestion * removed unneccessary references * made code more concise * ok_or_else instead of ok_or improves performance -> linter suggestion --- src/config.rs | 25 +++++++++++-------------- src/ip_source/icanhazip.rs | 1 - src/ip_source/ip_source.rs | 1 - src/ip_source/ipify.rs | 1 - src/main.rs | 11 ++++------- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index c5694f4..e5c26bf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,4 @@ use crate::opts; -use anyhow; use directories::ProjectDirs; use serde::Deserialize; use std::fs; @@ -19,10 +18,10 @@ pub struct Entry { } fn default_ttl() -> u32 { - return 300; + 300 } -#[derive(Deserialize, PartialEq, Debug)] +#[derive(Deserialize, Debug, PartialEq, Eq)] pub enum IPSourceName { Ipify, Icanhazip, @@ -47,7 +46,7 @@ pub struct Config { pub ttl: u32, } -const DEFAULT_TYPES: &'static [&'static str] = &["A"]; +const DEFAULT_TYPES: &[&str] = &["A"]; impl Config { pub fn fqdn<'c>(entry: &'c Entry, config: &'c Config) -> &'c str { @@ -58,7 +57,7 @@ impl Config { entry.ttl.unwrap_or(config.ttl) } - pub fn types<'e>(entry: &'e Entry) -> Vec<&'e str> { + pub fn types(entry: &Entry) -> Vec<&str> { entry.types.iter().map(|t| t.as_str()).collect() } } @@ -70,11 +69,11 @@ fn load_config_from>(path: P) -> anyhow: pub fn load_config(opts: &opts::Opts) -> anyhow::Result { let mut config = match &opts.config { - Some(config_path) => load_config_from(&config_path), + Some(config_path) => load_config_from(config_path), None => { let confpath = ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns") - .and_then(|dir| Some(PathBuf::from(dir.config_dir()).join("config.toml"))) - .ok_or(anyhow::anyhow!("Can't find config directory")); + .map(|dir| PathBuf::from(dir.config_dir()).join("config.toml")) + .ok_or_else(|| anyhow::anyhow!("Can't find config directory")); confpath .and_then(|path| { println!("Checking for config: {}", path.to_string_lossy()); @@ -93,11 +92,9 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result { .entry .into_iter() .map(|mut entry| { - entry.types = entry + entry .types - .into_iter() - .filter(|v| (v == "A" && !opts.skip_ipv4) || (v == "AAAA" && !opts.skip_ipv6)) - .collect(); + .retain(|v| (v == "A" && !opts.skip_ipv4) || (v == "AAAA" && !opts.skip_ipv6)); entry }) .collect(); @@ -107,13 +104,13 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result { pub fn validate_config(config: &Config) -> anyhow::Result<()> { for entry in &config.entry { - for entry_type in Config::types(&entry) { + for entry_type in Config::types(entry) { if entry_type != "A" && entry_type != "AAAA" { anyhow::bail!("Entry {} has invalid type {}", entry.name, entry_type); } } } - return Ok(()); + Ok(()) } #[cfg(test)] diff --git a/src/ip_source/icanhazip.rs b/src/ip_source/icanhazip.rs index 7f28a2b..6957b41 100644 --- a/src/ip_source/icanhazip.rs +++ b/src/ip_source/icanhazip.rs @@ -1,4 +1,3 @@ -use anyhow; use async_trait::async_trait; use super::ip_source::IPSource; diff --git a/src/ip_source/ip_source.rs b/src/ip_source/ip_source.rs index 1117626..13185b4 100644 --- a/src/ip_source/ip_source.rs +++ b/src/ip_source/ip_source.rs @@ -1,4 +1,3 @@ -use anyhow; use async_trait::async_trait; #[async_trait] diff --git a/src/ip_source/ipify.rs b/src/ip_source/ipify.rs index 304fa2c..14cba47 100644 --- a/src/ip_source/ipify.rs +++ b/src/ip_source/ipify.rs @@ -1,4 +1,3 @@ -use anyhow; use async_trait::async_trait; use super::ip_source::IPSource; diff --git a/src/main.rs b/src/main.rs index 662931b..4bdb20c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,8 @@ use crate::config::Config; use crate::gandi::GandiAPI; use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify}; -use anyhow; use clap::Parser; use config::IPSourceName; -use futures; use ip_source::icanhazip::IPSourceIcanhazip; use reqwest::{header, Client, ClientBuilder, StatusCode}; use serde::Serialize; @@ -16,7 +14,6 @@ mod gandi; mod ip_source; mod opts; use die_exit_2::*; -use governor; /// 30 requests per minute, see https://api.gandi.net/docs/reference/ const GANDI_RATE_LIMIT: u32 = 30; @@ -34,7 +31,7 @@ fn api_client(api_key: &str) -> anyhow::Result { let accept_value = header::HeaderValue::from_static("application/json"); headers.insert(header::ACCEPT, accept_value); let client = client_builder.default_headers(headers).build()?; - return Ok(client); + Ok(client) } #[derive(Serialize)] @@ -72,11 +69,11 @@ async fn run(base_url: &str, conf: &Config) -> anyhow::Result<()> for entry in &conf.entry { for entry_type in Config::types(entry) { - let fqdn = Config::fqdn(&entry, &conf).to_string(); + let fqdn = Config::fqdn(entry, &conf).to_string(); let url = GandiAPI { fqdn: &fqdn, rrset_name: &entry.name, - rrset_type: &entry_type, + rrset_type: entry_type, base_url, } .url(); @@ -87,7 +84,7 @@ async fn run(base_url: &str, conf: &Config) -> anyhow::Result<()> }; let payload = APIPayload { rrset_values: vec![ip.to_string()], - rrset_ttl: Config::ttl(&entry, &conf), + rrset_ttl: Config::ttl(entry, &conf), }; let req = client.put(url).json(&payload); let task_governor = governor.clone();