From 47df7780feadc64da10d1abefdf5049a0850ce25 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 12 Feb 2022 10:51:52 +0100 Subject: [PATCH] Use captured identifiers in format strings (#507) Makes for arguably cleaner-looking code. The downside is that the MSRV is 1.58 https://blog.rust-lang.org/2022/01/13/Rust-1.58.0.html Given that nobody uses lychee as a library yet and we have precompiled binaries, it's an acceptable tradeoff. My little research revealed that this is a much-liked feature: https://twitter.com/matthiasendler/status/1483895557621960715 --- README.md | 2 +- examples/client_pool/client_pool.rs | 2 +- examples/extract/extract.rs | 2 +- lychee-bin/src/commands/check.rs | 2 +- lychee-bin/src/commands/dump.rs | 2 +- lychee-bin/src/main.rs | 4 ++-- lychee-bin/src/writer/compact.rs | 10 ++++++++-- lychee-bin/tests/cli.rs | 3 +-- lychee-lib/src/collector.rs | 2 +- lychee-lib/src/lib.rs | 2 +- lychee-lib/src/quirks/mod.rs | 3 +-- lychee-lib/src/types/input.rs | 2 +- lychee-lib/src/types/mail.rs | 4 ++-- 13 files changed, 22 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index db4c65c..d3d23be 100644 --- a/README.md +++ b/README.md @@ -291,7 +291,7 @@ use lychee_lib::Result; #[tokio::main] async fn main() -> Result<()> { let response = lychee_lib::check("https://github.com/lycheeverse/lychee").await?; - println!("{}", response); + println!("{response}"); Ok(()) } ``` diff --git a/examples/client_pool/client_pool.rs b/examples/client_pool/client_pool.rs index 4df05f2..300aeae 100644 --- a/examples/client_pool/client_pool.rs +++ b/examples/client_pool/client_pool.rs @@ -41,7 +41,7 @@ async fn main() -> Result<()> { // Finally, listen to incoming responses from lychee while let Some(response) = recv_resp.recv().await { - println!("{}", response); + println!("{response}"); } Ok(()) diff --git a/examples/extract/extract.rs b/examples/extract/extract.rs index cbda676..4cecd29 100644 --- a/examples/extract/extract.rs +++ b/examples/extract/extract.rs @@ -7,7 +7,7 @@ use std::fs; async fn main() -> Result<()> { let input = fs::read_to_string("fixtures/elvis.html").unwrap(); let links = Extractor::extract(&InputContent::from_string(&input, FileType::Html)); - println!("{:#?}", links); + println!("{links:#?}"); Ok(()) } diff --git a/lychee-bin/src/commands/check.rs b/lychee-bin/src/commands/check.rs index fd49a34..cbe8464 100644 --- a/lychee-bin/src/commands/check.rs +++ b/lychee-bin/src/commands/check.rs @@ -151,6 +151,6 @@ fn show_progress(progress_bar: &Option, response: &Response, verbos if (response.status().is_success() || response.status().is_excluded()) && !verbose { return; } - println!("{}", out); + println!("{out}"); } } diff --git a/lychee-bin/src/commands/dump.rs b/lychee-bin/src/commands/dump.rs index f170f61..572074c 100644 --- a/lychee-bin/src/commands/dump.rs +++ b/lychee-bin/src/commands/dump.rs @@ -25,7 +25,7 @@ where // to another program like `grep`. if let Err(e) = write(&request, verbose) { if e.kind() != io::ErrorKind::BrokenPipe { - eprintln!("{}", e); + eprintln!("{e}"); return Ok(ExitCode::UnexpectedFailure); } } diff --git a/lychee-bin/src/main.rs b/lychee-bin/src/main.rs index 00a1ac1..3badf9d 100644 --- a/lychee-bin/src/main.rs +++ b/lychee-bin/src/main.rs @@ -173,7 +173,7 @@ fn load_cache(cfg: &Config) -> Option { match cache { Ok(cache) => Some(cache), Err(e) => { - println!("Error while loading cache: {}. Continuing without.", e); + println!("Error while loading cache: {e}. Continuing without."); None } } @@ -247,7 +247,7 @@ fn write_stats(stats: ResponseStats, cfg: &Config) -> Result<()> { println!(); } // we assume that the formatted stats don't have a final newline - println!("{}", formatted); + println!("{formatted}"); } Ok(()) } diff --git a/lychee-bin/src/writer/compact.rs b/lychee-bin/src/writer/compact.rs index 31cee1b..034bbc6 100644 --- a/lychee-bin/src/writer/compact.rs +++ b/lychee-bin/src/writer/compact.rs @@ -26,7 +26,7 @@ pub(crate) fn print_errors(stats: &ResponseStats) -> String { let mut err: Vec<_> = errors .into_iter() .filter(|(_, v)| *v > 0) - .map(|(k, v)| format!("{}:{}", k, v)) + .map(|(k, v)| format!("{k}:{v}")) .collect(); err.sort(); err.join("|") @@ -37,10 +37,16 @@ impl Display for CompactResponseStats { let stats = &self.0; if !stats.fail_map.is_empty() { + let input = if stats.fail_map.len() == 1 { + "input" + } else { + "inputs" + }; + color!( f, BOLD_PINK, - "Issues found in {} input(s). Find details below.\n\n", + "Issues found in {} {input}. Find details below.\n\n", stats.fail_map.len() )?; } diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index de1e20e..c17146f 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -327,8 +327,7 @@ mod cli { .failure() .code(1) .stderr(contains(format!( - "Error: Failed to read from path: `{}`, reason: No such file or directory (os error 2)", - filename + "Error: Failed to read from path: `{filename}`, reason: No such file or directory (os error 2)" ))); } diff --git a/lychee-lib/src/collector.rs b/lychee-lib/src/collector.rs index a2d5eff..0a6450b 100644 --- a/lychee-lib/src/collector.rs +++ b/lychee-lib/src/collector.rs @@ -330,7 +330,7 @@ mod test { let expected_urls = HashSet::from_iter([ website("https://github.com/lycheeverse/lychee/"), - website(&format!("{}about", server_uri)), + website(&format!("{server_uri}about")), ]); assert_eq!(links, expected_urls); diff --git a/lychee-lib/src/lib.rs b/lychee-lib/src/lib.rs index 6d94b31..6d60294 100644 --- a/lychee-lib/src/lib.rs +++ b/lychee-lib/src/lib.rs @@ -8,7 +8,7 @@ //! #[tokio::main] //! async fn main() -> Result<()> { //! let response = lychee_lib::check("https://github.com/lycheeverse/lychee").await?; -//! println!("{}", response); +//! println!("{response}"); //! Ok(()) //! } //! ``` diff --git a/lychee-lib/src/quirks/mod.rs b/lychee-lib/src/quirks/mod.rs index 99d6bbe..f05dd8c 100644 --- a/lychee-lib/src/quirks/mod.rs +++ b/lychee-lib/src/quirks/mod.rs @@ -63,8 +63,7 @@ impl Default for Quirks { let mut out = request; if let Some(id) = query(&out).get("v") { *out.url_mut() = - Url::parse(&format!("https://img.youtube.com/vi/{}/0.jpg", &id)) - .unwrap(); + Url::parse(&format!("https://img.youtube.com/vi/{id}/0.jpg")).unwrap(); } out }, diff --git a/lychee-lib/src/types/input.rs b/lychee-lib/src/types/input.rs index 4e75d7e..67a8428 100644 --- a/lychee-lib/src/types/input.rs +++ b/lychee-lib/src/types/input.rs @@ -263,7 +263,7 @@ impl Input { let content: InputContent = Self::path_content(&path).await?; yield content; } - Err(e) => println!("{:?}", e), + Err(e) => println!("{e:?}"), } } } diff --git a/lychee-lib/src/types/mail.rs b/lychee-lib/src/types/mail.rs index 9025395..4fec044 100644 --- a/lychee-lib/src/types/mail.rs +++ b/lychee-lib/src/types/mail.rs @@ -6,9 +6,9 @@ pub(crate) fn error_from_output(o: &CheckEmailOutput) -> String { if let Err(_e) = o.misc.as_ref() { return "Error occurred connecting to this email server via SMTP".to_string(); } else if let Err(e) = &o.smtp { - return format!("{:?}", e); + return format!("{e:?}"); } else if let Err(e) = &o.mx { - return format!("{:?}", e); + return format!("{e:?}"); } match &o.is_reachable { Reachable::Safe => "Safe: The email is safe to send",