From b0dea405eebc659b85d148a76b98a130770361b9 Mon Sep 17 00:00:00 2001 From: NetworkCats Date: Tue, 3 Mar 2026 00:07:18 +0800 Subject: [PATCH] style: apply rustfmt formatting to source and test files --- src/db.rs | 2 +- src/error.rs | 6 +++- src/routes.rs | 2 +- src/updater.rs | 10 ++++-- tests/integration.rs | 75 +++++++++++++++++--------------------------- 5 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/db.rs b/src/db.rs index 7e62586..75073a3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -7,7 +7,7 @@ use maxminddb::Reader; use serde::Deserialize; use crate::models::{ - from_mmdb_record, IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo, + IpInfo, MmdbAsn, MmdbCity, MmdbCountry, MmdbProxy, MmdbRecord, ProxyInfo, from_mmdb_record, }; pub type DbReader = Reader>; diff --git a/src/error.rs b/src/error.rs index 1349c09..b8b88b9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -25,7 +25,11 @@ mod tests { use super::*; use http_body_util::BodyExt; - async fn check_error_response(error: AppError, expected_status: StatusCode, expected_body: &str) { + async fn check_error_response( + error: AppError, + expected_status: StatusCode, + expected_body: &str, + ) { let response = error.into_response(); assert_eq!(response.status(), expected_status); let body = response.into_body().collect().await.unwrap().to_bytes(); diff --git a/src/routes.rs b/src/routes.rs index c45aea3..26bcd98 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,5 +1,5 @@ -use axum::routing::get; use axum::Router; +use axum::routing::get; use tower_http::services::ServeDir; use crate::handlers::{self, AppState}; diff --git a/src/updater.rs b/src/updater.rs index 3602b36..0dc3f4d 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -8,7 +8,10 @@ use tracing::error; use crate::db::{DbReader, SharedDb, load_db}; -pub async fn ensure_db_exists(db_path: &Path, update_url: &str) -> Result<(), Box> { +pub async fn ensure_db_exists( + db_path: &Path, + update_url: &str, +) -> Result<(), Box> { if db_path.exists() { return Ok(()); } @@ -20,7 +23,10 @@ pub async fn ensure_db_exists(db_path: &Path, update_url: &str) -> Result<(), Bo download_db(update_url, db_path).await } -async fn download_db(url: &str, dest: &Path) -> Result<(), Box> { +async fn download_db( + url: &str, + dest: &Path, +) -> Result<(), Box> { let tmp_path = dest.with_extension("mmdb.tmp"); let client = reqwest::Client::builder() .timeout(Duration::from_secs(300)) diff --git a/tests/integration.rs b/tests/integration.rs index 74fe1d1..a25fc08 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -237,12 +237,8 @@ async fn root_without_cf_header_returns_error() { #[tokio::test] async fn json_current_ip_via_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/json", - vec![("CF-Connecting-IP", "8.8.8.8")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/json", vec![("CF-Connecting-IP", "8.8.8.8")]).await; assert_eq!(status, StatusCode::OK); let json: serde_json::Value = serde_json::from_str(&body).unwrap(); assert_eq!(json["ip"], "8.8.8.8"); @@ -286,12 +282,8 @@ async fn ipv6_via_cf_header() { #[tokio::test] async fn empty_ip_query_falls_back_to_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/json?ip=", - vec![("CF-Connecting-IP", "8.8.8.8")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/json?ip=", vec![("CF-Connecting-IP", "8.8.8.8")]).await; assert_eq!(status, StatusCode::OK); let json: serde_json::Value = serde_json::from_str(&body).unwrap(); assert_eq!(json["ip"], "8.8.8.8"); @@ -300,12 +292,8 @@ async fn empty_ip_query_falls_back_to_cf_header() { #[tokio::test] async fn whitespace_ip_query_falls_back_to_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/json?ip=%20", - vec![("CF-Connecting-IP", "8.8.8.8")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/json?ip=%20", vec![("CF-Connecting-IP", "8.8.8.8")]).await; assert_eq!(status, StatusCode::OK); let json: serde_json::Value = serde_json::from_str(&body).unwrap(); assert_eq!(json["ip"], "8.8.8.8"); @@ -358,12 +346,7 @@ async fn accept_json_with_query_param_returns_html() { #[tokio::test] async fn ip_endpoint_via_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/ip", - vec![("CF-Connecting-IP", "1.1.1.1")], - ) - .await; + let (status, body) = get_with_headers(&app, "/ip", vec![("CF-Connecting-IP", "1.1.1.1")]).await; assert_eq!(status, StatusCode::OK); assert_eq!(body.trim(), "1.1.1.1"); } @@ -371,12 +354,8 @@ async fn ip_endpoint_via_cf_header() { #[tokio::test] async fn asn_endpoint_via_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/asn", - vec![("CF-Connecting-IP", "1.1.1.1")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/asn", vec![("CF-Connecting-IP", "1.1.1.1")]).await; assert_eq!(status, StatusCode::OK); assert_eq!(body.trim(), "AS13335"); } @@ -384,12 +363,8 @@ async fn asn_endpoint_via_cf_header() { #[tokio::test] async fn org_endpoint_via_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/org", - vec![("CF-Connecting-IP", "1.1.1.1")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/org", vec![("CF-Connecting-IP", "1.1.1.1")]).await; assert_eq!(status, StatusCode::OK); assert_eq!(body.trim(), "Cloudflare, Inc."); } @@ -397,12 +372,8 @@ async fn org_endpoint_via_cf_header() { #[tokio::test] async fn country_endpoint_via_cf_header() { let app = build_test_app(); - let (status, body) = get_with_headers( - &app, - "/country", - vec![("CF-Connecting-IP", "45.77.77.77")], - ) - .await; + let (status, body) = + get_with_headers(&app, "/country", vec![("CF-Connecting-IP", "45.77.77.77")]).await; assert_eq!(status, StatusCode::OK); assert_eq!(body.trim(), "United States"); } @@ -411,11 +382,17 @@ async fn country_endpoint_via_cf_header() { async fn invalid_ip_on_field_endpoints_returns_400() { let app = build_test_app(); - let endpoints = ["/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor"]; + let endpoints = [ + "/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor", + ]; for endpoint in endpoints { let uri = format!("{endpoint}?ip=invalid"); let (status, _) = get(&app, &uri).await; - assert_eq!(status, StatusCode::BAD_REQUEST, "expected 400 for {endpoint}?ip=invalid"); + assert_eq!( + status, + StatusCode::BAD_REQUEST, + "expected 400 for {endpoint}?ip=invalid" + ); } } @@ -423,9 +400,15 @@ async fn invalid_ip_on_field_endpoints_returns_400() { async fn field_endpoints_without_ip_or_header_return_400() { let app = build_test_app(); - let endpoints = ["/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor", "/json"]; + let endpoints = [ + "/ip", "/asn", "/org", "/country", "/city", "/proxy", "/vpn", "/hosting", "/tor", "/json", + ]; for endpoint in endpoints { let (status, _) = get(&app, endpoint).await; - assert_eq!(status, StatusCode::BAD_REQUEST, "expected 400 for {endpoint} without IP"); + assert_eq!( + status, + StatusCode::BAD_REQUEST, + "expected 400 for {endpoint} without IP" + ); } }