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
This commit is contained in:
jannikac 2022-12-18 06:53:48 +01:00 committed by GitHub
parent bbd7ce347a
commit 041c1109e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 24 deletions

View File

@ -1,5 +1,4 @@
use crate::opts; use crate::opts;
use anyhow;
use directories::ProjectDirs; use directories::ProjectDirs;
use serde::Deserialize; use serde::Deserialize;
use std::fs; use std::fs;
@ -19,10 +18,10 @@ pub struct Entry {
} }
fn default_ttl() -> u32 { fn default_ttl() -> u32 {
return 300; 300
} }
#[derive(Deserialize, PartialEq, Debug)] #[derive(Deserialize, Debug, PartialEq, Eq)]
pub enum IPSourceName { pub enum IPSourceName {
Ipify, Ipify,
Icanhazip, Icanhazip,
@ -47,7 +46,7 @@ pub struct Config {
pub ttl: u32, pub ttl: u32,
} }
const DEFAULT_TYPES: &'static [&'static str] = &["A"]; const DEFAULT_TYPES: &[&str] = &["A"];
impl Config { impl Config {
pub fn fqdn<'c>(entry: &'c Entry, config: &'c Config) -> &'c str { pub fn fqdn<'c>(entry: &'c Entry, config: &'c Config) -> &'c str {
@ -58,7 +57,7 @@ impl Config {
entry.ttl.unwrap_or(config.ttl) 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() entry.types.iter().map(|t| t.as_str()).collect()
} }
} }
@ -70,11 +69,11 @@ fn load_config_from<P: std::convert::AsRef<std::path::Path>>(path: P) -> anyhow:
pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> { pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
let mut config = match &opts.config { let mut config = match &opts.config {
Some(config_path) => load_config_from(&config_path), Some(config_path) => load_config_from(config_path),
None => { None => {
let confpath = ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns") let confpath = ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns")
.and_then(|dir| Some(PathBuf::from(dir.config_dir()).join("config.toml"))) .map(|dir| PathBuf::from(dir.config_dir()).join("config.toml"))
.ok_or(anyhow::anyhow!("Can't find config directory")); .ok_or_else(|| anyhow::anyhow!("Can't find config directory"));
confpath confpath
.and_then(|path| { .and_then(|path| {
println!("Checking for config: {}", path.to_string_lossy()); println!("Checking for config: {}", path.to_string_lossy());
@ -93,11 +92,9 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
.entry .entry
.into_iter() .into_iter()
.map(|mut entry| { .map(|mut entry| {
entry.types = entry entry
.types .types
.into_iter() .retain(|v| (v == "A" && !opts.skip_ipv4) || (v == "AAAA" && !opts.skip_ipv6));
.filter(|v| (v == "A" && !opts.skip_ipv4) || (v == "AAAA" && !opts.skip_ipv6))
.collect();
entry entry
}) })
.collect(); .collect();
@ -107,13 +104,13 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
pub fn validate_config(config: &Config) -> anyhow::Result<()> { pub fn validate_config(config: &Config) -> anyhow::Result<()> {
for entry in &config.entry { 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" { if entry_type != "A" && entry_type != "AAAA" {
anyhow::bail!("Entry {} has invalid type {}", entry.name, entry_type); anyhow::bail!("Entry {} has invalid type {}", entry.name, entry_type);
} }
} }
} }
return Ok(()); Ok(())
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,3 @@
use anyhow;
use async_trait::async_trait; use async_trait::async_trait;
use super::ip_source::IPSource; use super::ip_source::IPSource;

View File

@ -1,4 +1,3 @@
use anyhow;
use async_trait::async_trait; use async_trait::async_trait;
#[async_trait] #[async_trait]

View File

@ -1,4 +1,3 @@
use anyhow;
use async_trait::async_trait; use async_trait::async_trait;
use super::ip_source::IPSource; use super::ip_source::IPSource;

View File

@ -1,10 +1,8 @@
use crate::config::Config; use crate::config::Config;
use crate::gandi::GandiAPI; use crate::gandi::GandiAPI;
use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify}; use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify};
use anyhow;
use clap::Parser; use clap::Parser;
use config::IPSourceName; use config::IPSourceName;
use futures;
use ip_source::icanhazip::IPSourceIcanhazip; use ip_source::icanhazip::IPSourceIcanhazip;
use reqwest::{header, Client, ClientBuilder, StatusCode}; use reqwest::{header, Client, ClientBuilder, StatusCode};
use serde::Serialize; use serde::Serialize;
@ -16,7 +14,6 @@ mod gandi;
mod ip_source; mod ip_source;
mod opts; mod opts;
use die_exit_2::*; use die_exit_2::*;
use governor;
/// 30 requests per minute, see https://api.gandi.net/docs/reference/ /// 30 requests per minute, see https://api.gandi.net/docs/reference/
const GANDI_RATE_LIMIT: u32 = 30; const GANDI_RATE_LIMIT: u32 = 30;
@ -34,7 +31,7 @@ fn api_client(api_key: &str) -> anyhow::Result<Client> {
let accept_value = header::HeaderValue::from_static("application/json"); let accept_value = header::HeaderValue::from_static("application/json");
headers.insert(header::ACCEPT, accept_value); headers.insert(header::ACCEPT, accept_value);
let client = client_builder.default_headers(headers).build()?; let client = client_builder.default_headers(headers).build()?;
return Ok(client); Ok(client)
} }
#[derive(Serialize)] #[derive(Serialize)]
@ -72,11 +69,11 @@ async fn run<IP: IPSource>(base_url: &str, conf: &Config) -> anyhow::Result<()>
for entry in &conf.entry { for entry in &conf.entry {
for entry_type in Config::types(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 { let url = GandiAPI {
fqdn: &fqdn, fqdn: &fqdn,
rrset_name: &entry.name, rrset_name: &entry.name,
rrset_type: &entry_type, rrset_type: entry_type,
base_url, base_url,
} }
.url(); .url();
@ -87,7 +84,7 @@ async fn run<IP: IPSource>(base_url: &str, conf: &Config) -> anyhow::Result<()>
}; };
let payload = APIPayload { let payload = APIPayload {
rrset_values: vec![ip.to_string()], 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 req = client.put(url).json(&payload);
let task_governor = governor.clone(); let task_governor = governor.clone();