generics
This commit is contained in:
parent
4f3e689218
commit
2e2d723b2a
@ -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<Box<str>>,
|
||||
current_ip: Ipv4Addr,
|
||||
api_key: Box<str>,
|
||||
zone_id: Box<str>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
struct DnsRecord {
|
||||
id: String,
|
||||
@ -42,8 +35,24 @@ struct CloudflareResponse {
|
||||
result: Option<Vec<DnsRecord>>,
|
||||
}
|
||||
|
||||
impl CloudflareClient {
|
||||
pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> {
|
||||
pub struct CloudflareClient<A, Z>
|
||||
where
|
||||
A: fmt::Display,
|
||||
Z: fmt::Display,
|
||||
{
|
||||
client: Client,
|
||||
domains: Vec<Box<str>>,
|
||||
current_ip: Ipv4Addr,
|
||||
api_key: A,
|
||||
zone_id: Z,
|
||||
}
|
||||
|
||||
impl<Api, Zone> CloudflareClient<Api, Zone>
|
||||
where
|
||||
Api: fmt::Display,
|
||||
Zone: fmt::Display,
|
||||
{
|
||||
pub async fn new(api_key: Api, zone_id: Zone, domains: Vec<Box<str>>) -> Result<Self> {
|
||||
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<Vec<DnsRecord>> {
|
||||
async fn get_dns_records<D: fmt::Display>(&self, domain: &D) -> Result<Vec<DnsRecord>> {
|
||||
let url = format!(
|
||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}",
|
||||
self.zone_id, domain
|
||||
|
@ -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<PathBuf, Vec<PathBuf>> {
|
||||
Err(tried_paths)
|
||||
}
|
||||
|
||||
pub async fn read_config(path: &PathBuf) -> anyhow::Result<Config> {
|
||||
let mut file = fs::File::open(&path).await?;
|
||||
pub async fn read_config<P: AsRef<Path>>(path: &P) -> anyhow::Result<Config> {
|
||||
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)?)
|
||||
|
@ -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<Api, Zone>,
|
||||
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<Api, Zone>, errs_max: usize) -> Self {
|
||||
Self {
|
||||
cloudflare,
|
||||
errs_counter: 0,
|
||||
|
Loading…
x
Reference in New Issue
Block a user