mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-26 08:32:48 +01:00
Use implicit format-args captures where applicable (#1709)
This commit is contained in:
parent
b07918b213
commit
7ecf8bd6cf
16 changed files with 42 additions and 46 deletions
|
@ -313,7 +313,7 @@ where
|
|||
if let Some(path_without_trailing_slash) = path.strip_suffix('/') {
|
||||
router.route(path_without_trailing_slash, any(redirect_handler))
|
||||
} else {
|
||||
router.route(&format!("{}/", path), any(redirect_handler))
|
||||
router.route(&format!("{path}/"), any(redirect_handler))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -157,10 +157,10 @@ mod tests {
|
|||
.index(|| async { "users#index" })
|
||||
.create(|| async { "users#create" })
|
||||
.new(|| async { "users#new" })
|
||||
.show(|Path(id): Path<u64>| async move { format!("users#show id={}", id) })
|
||||
.edit(|Path(id): Path<u64>| async move { format!("users#edit id={}", id) })
|
||||
.update(|Path(id): Path<u64>| async move { format!("users#update id={}", id) })
|
||||
.destroy(|Path(id): Path<u64>| async move { format!("users#destroy id={}", id) });
|
||||
.show(|Path(id): Path<u64>| async move { format!("users#show id={id}") })
|
||||
.edit(|Path(id): Path<u64>| async move { format!("users#edit id={id}") })
|
||||
.update(|Path(id): Path<u64>| async move { format!("users#update id={id}") })
|
||||
.destroy(|Path(id): Path<u64>| async move { format!("users#destroy id={id}") });
|
||||
|
||||
let mut app = Router::new().merge(users);
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ where
|
|||
|
||||
if out.is_some() {
|
||||
let kw_name = std::any::type_name::<K>().split("::").last().unwrap();
|
||||
let msg = format!("`{}` specified more than once", kw_name);
|
||||
let msg = format!("`{kw_name}` specified more than once");
|
||||
return Err(syn::Error::new_spanned(kw, msg));
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ where
|
|||
|
||||
if out.is_some() {
|
||||
let kw_name = std::any::type_name::<K>().split("::").last().unwrap();
|
||||
let msg = format!("`{}` specified more than once", kw_name);
|
||||
let msg = format!("`{kw_name}` specified more than once");
|
||||
return Err(syn::Error::new_spanned(kw, msg));
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ where
|
|||
if let Some((kw, inner)) = b {
|
||||
if a.is_some() {
|
||||
let kw_name = std::any::type_name::<K>().split("::").last().unwrap();
|
||||
let msg = format!("`{}` specified more than once", kw_name);
|
||||
let msg = format!("`{kw_name}` specified more than once");
|
||||
return Err(syn::Error::new_spanned(kw, msg));
|
||||
}
|
||||
*a = Some((kw, inner));
|
||||
|
@ -89,7 +89,7 @@ where
|
|||
if let Some(kw) = b {
|
||||
if a.is_some() {
|
||||
let kw_name = std::any::type_name::<K>().split("::").last().unwrap();
|
||||
let msg = format!("`{}` specified more than once", kw_name);
|
||||
let msg = format!("`{kw_name}` specified more than once");
|
||||
return Err(syn::Error::new_spanned(kw, msg));
|
||||
}
|
||||
*a = Some(kw);
|
||||
|
|
|
@ -111,9 +111,8 @@ fn check_extractor_count(item_fn: &ItemFn) -> Option<TokenStream> {
|
|||
None
|
||||
} else {
|
||||
let error_message = format!(
|
||||
"Handlers cannot take more than {} arguments. \
|
||||
"Handlers cannot take more than {max_extractors} arguments. \
|
||||
Use `(a, b): (ExtractorA, ExtractorA)` to further nest extractors",
|
||||
max_extractors,
|
||||
);
|
||||
let error = syn::Error::new_spanned(&item_fn.sig.inputs, error_message).to_compile_error();
|
||||
Some(error)
|
||||
|
|
|
@ -665,7 +665,7 @@ where
|
|||
Ok(tokens) => {
|
||||
let tokens = (quote! { #tokens }).into();
|
||||
if std::env::var_os("AXUM_MACROS_DEBUG").is_some() {
|
||||
eprintln!("{}", tokens);
|
||||
eprintln!("{tokens}");
|
||||
}
|
||||
tokens
|
||||
}
|
||||
|
@ -722,7 +722,7 @@ fn run_ui_tests(directory: &str) {
|
|||
path = path_without_prefix.to_owned();
|
||||
}
|
||||
|
||||
if !path.contains(&format!("/{}/", directory)) {
|
||||
if !path.contains(&format!("/{directory}/")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -734,8 +734,8 @@ fn run_ui_tests(directory: &str) {
|
|||
panic!()
|
||||
}
|
||||
} else {
|
||||
t.compile_fail(format!("tests/{}/fail/*.rs", directory));
|
||||
t.pass(format!("tests/{}/pass/*.rs", directory));
|
||||
t.compile_fail(format!("tests/{directory}/fail/*.rs"));
|
||||
t.pass(format!("tests/{directory}/pass/*.rs"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -267,9 +267,9 @@ fn expand_unnamed_fields(
|
|||
|
||||
fn simple_pluralize(count: usize, word: &str) -> String {
|
||||
if count == 1 {
|
||||
format!("{} {}", count, word)
|
||||
format!("{count} {word}")
|
||||
} else {
|
||||
format!("{} {}s", count, word)
|
||||
format!("{count} {word}s")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -354,7 +354,7 @@ fn format_str_from_path(segments: &[Segment]) -> String {
|
|||
segments
|
||||
.iter()
|
||||
.map(|segment| match segment {
|
||||
Segment::Capture(capture, _) => format!("{{{}}}", capture),
|
||||
Segment::Capture(capture, _) => format!("{{{capture}}}"),
|
||||
Segment::Static(segment) => segment.to_owned(),
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
|
|
|
@ -26,7 +26,7 @@ fn main() {
|
|||
for a in 0..10 {
|
||||
for b in 0..10 {
|
||||
for c in 0..10 {
|
||||
app = app.route(&format!("/foo-{}/bar-{}/baz-{}", a, b, c), get(|| async {}));
|
||||
app = app.route(&format!("/foo-{a}/bar-{b}/baz-{c}"), get(|| async {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ impl BenchmarkBuilder {
|
|||
|
||||
for (key, value) in self.headers.into_iter().flatten() {
|
||||
cmd.arg("--header");
|
||||
cmd.arg(format!("{}: {}", key, value));
|
||||
cmd.arg(format!("{key}: {value}"));
|
||||
}
|
||||
|
||||
if let Some(body) = self.body {
|
||||
|
@ -196,7 +196,7 @@ impl BenchmarkBuilder {
|
|||
let stdout = std::io::BufReader::new(stdout);
|
||||
for line in stdout.lines() {
|
||||
let line = line.unwrap();
|
||||
println!(" {}", line);
|
||||
println!(" {line}");
|
||||
}
|
||||
|
||||
let status = child.wait().unwrap();
|
||||
|
|
|
@ -151,7 +151,7 @@ mod tests {
|
|||
#[crate::test]
|
||||
async fn socket_addr() {
|
||||
async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
|
||||
format!("{}", addr)
|
||||
format!("{addr}")
|
||||
}
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
|
@ -170,7 +170,7 @@ mod tests {
|
|||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
|
||||
let res = client.get(format!("http://{addr}")).send().await.unwrap();
|
||||
let body = res.text().await.unwrap();
|
||||
assert!(body.starts_with("127.0.0.1:"));
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ mod tests {
|
|||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
|
||||
let res = client.get(format!("http://{addr}")).send().await.unwrap();
|
||||
let body = res.text().await.unwrap();
|
||||
assert_eq!(body, "it worked!");
|
||||
}
|
||||
|
|
|
@ -334,12 +334,11 @@ impl fmt::Display for ErrorKind {
|
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ErrorKind::Message(error) => error.fmt(f),
|
||||
ErrorKind::InvalidUtf8InPathParam { key } => write!(f, "Invalid UTF-8 in `{}`", key),
|
||||
ErrorKind::InvalidUtf8InPathParam { key } => write!(f, "Invalid UTF-8 in `{key}`"),
|
||||
ErrorKind::WrongNumberOfParameters { got, expected } => {
|
||||
write!(
|
||||
f,
|
||||
"Wrong number of path arguments for `Path`. Expected {} but got {}",
|
||||
expected, got
|
||||
"Wrong number of path arguments for `Path`. Expected {expected} but got {got}"
|
||||
)?;
|
||||
|
||||
if *expected == 1 {
|
||||
|
@ -348,28 +347,26 @@ impl fmt::Display for ErrorKind {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
ErrorKind::UnsupportedType { name } => write!(f, "Unsupported type `{}`", name),
|
||||
ErrorKind::UnsupportedType { name } => write!(f, "Unsupported type `{name}`"),
|
||||
ErrorKind::ParseErrorAtKey {
|
||||
key,
|
||||
value,
|
||||
expected_type,
|
||||
} => write!(
|
||||
f,
|
||||
"Cannot parse `{}` with value `{:?}` to a `{}`",
|
||||
key, value, expected_type
|
||||
"Cannot parse `{key}` with value `{value:?}` to a `{expected_type}`"
|
||||
),
|
||||
ErrorKind::ParseError {
|
||||
value,
|
||||
expected_type,
|
||||
} => write!(f, "Cannot parse `{:?}` to a `{}`", value, expected_type),
|
||||
} => write!(f, "Cannot parse `{value:?}` to a `{expected_type}`"),
|
||||
ErrorKind::ParseErrorAtIndex {
|
||||
index,
|
||||
value,
|
||||
expected_type,
|
||||
} => write!(
|
||||
f,
|
||||
"Cannot parse value at index {} with value `{:?}` to a `{}`",
|
||||
index, value, expected_type
|
||||
"Cannot parse value at index {index} with value `{value:?}` to a `{expected_type}`"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -355,7 +355,7 @@ mod tests {
|
|||
#[crate::test]
|
||||
async fn handler_into_service() {
|
||||
async fn handle(body: String) -> impl IntoResponse {
|
||||
format!("you said: {}", body)
|
||||
format!("you said: {body}")
|
||||
}
|
||||
|
||||
let client = TestClient::new(handle.into_service());
|
||||
|
|
|
@ -262,7 +262,7 @@ mod tests {
|
|||
#[crate::test]
|
||||
async fn json_content_types() {
|
||||
async fn valid_json_content_type(content_type: &str) -> bool {
|
||||
println!("testing {:?}", content_type);
|
||||
println!("testing {content_type:?}");
|
||||
|
||||
let app = Router::new().route("/", post(|Json(_): Json<Value>| async {}));
|
||||
|
||||
|
|
|
@ -107,9 +107,9 @@ fn strip_prefix(uri: &Uri, prefix: &str) -> Option<Uri> {
|
|||
|
||||
let new_path_and_query = match (after_prefix.starts_with('/'), path_and_query.query()) {
|
||||
(true, None) => after_prefix.parse().unwrap(),
|
||||
(true, Some(query)) => format!("{}?{}", after_prefix, query).parse().unwrap(),
|
||||
(false, None) => format!("/{}", after_prefix).parse().unwrap(),
|
||||
(false, Some(query)) => format!("/{}?{}", after_prefix, query).parse().unwrap(),
|
||||
(true, Some(query)) => format!("{after_prefix}?{query}").parse().unwrap(),
|
||||
(false, None) => format!("/{after_prefix}").parse().unwrap(),
|
||||
(false, Some(query)) => format!("/{after_prefix}?{query}").parse().unwrap(),
|
||||
};
|
||||
|
||||
let mut parts = uri.clone().into_parts();
|
||||
|
|
|
@ -63,8 +63,8 @@ async fn multiple_ors_balanced_differently() {
|
|||
let client = TestClient::new(app);
|
||||
|
||||
for n in ["one", "two", "three", "four"].iter() {
|
||||
println!("running: {} / {}", name, n);
|
||||
let res = client.get(&format!("/{}", n)).send().await;
|
||||
println!("running: {name} / {n}");
|
||||
let res = client.get(&format!("/{n}")).send().await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.text().await, *n);
|
||||
}
|
||||
|
@ -180,7 +180,7 @@ async fn many_ors() {
|
|||
let client = TestClient::new(app);
|
||||
|
||||
for n in 1..=7 {
|
||||
let res = client.get(&format!("/r{}", n)).send().await;
|
||||
let res = client.get(&format!("/r{n}")).send().await;
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
}
|
||||
|
||||
|
|
|
@ -385,7 +385,7 @@ async fn static_and_dynamic_paths() {
|
|||
let app = Router::new()
|
||||
.route(
|
||||
"/:key",
|
||||
get(|Path(key): Path<String>| async move { format!("dynamic: {}", key) }),
|
||||
get(|Path(key): Path<String>| async move { format!("dynamic: {key}") }),
|
||||
)
|
||||
.route("/foo", get(|| async { "static" }));
|
||||
|
||||
|
@ -618,7 +618,7 @@ async fn body_limited_by_default() {
|
|||
let client = TestClient::new(app);
|
||||
|
||||
for uri in ["/bytes", "/string", "/json"] {
|
||||
println!("calling {}", uri);
|
||||
println!("calling {uri}");
|
||||
|
||||
let stream = futures_util::stream::repeat("a".repeat(1000)).map(Ok::<_, hyper::Error>);
|
||||
let body = Body::wrap_stream(stream);
|
||||
|
|
|
@ -207,7 +207,7 @@ async fn nest_static_file_server() {
|
|||
get_service(ServeDir::new(".")).handle_error(|error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
format!("Unhandled internal error: {error}"),
|
||||
)
|
||||
}),
|
||||
);
|
||||
|
@ -323,7 +323,7 @@ async fn outer_middleware_still_see_whole_url() {
|
|||
async fn nest_at_capture() {
|
||||
let api_routes = Router::new().route(
|
||||
"/:b",
|
||||
get(|Path((a, b)): Path<(String, String)>| async move { format!("a={} b={}", a, b) }),
|
||||
get(|Path((a, b)): Path<(String, String)>| async move { format!("a={a} b={b}") }),
|
||||
);
|
||||
|
||||
let app = Router::new().nest("/:a", api_routes);
|
||||
|
|
|
@ -26,7 +26,7 @@ impl TestClient {
|
|||
{
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Could not bind ephemeral socket");
|
||||
let addr = listener.local_addr().unwrap();
|
||||
println!("Listening on {}", addr);
|
||||
println!("Listening on {addr}");
|
||||
|
||||
tokio::spawn(async move {
|
||||
let server = Server::from_tcp(listener).unwrap().serve(Shared::new(svc));
|
||||
|
|
Loading…
Add table
Reference in a new issue