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 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<P: std::convert::AsRef<std::path::Path>>(path: P) -> anyhow:
pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
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<Config> {
.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<Config> {
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)]

View File

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

View File

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

View File

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

View File

@ -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<Client> {
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<IP: IPSource>(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<IP: IPSource>(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();