From bac4981663493fc878ad22d3ea6b47bc63045b7c Mon Sep 17 00:00:00 2001 From: Bill Fraser Date: Wed, 5 Feb 2025 14:51:36 -0800 Subject: [PATCH] rustfmt modules included with if_feature! macro rustfmt has a known limitation (https://github.com/rust-lang/rustfmt/issues/3253) that modules declared inside macros are not processed when using 'cargo fmt'. So instead, let's brute-force 'find' all applicable rust sources and call rustfmt directly. --- .github/workflows/cargo-test.yml | 4 +-- src/default_async_client.rs | 45 ++++++++++++++++++------------- src/default_client.rs | 46 +++++++++++++++++--------------- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/.github/workflows/cargo-test.yml b/.github/workflows/cargo-test.yml index 8bbcbf7..13bb8d1 100644 --- a/.github/workflows/cargo-test.yml +++ b/.github/workflows/cargo-test.yml @@ -20,9 +20,7 @@ jobs: - name: Run rustfmt run: | - echo "// empty module for rustfmt" > tests/generated.rs - rustup run nightly cargo fmt --check - rm tests/generated.rs + find . -path ./src/generated -prune -o -name '*.rs' -print | xargs rustup run nightly rustfmt --edition 2021 --check - name: Set up Python uses: actions/setup-python@v5.3.0 diff --git a/src/default_async_client.rs b/src/default_async_client.rs index 2a0a0fa..e4befa6 100644 --- a/src/default_async_client.rs +++ b/src/default_async_client.rs @@ -10,16 +10,18 @@ //! This code (and its dependencies) are only built if you use the `default_async_client` Cargo //! feature. -use std::future::{Future, ready}; -use std::str::FromStr; -use std::sync::Arc; -use bytes::Bytes; -use futures::{FutureExt, TryFutureExt, TryStreamExt}; -use crate::async_client_trait::{AppAuthClient, HttpClient, HttpRequest, HttpRequestResultRaw, - NoauthClient, TeamAuthClient, TeamSelect, UserAuthClient}; +use crate::async_client_trait::{ + AppAuthClient, HttpClient, HttpRequest, HttpRequestResultRaw, NoauthClient, TeamAuthClient, + TeamSelect, UserAuthClient, +}; use crate::default_client_common::impl_set_path_root; -use crate::Error; use crate::oauth2::{Authorization, TokenCache}; +use crate::Error; +use bytes::Bytes; +use futures::{FutureExt, TryFutureExt, TryStreamExt}; +use std::future::{ready, Future}; +use std::str::FromStr; +use std::sync::Arc; macro_rules! impl_update_token { ($self:ident) => { @@ -76,7 +78,7 @@ impl HttpClient for UserAuthDefaultClient { &self, request: Self::Request, body: Bytes, - ) -> impl Future> + Send { + ) -> impl Future> + Send { self.inner.execute(request, body) } @@ -131,7 +133,7 @@ impl HttpClient for TeamAuthDefaultClient { &self, request: Self::Request, body: Bytes, - ) -> impl Future> + Send { + ) -> impl Future> + Send { self.inner.execute(request, body) } @@ -182,12 +184,17 @@ impl AppAuthDefaultClient { impl HttpClient for AppAuthDefaultClient { type Request = ReqwestRequest; - fn execute(&self, request: Self::Request, body: Bytes) -> impl Future> + Send { + fn execute( + &self, + request: Self::Request, + body: Bytes, + ) -> impl Future> + Send { self.inner.execute(request, body) } fn new_request(&self, url: &str) -> Self::Request { - self.inner.new_request(url) + self.inner + .new_request(url) .set_header("Authorization", &self.auth) } } @@ -212,7 +219,7 @@ impl HttpClient for NoauthDefaultClient { &self, request: Self::Request, body: Bytes, - ) -> impl Future> + Send { + ) -> impl Future> + Send { self.inner.execute(request, body) } @@ -240,7 +247,7 @@ impl HttpClient for TokenUpdateClient<'_> { &self, request: Self::Request, body: Bytes, - ) -> impl Future> + Send { + ) -> impl Future> + Send { self.inner.execute(request, body) } @@ -263,7 +270,7 @@ impl Default for ReqwestClient { .https_only(true) .http2_prior_knowledge() .build() - .unwrap() + .unwrap(), } } } @@ -290,7 +297,8 @@ impl HttpClient for ReqwestClient { if !body.is_empty() { *req.body_mut() = Some(reqwest::Body::from(body)); } - self.inner.execute(req) + self.inner + .execute(req) .map_ok_or_else( |e| Err(Error::HttpClient(Box::new(e))), |resp| { @@ -317,7 +325,8 @@ impl HttpClient for ReqwestClient { }) .transpose()?; - let body = resp.bytes_stream() + let body = resp + .bytes_stream() .map_err(|e| futures::io::Error::new(futures::io::ErrorKind::Other, e)) .into_async_read(); @@ -327,7 +336,7 @@ impl HttpClient for ReqwestClient { content_length, body: Box::new(body), }) - } + }, ) .boxed() } diff --git a/src/default_client.rs b/src/default_client.rs index 9859fde..52e0b03 100644 --- a/src/default_client.rs +++ b/src/default_client.rs @@ -11,16 +11,18 @@ //! //! This code (and its dependencies) are only built if you use the `default_client` Cargo feature. -use crate::Error; +use crate::client_trait::{ + AppAuthClient, HttpClient, HttpRequest, HttpRequestResultRaw, NoauthClient, TeamAuthClient, + TeamSelect, UserAuthClient, +}; +use crate::default_client_common::impl_set_path_root; use crate::oauth2::{Authorization, TokenCache}; +use crate::Error; +use futures::FutureExt; use std::borrow::Cow; use std::fmt::Write; use std::str::FromStr; use std::sync::Arc; -use futures::FutureExt; -use crate::client_trait::{AppAuthClient, HttpClient, HttpRequest, HttpRequestResultRaw, - NoauthClient, TeamAuthClient, TeamSelect, UserAuthClient}; -use crate::default_client_common::impl_set_path_root; macro_rules! impl_update_token { ($self:ident) => { @@ -176,7 +178,8 @@ impl HttpClient for AppAuthDefaultClient { } fn new_request(&self, url: &str) -> Self::Request { - self.inner.new_request(url) + self.inner + .new_request(url) .set_header("Authorization", &self.auth) } } @@ -256,12 +259,8 @@ impl HttpClient for UreqClient { }; let (status, resp) = match resp { - Ok(resp) => { - (resp.status(), resp) - } - Err(ureq::Error::Status(status, resp)) => { - (status, resp) - } + Ok(resp) => (resp.status(), resp), + Err(ureq::Error::Status(status, resp)) => (status, resp), Err(e @ ureq::Error::Transport(_)) => { return Err(RequestError { inner: e }.into()); } @@ -269,11 +268,12 @@ impl HttpClient for UreqClient { let result_header = resp.header("Dropbox-API-Result").map(String::from); - let content_length = resp.header("Content-Length") + let content_length = resp + .header("Content-Length") .map(|s| { - u64::from_str(s) - .map_err(|e| Error::UnexpectedResponse( - format!("invalid Content-Length {s:?}: {e}"))) + u64::from_str(s).map_err(|e| { + Error::UnexpectedResponse(format!("invalid Content-Length {s:?}: {e}")) + }) }) .transpose()?; @@ -335,7 +335,7 @@ macro_rules! wrap_error { Self::HttpClient(Box::new(DefaultClientError::from(e))) } } - } + }; } wrap_error!(std::io::Error); @@ -403,13 +403,17 @@ mod test { assert_eq!(Cow::Borrowed("foobar"), json_escape_header("foobar")); assert_eq!( Cow::<'_, str>::Owned("tro\\u0161kovi".to_owned()), - json_escape_header("troškovi")); + json_escape_header("troškovi") + ); assert_eq!( Cow::<'_, str>::Owned( - r#"{"field": "some_\u00fc\u00f1\u00eec\u00f8d\u00e9_and_\u007f"}"#.to_owned()), - json_escape_header("{\"field\": \"some_üñîcødé_and_\x7f\"}")); + r#"{"field": "some_\u00fc\u00f1\u00eec\u00f8d\u00e9_and_\u007f"}"#.to_owned() + ), + json_escape_header("{\"field\": \"some_üñîcødé_and_\x7f\"}") + ); assert_eq!( Cow::<'_, str>::Owned("almost,\\u007f but not quite".to_owned()), - json_escape_header("almost,\x7f but not quite")); + json_escape_header("almost,\x7f but not quite") + ); } }