From 50a90e68171999cd03dd32fac66fe768cb929c8d Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Sat, 3 Feb 2024 10:04:06 -0800 Subject: [PATCH] run cargo fmt on project --- src/radar/bgp/prefix_origins.rs | 37 +++++++++++++++++++++---------- src/radar/bgp/routing_stats.rs | 39 ++++++++++++++++++++++----------- src/radar/client.rs | 26 ++++++++++++++++------ src/radar/entities/mod.rs | 1 + src/radar/error.rs | 2 +- src/radar/mod.rs | 8 +++---- 6 files changed, 76 insertions(+), 37 deletions(-) diff --git a/src/radar/bgp/prefix_origins.rs b/src/radar/bgp/prefix_origins.rs index 738b7da..127648e 100644 --- a/src/radar/bgp/prefix_origins.rs +++ b/src/radar/bgp/prefix_origins.rs @@ -1,7 +1,7 @@ -use serde::{Deserialize, Serialize}; use crate::radar::bgp::BgpRoutesMeta; use crate::radar::client::RadarClient; use crate::radar::error::RadarError; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct PrefixOriginsEntry { @@ -23,11 +23,17 @@ struct PrefixOriginsResponse { pub success: bool, } -impl RadarClient{ - - pub fn get_bgp_prefix_origins(&self, origin: Option, prefix: Option, rpki_status: Option) -> Result{ +impl RadarClient { + pub fn get_bgp_prefix_origins( + &self, + origin: Option, + prefix: Option, + rpki_status: Option, + ) -> Result { if origin.is_none() && prefix.is_none() { - return Err(RadarError::InvalidParamsError("prefix_origins: origin or prefix must be specified".to_string())); + return Err(RadarError::InvalidParamsError( + "prefix_origins: origin or prefix must be specified".to_string(), + )); } let mut route = "radar/bgp/routes/pfx2as".to_string(); @@ -49,11 +55,12 @@ impl RadarClient{ let response = self.send_request(route.as_str())?; if !response.status().is_success() { - return Err(RadarError::RequestError(response.error_for_status().unwrap_err())); + return Err(RadarError::RequestError( + response.error_for_status().unwrap_err(), + )); } Ok(response.json::()?.result) } - } #[cfg(test)] @@ -65,12 +72,18 @@ mod tests { let client = RadarClient::new().unwrap(); assert!(client.get_bgp_prefix_origins(None, None, None).is_err()); - let res = client.get_bgp_prefix_origins(Some(13335), None, None).unwrap(); - assert!(res.prefix_origins.len()>1); - let res = client.get_bgp_prefix_origins(None, Some("1.1.1.0/24".to_string()), None).unwrap(); + let res = client + .get_bgp_prefix_origins(Some(13335), None, None) + .unwrap(); + assert!(res.prefix_origins.len() > 1); + let res = client + .get_bgp_prefix_origins(None, Some("1.1.1.0/24".to_string()), None) + .unwrap(); assert_eq!(res.prefix_origins.len(), 1); // non-existing prefix - let res = client.get_bgp_prefix_origins(None, Some("1.1.1.1/25".to_string()), None).unwrap(); + let res = client + .get_bgp_prefix_origins(None, Some("1.1.1.1/25".to_string()), None) + .unwrap(); assert_eq!(res.prefix_origins.len(), 0); } -} \ No newline at end of file +} diff --git a/src/radar/bgp/routing_stats.rs b/src/radar/bgp/routing_stats.rs index d7224d6..9b8bb42 100644 --- a/src/radar/bgp/routing_stats.rs +++ b/src/radar/bgp/routing_stats.rs @@ -1,7 +1,7 @@ -use serde::{Deserialize, Serialize}; use crate::radar::bgp::BgpRoutesMeta; use crate::radar::client::RadarClient; use crate::radar::error::RadarError; +use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RoutingStatsEntry { @@ -37,11 +37,16 @@ struct RoutingStatsResponse { pub success: bool, } -impl RadarClient{ - - pub fn get_bgp_routing_stats(&self, asn: Option, country_code: Option) -> Result{ +impl RadarClient { + pub fn get_bgp_routing_stats( + &self, + asn: Option, + country_code: Option, + ) -> Result { if asn.is_some() && country_code.is_some() { - return Err(RadarError::InvalidParamsError("bgp_routing_stats: only one of asn or country code can be specified".to_string())); + return Err(RadarError::InvalidParamsError( + "bgp_routing_stats: only one of asn or country code can be specified".to_string(), + )); } let mut route = "radar/bgp/routes/stats".to_string(); @@ -52,7 +57,9 @@ impl RadarClient{ } if let Some(code) = country_code { if code.len() != 2 { - return Err(RadarError::InvalidParamsError("bgp_routing_stats: country code must be 2 characters".to_string())); + return Err(RadarError::InvalidParamsError( + "bgp_routing_stats: country code must be 2 characters".to_string(), + )); } params.push(format!("location={}", code)); } @@ -63,7 +70,9 @@ impl RadarClient{ let response = self.send_request(route.as_str())?; if !response.status().is_success() { - return Err(RadarError::RequestError(response.error_for_status().unwrap_err())); + return Err(RadarError::RequestError( + response.error_for_status().unwrap_err(), + )); } Ok(response.json::()?.result) } @@ -80,25 +89,29 @@ mod tests { // global routing table stats let res = client.get_bgp_routing_stats(None, None); assert!(res.is_ok()); - assert!(res.unwrap().stats.routes_total>1_000_000); + assert!(res.unwrap().stats.routes_total > 1_000_000); // per_asn routing table stats let res = client.get_bgp_routing_stats(Some(13335), None); assert!(res.is_ok()); - assert!(res.unwrap().stats.routes_total>1_000); + assert!(res.unwrap().stats.routes_total > 1_000); // per_asn routing table stats let res = client.get_bgp_routing_stats(None, Some("US".to_string())); assert!(res.is_ok()); - assert!(res.unwrap().stats.routes_total>1_000); + assert!(res.unwrap().stats.routes_total > 1_000); // per_asn routing table stats let res = client.get_bgp_routing_stats(None, Some("us".to_string())); assert!(res.is_ok()); - assert!(res.unwrap().stats.routes_total>1_000); + assert!(res.unwrap().stats.routes_total > 1_000); // error cases - assert!(client.get_bgp_routing_stats(None, Some("united stats".to_string())).is_err()); - assert!(client.get_bgp_routing_stats(Some(13335), Some("US".to_string())).is_err()); + assert!(client + .get_bgp_routing_stats(None, Some("united stats".to_string())) + .is_err()); + assert!(client + .get_bgp_routing_stats(Some(13335), Some("US".to_string())) + .is_err()); } } diff --git a/src/radar/client.rs b/src/radar/client.rs index fab75bc..38a5d36 100644 --- a/src/radar/client.rs +++ b/src/radar/client.rs @@ -1,6 +1,6 @@ -use reqwest::{header}; -use reqwest::blocking::{Client, Response}; use crate::radar::error::RadarError; +use reqwest::blocking::{Client, Response}; +use reqwest::header; pub const CF_API_BASE_URL: &str = "https://api.cloudflare.com/client/v4"; @@ -15,13 +15,23 @@ impl RadarClient { let api_token = match std::env::var("CF_API_TOKEN") { Ok(token) => token, - Err(_) => return Err(RadarError::TokenError("missing environment variable CF_API_TOKEN".to_string())), + Err(_) => { + return Err(RadarError::TokenError( + "missing environment variable CF_API_TOKEN".to_string(), + )) + } }; // build reqwest client with default authorization token let mut headers = header::HeaderMap::new(); - headers.insert("Authorization", header::HeaderValue::from_str(format!("Bearer {}", api_token).as_str()).unwrap()); - headers.insert("Content-Type", header::HeaderValue::from_static("application/json")); + headers.insert( + "Authorization", + header::HeaderValue::from_str(format!("Bearer {}", api_token).as_str()).unwrap(), + ); + headers.insert( + "Content-Type", + header::HeaderValue::from_static("application/json"), + ); let client = reqwest::blocking::ClientBuilder::new() .user_agent("radar-rs/0.1") .default_headers(headers) @@ -33,7 +43,9 @@ impl RadarClient { } fn verify_token(client: &Client) -> Result<(), RadarError> { - let response = client.get(format!("{}/user/tokens/verify", CF_API_BASE_URL)).send()?; + let response = client + .get(format!("{}/user/tokens/verify", CF_API_BASE_URL)) + .send()?; if !response.status().is_success() { return Err(RadarError::TokenError("invalid api token".to_string())); } @@ -55,4 +67,4 @@ mod tests { // NOTE: need to set a valid CF_API_TOKEN in .env file RadarClient::new().unwrap(); } -} \ No newline at end of file +} diff --git a/src/radar/entities/mod.rs b/src/radar/entities/mod.rs index e69de29..8b13789 100644 --- a/src/radar/entities/mod.rs +++ b/src/radar/entities/mod.rs @@ -0,0 +1 @@ + diff --git a/src/radar/error.rs b/src/radar/error.rs index 71c9b2f..f3fa32a 100644 --- a/src/radar/error.rs +++ b/src/radar/error.rs @@ -10,4 +10,4 @@ pub enum RadarError { #[error("network request error: {0}")] RequestError(#[from] reqwest::Error), -} \ No newline at end of file +} diff --git a/src/radar/mod.rs b/src/radar/mod.rs index a6eb9c7..0b5b38d 100644 --- a/src/radar/mod.rs +++ b/src/radar/mod.rs @@ -1,8 +1,8 @@ -mod entities; mod bgp; -mod error; mod client; +mod entities; +mod error; -pub use error::RadarError; +pub use bgp::*; pub use client::RadarClient; -pub use bgp::*; \ No newline at end of file +pub use error::RadarError;