Skip to content

Commit

Permalink
rustfmt modules included with if_feature! macro
Browse files Browse the repository at this point in the history
rustfmt has a known limitation (rust-lang/rustfmt#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.
  • Loading branch information
wfraser committed Feb 5, 2025
1 parent 065470e commit bac4981
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/cargo-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
45 changes: 27 additions & 18 deletions src/default_async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -76,7 +78,7 @@ impl HttpClient for UserAuthDefaultClient {
&self,
request: Self::Request,
body: Bytes,
) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
) -> impl Future<Output = Result<HttpRequestResultRaw, Error>> + Send {
self.inner.execute(request, body)
}

Expand Down Expand Up @@ -131,7 +133,7 @@ impl HttpClient for TeamAuthDefaultClient {
&self,
request: Self::Request,
body: Bytes,
) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
) -> impl Future<Output = Result<HttpRequestResultRaw, Error>> + Send {
self.inner.execute(request, body)
}

Expand Down Expand Up @@ -182,12 +184,17 @@ impl AppAuthDefaultClient {
impl HttpClient for AppAuthDefaultClient {
type Request = ReqwestRequest;

fn execute(&self, request: Self::Request, body: Bytes) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
fn execute(
&self,
request: Self::Request,
body: Bytes,
) -> impl Future<Output = Result<HttpRequestResultRaw, Error>> + 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)
}
}
Expand All @@ -212,7 +219,7 @@ impl HttpClient for NoauthDefaultClient {
&self,
request: Self::Request,
body: Bytes,
) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
) -> impl Future<Output = Result<HttpRequestResultRaw, Error>> + Send {
self.inner.execute(request, body)
}

Expand Down Expand Up @@ -240,7 +247,7 @@ impl HttpClient for TokenUpdateClient<'_> {
&self,
request: Self::Request,
body: Bytes,
) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
) -> impl Future<Output = Result<HttpRequestResultRaw, Error>> + Send {
self.inner.execute(request, body)
}

Expand All @@ -263,7 +270,7 @@ impl Default for ReqwestClient {
.https_only(true)
.http2_prior_knowledge()
.build()
.unwrap()
.unwrap(),
}
}
}
Expand All @@ -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| {
Expand All @@ -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();

Expand All @@ -327,7 +336,7 @@ impl HttpClient for ReqwestClient {
content_length,
body: Box::new(body),
})
}
},
)
.boxed()
}
Expand Down
46 changes: 25 additions & 21 deletions src/default_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -256,24 +259,21 @@ 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());
}
};

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()?;

Expand Down Expand Up @@ -335,7 +335,7 @@ macro_rules! wrap_error {
Self::HttpClient(Box::new(DefaultClientError::from(e)))
}
}
}
};
}

wrap_error!(std::io::Error);
Expand Down Expand Up @@ -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")
);
}
}

0 comments on commit bac4981

Please sign in to comment.