From 2e2d723b2a4950fc0c9db39aac77cdb0f6679dbd Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sun, 14 Jul 2024 16:58:22 +0200 Subject: [PATCH] generics --- src/cloudflare.rs | 31 ++++++++++++++++++++----------- src/config.rs | 7 ++++--- src/message_handler.rs | 16 ++++++++++++---- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/cloudflare.rs b/src/cloudflare.rs index 093a947..fc31d8f 100644 --- a/src/cloudflare.rs +++ b/src/cloudflare.rs @@ -4,19 +4,12 @@ use reqwest::Client; use serde::{self, Deserialize, Serialize}; use std::{ collections::HashMap, + fmt, net::{IpAddr, Ipv4Addr}, }; use crate::get_current_public_ipv4; -pub struct CloudflareClient { - client: Client, - domains: Vec>, - current_ip: Ipv4Addr, - api_key: Box, - zone_id: Box, -} - #[derive(Serialize, Deserialize, Clone, Debug)] struct DnsRecord { id: String, @@ -42,8 +35,24 @@ struct CloudflareResponse { result: Option>, } -impl CloudflareClient { - pub async fn new(api_key: Box, zone_id: Box, domains: Vec>) -> Result { +pub struct CloudflareClient +where + A: fmt::Display, + Z: fmt::Display, +{ + client: Client, + domains: Vec>, + current_ip: Ipv4Addr, + api_key: A, + zone_id: Z, +} + +impl CloudflareClient +where + Api: fmt::Display, + Zone: fmt::Display, +{ + pub async fn new(api_key: Api, zone_id: Zone, domains: Vec>) -> Result { let force_ipv4 = IpAddr::from([0, 0, 0, 0]); let client = reqwest::ClientBuilder::new() .local_address(force_ipv4) @@ -117,7 +126,7 @@ impl CloudflareClient { Ok(()) } - async fn get_dns_records(&self, domain: &str) -> Result> { + async fn get_dns_records(&self, domain: &D) -> Result> { let url = format!( "https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}", self.zone_id, domain diff --git a/src/config.rs b/src/config.rs index bf4f9df..1d2cf72 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,8 @@ use anyhow; use dirs; use log::warn; use serde::{self, Deserialize, Serialize}; -use std::{env, path::PathBuf}; +use std::env; +use std::path::{Path, PathBuf}; use tokio::{fs, io::AsyncReadExt}; use crate::PROGRAM_NAME; @@ -58,8 +59,8 @@ pub async fn get_config_path() -> Result> { Err(tried_paths) } -pub async fn read_config(path: &PathBuf) -> anyhow::Result { - let mut file = fs::File::open(&path).await?; +pub async fn read_config>(path: &P) -> anyhow::Result { + let mut file = fs::File::open(path).await?; let mut buf = String::new(); file.read_to_string(&mut buf).await?; Ok(toml::from_str(&buf)?) diff --git a/src/message_handler.rs b/src/message_handler.rs index 4f29c35..a2a229e 100644 --- a/src/message_handler.rs +++ b/src/message_handler.rs @@ -6,14 +6,22 @@ use netlink_packet_route::RouteNetlinkMessage as RtnlMessage; use crate::CloudflareClient; -pub struct MessageHandler<'a> { - cloudflare: &'a mut CloudflareClient, +pub struct MessageHandler<'a, Api, Zone> +where + Api: fmt::Display, + Zone: fmt::Display, +{ + cloudflare: &'a mut CloudflareClient, errs_counter: usize, errs_max: usize, } -impl<'a> MessageHandler<'a> { - pub fn new(cloudflare: &'a mut CloudflareClient, errs_max: usize) -> Self { +impl<'a, Api, Zone> MessageHandler<'a, Api, Zone> +where + Api: fmt::Display, + Zone: fmt::Display, +{ + pub fn new(cloudflare: &'a mut CloudflareClient, errs_max: usize) -> Self { Self { cloudflare, errs_counter: 0,