From e442b05740651836d1f250f1471518800e1a9e6e Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 12 Dec 2022 01:29:25 +0100 Subject: [PATCH] Correct syntax formatting --- src/config.rs | 18 +++++--- src/error.rs | 4 +- src/main.rs | 115 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 89 insertions(+), 48 deletions(-) diff --git a/src/config.rs b/src/config.rs index fb6b7cf..d8a13d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ -use ron::{ de::from_reader, ser::to_writer }; -use serde::{ Deserialize, Serialize }; -use std::{ fs::File, time }; +use std::{fs::File, time}; + +use ron::{de::from_reader, ser::to_writer}; +use serde::{Deserialize, Serialize}; use crate::error::Result; @@ -9,12 +10,17 @@ pub struct Config { pub delay_between_check: time::Duration, pub api_key: String, pub domains: Vec<(String, String)>, // Hostname, domain. - pub ttl: i32 + pub ttl: i32, } impl Config { pub fn default() -> Self { - Config { delay_between_check: time::Duration::from_secs(120), api_key: String::from(""), domains: Vec::new(), ttl: 300 } + Config { + delay_between_check: time::Duration::from_secs(120), + api_key: String::from(""), + domains: Vec::new(), + ttl: 300, + } } pub fn read(file_path: &str) -> Result { @@ -29,4 +35,4 @@ impl Config { } } } -} \ No newline at end of file +} diff --git a/src/error.rs b/src/error.rs index 12221fb..e047f56 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,7 @@ pub type Result = std::result::Result>; #[derive(Debug)] pub struct Error { - pub message: String + pub message: String, } impl std::fmt::Display for Error { @@ -12,4 +12,4 @@ impl std::fmt::Display for Error { } } -impl std::error::Error for Error { } \ No newline at end of file +impl std::error::Error for Error {} diff --git a/src/main.rs b/src/main.rs index 4b0d196..4fdc273 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,14 +9,20 @@ #![cfg_attr(debug_assertions, allow(unused_variables, unused_imports, dead_code))] -use std::{ net::{ IpAddr, Ipv4Addr }, thread, time }; -use serde_json::{ Value, json }; +use std::{ + net::{IpAddr, Ipv4Addr}, + thread, time, +}; -mod error; -mod config; +use serde_json::{json, Value}; + +use crate::{ + config::Config, + error::{Error, Result}, +}; -use crate::error::{ Result, Error }; -use crate::config::Config; +mod config; +mod error; const FILE_CONF: &str = "config.ron"; @@ -25,7 +31,13 @@ fn main() -> Result<()> { let config = Config::read(FILE_CONF)?; - println!("Configuration: {:?}", Config { api_key: String::from("*****"), ..config.clone() }); + println!( + "Configuration: {:?}", + Config { + api_key: String::from("*****"), + ..config.clone() + } + ); loop { let time_beginning_loop = time::Instant::now(); @@ -50,7 +62,10 @@ fn check_and_update_dns(api_key: &str, domains: &Vec<(String, String)>, ttl: i32 let current_ip = get_current_record_ip(api_key, hostname, domain)?; if real_ip != current_ip { - println!("IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...", domain, real_ip, current_ip); + println!( + "IP addresses don't match for domain {}: real = {}, dns = {}. Renewing DNS...", + domain, real_ip, current_ip + ); update_record_ip(api_key, hostname, domain, real_ip, ttl)?; println!("Renewing of {}.{} successfully", hostname, domain); } @@ -64,44 +79,58 @@ fn get_real_ip() -> Result { let client = reqwest::blocking::Client::new(); match client.get(url).send() { - Ok(resp) => + Ok(resp) => { if resp.status().is_success() { let content = resp.text().unwrap(); match content.parse() { Ok(IpAddr::V4(ip_v4)) => Ok(ip_v4), - _ => Err(Box::new(Error { message: String::from("Can't parse IPv4 from ipify") })) + _ => Err(Box::new(Error { + message: String::from("Can't parse IPv4 from ipify"), + })), } } else { - Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", url, resp) })) - }, - - Err(error) => { - Err(Box::new(Error { message: format!("Error during request to {}: {:?}", url, error) })) + Err(Box::new(Error { + message: format!("Request unsuccessful to {}: {:#?}", url, resp), + })) + } } + Err(error) => Err(Box::new(Error { + message: format!("Error during request to {}: {:?}", url, error), + })), } } fn get_current_record_ip(api_key: &str, name: &str, fqdn: &str) -> Result { - let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Get)?; + let json_value = request_livedns_gandi( + api_key, + &format!("domains/{}/records/{}/A", fqdn, name), + Method::Get, + )?; match &json_value["rrset_values"][0] { - Value::String(ip_str) => - Ok(ip_str.parse()?), - _ => - Result::Err(Box::new(Error { message: format!("Unable to extract the IP from the JSON answer: {}", json_value) })) + Value::String(ip_str) => Ok(ip_str.parse()?), + _ => Result::Err(Box::new(Error { + message: format!( + "Unable to extract the IP from the JSON answer: {}", + json_value + ), + })), } } fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i32) -> Result<()> { - let json_body = - json!( - { - "rrset_values": [ format!("{}", ip) ], - "rrset_ttl": ttl - } - ); + let json_body = json!( + { + "rrset_values": [ format!("{}", ip) ], + "rrset_ttl": ttl + } + ); - let json_value = request_livedns_gandi(api_key, &format!("domains/{}/records/{}/A", fqdn, name), Method::Put(json_body.to_string()))?; + let json_value = request_livedns_gandi( + api_key, + &format!("domains/{}/records/{}/A", fqdn, name), + Method::Put(json_body.to_string()), + )?; println!("Update response: {}", json_value); @@ -110,28 +139,34 @@ fn update_record_ip(api_key: &str, name: &str, fqdn: &str, ip: Ipv4Addr, ttl: i3 enum Method { Put(String), - Get + Get, } fn request_livedns_gandi(api_key: &str, url_fragment: &str, method: Method) -> Result { let url = format!("https://api.gandi.net/v5/livedns/{}", url_fragment); let client = reqwest::blocking::Client::new(); - let request_builder = - match method { - Method::Put(body) => client.put(&url).body(body), - Method::Get => client.get(&url) - }; + let request_builder = match method { + Method::Put(body) => client.put(&url).body(body), + Method::Get => client.get(&url), + }; - match request_builder.header("Authorization", format!("Apikey {}", api_key)).send() { - Ok(resp) => + match request_builder + .header("Authorization", format!("Apikey {}", api_key)) + .send() + { + Ok(resp) => { if resp.status().is_success() { let content = resp.text().unwrap(); Ok(serde_json::from_str(&content).unwrap()) } else { - Err(Box::new(Error { message: format!("Request unsuccessful to {}: {:#?}", &url, resp) })) - }, - Err(error) => - Err(Box::new(Error { message: format!("Error during request to {}: {:?}", &url, error) })) + Err(Box::new(Error { + message: format!("Request unsuccessful to {}: {:#?}", &url, resp), + })) + } + } + Err(error) => Err(Box::new(Error { + message: format!("Error during request to {}: {:?}", &url, error), + })), } } -- 2.45.2