From 97dad443488c153a57aa541d79cfb9b13e1dc1c7 Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:10:32 +0300 Subject: [PATCH] cargo clippy --- src/lib.rs | 144 ++++++++++++++++++++++-------------------------- src/response.rs | 2 +- src/traits.rs | 12 ++-- src/utils.rs | 6 +- 4 files changed, 76 insertions(+), 88 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 338cd8e..65add9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::too_many_arguments)] use std::str::FromStr; use std::sync::{Arc, LazyLock, Mutex}; use std::time::Duration; @@ -33,6 +34,8 @@ use traits::{CookiesTraits, HeadersTraits}; mod utils; use utils::load_ca_certs; +type IndexMapSSR = IndexMap; + // Tokio global one-thread runtime static RUNTIME: LazyLock = LazyLock::new(|| { runtime::Builder::new_current_thread() @@ -50,7 +53,7 @@ pub struct Client { #[pyo3(get, set)] auth_bearer: Option, #[pyo3(get, set)] - params: Option>, + params: Option, #[pyo3(get, set)] proxy: Option, #[pyo3(get, set)] @@ -115,9 +118,9 @@ impl Client { fn new( auth: Option<(String, Option)>, auth_bearer: Option, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, cookie_store: Option, referer: Option, proxy: Option, @@ -135,8 +138,8 @@ impl Client { // Impersonate if let Some(impersonation_type) = impersonate { - let impersonation = Impersonate::from_str(impersonation_type) - .map_err(|err| PyValueError::new_err(err))?; + let impersonation = + Impersonate::from_str(impersonation_type).map_err(PyValueError::new_err)?; client_builder = client_builder.impersonate(impersonation); } @@ -214,7 +217,7 @@ impl Client { } #[getter] - pub fn get_headers(&self) -> Result> { + pub fn get_headers(&self) -> Result { let mut client = self.client.lock().unwrap(); let mut headers = client.headers_mut().clone(); headers.remove(COOKIE); @@ -222,10 +225,7 @@ impl Client { } #[setter] - pub fn set_headers( - &self, - new_headers: Option>, - ) -> Result<()> { + pub fn set_headers(&self, new_headers: Option) -> Result<()> { let mut client = self.client.lock().unwrap(); let headers = client.headers_mut(); headers.clear(); @@ -238,11 +238,10 @@ impl Client { } #[getter] - pub fn get_cookies(&self) -> Result> { + pub fn get_cookies(&self) -> Result { let mut client = self.client.lock().unwrap(); let headers = client.headers_mut(); - let mut cookies: IndexMap = - IndexMap::with_hasher(RandomState::default()); + let mut cookies: IndexMapSSR = IndexMap::with_hasher(RandomState::default()); if let Some(cookie_header) = headers.get(COOKIE) { for part in cookie_header.to_str()?.split(';') { if let Some((key, value)) = part.trim().split_once('=') { @@ -254,10 +253,7 @@ impl Client { } #[setter] - pub fn set_cookies( - &self, - cookies: Option>, - ) -> Result<()> { + pub fn set_cookies(&self, cookies: Option) -> Result<()> { let mut client = self.client.lock().unwrap(); let headers = client.headers_mut(); if let Some(cookies) = cookies { @@ -312,9 +308,9 @@ impl Client { py: Python, method: &str, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -397,11 +393,11 @@ impl Client { let resp = request_builder.send().await?; // Response items - let cookies: IndexMap = resp + let cookies: IndexMapSSR = resp .cookies() .map(|cookie| (cookie.name().to_string(), cookie.value().to_string())) .collect(); - let headers: IndexMap = resp.headers().to_indexmap(); + let headers: IndexMapSSR = resp.headers().to_indexmap(); let status_code = resp.status().as_u16(); let url = resp.url().to_string(); let buf = resp.bytes().await?; @@ -412,16 +408,8 @@ impl Client { // Execute an async future, releasing the Python GIL for concurrency. // Use Tokio global runtime to block on the future. - let result: Result< - ( - Bytes, - IndexMap, - IndexMap, - u16, - String, - ), - Error, - > = py.allow_threads(|| RUNTIME.block_on(future)); + let result: Result<(Bytes, IndexMapSSR, IndexMapSSR, u16, String), Error> = + py.allow_threads(|| RUNTIME.block_on(future)); let (f_buf, f_cookies, f_headers, f_status_code, f_url) = result?; Ok(Response { @@ -439,9 +427,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -468,9 +456,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -497,9 +485,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -526,9 +514,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -556,9 +544,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -590,9 +578,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -624,9 +612,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -662,9 +650,9 @@ fn request( py: Python, method: &str, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -717,9 +705,9 @@ fn request( fn get( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -763,9 +751,9 @@ fn get( fn head( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -809,9 +797,9 @@ fn head( fn options( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -855,9 +843,9 @@ fn options( fn delete( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -902,9 +890,9 @@ fn delete( fn post( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -957,9 +945,9 @@ fn post( fn put( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, @@ -1012,9 +1000,9 @@ fn put( fn patch( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option, + headers: Option, + cookies: Option, content: Option>, data: Option<&Bound<'_, PyAny>>, json: Option<&Bound<'_, PyAny>>, diff --git a/src/response.rs b/src/response.rs index 50fd6bf..a574642 100644 --- a/src/response.rs +++ b/src/response.rs @@ -61,7 +61,7 @@ impl Response { let (decoded_str, detected_encoding, _) = encoding.decode(raw_bytes); // Update self.encoding based on the detected encoding - if &self.encoding != detected_encoding.name() { + if self.encoding != detected_encoding.name() { self.encoding = detected_encoding.name().to_string(); } diff --git a/src/traits.rs b/src/traits.rs index b8ce4bc..aeaf48f 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -4,14 +4,16 @@ use indexmap::IndexMap; use rquest::header::{HeaderMap, HeaderName, HeaderValue}; +type IndexMapSSR = IndexMap; + pub trait HeadersTraits { - fn to_indexmap(&self) -> IndexMap; + fn to_indexmap(&self) -> IndexMapSSR; fn to_headermap(&self) -> HeaderMap; fn insert_key_value(&mut self, key: String, value: String) -> Result<(), Error>; } -impl HeadersTraits for IndexMap { - fn to_indexmap(&self) -> IndexMap { +impl HeadersTraits for IndexMapSSR { + fn to_indexmap(&self) -> IndexMapSSR { self.clone() } fn to_headermap(&self) -> HeaderMap { @@ -33,7 +35,7 @@ impl HeadersTraits for IndexMap { } impl HeadersTraits for HeaderMap { - fn to_indexmap(&self) -> IndexMap { + fn to_indexmap(&self) -> IndexMapSSR { self.iter() .map(|(k, v)| { ( @@ -64,7 +66,7 @@ pub trait CookiesTraits { fn to_string(&self) -> String; } -impl CookiesTraits for IndexMap { +impl CookiesTraits for IndexMapSSR { fn to_string(&self) -> String { self.iter() .map(|(k, v)| format!("{}={}", k, v)) diff --git a/src/utils.rs b/src/utils.rs index 7e187be..91a4f85 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,14 +15,12 @@ use rquest::boring::{ pub fn load_ca_certs() -> Option<&'static X509Store> { static CERT_STORE: LazyLock> = LazyLock::new(|| { let mut ca_store = X509StoreBuilder::new()?; - if let Some(ca_cert_path) = std::env::var("PRIMP_CA_BUNDLE") - .or(std::env::var("CA_CERT_FILE")) - .ok() + if let Ok(ca_cert_path) = std::env::var("PRIMP_CA_BUNDLE").or(std::env::var("CA_CERT_FILE")) { // Use CA certificate bundle from env var PRIMP_CA_BUNDLE let cert_file = &std::fs::read(ca_cert_path) .expect("Failed to read file from env var PRIMP_CA_BUNDLE"); - let certs = X509::stack_from_pem(&cert_file)?; + let certs = X509::stack_from_pem(cert_file)?; for cert in certs { ca_store.add_cert(cert)?; }