From e8b9e5555803e4244baddea7025ed66b32844b04 Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 10 Dec 2024 20:09:18 +0000 Subject: [PATCH] Formatting without fmt::Debug (#76) * More efficient string formatting * Fixed escaping * Format without fmt::Debug dependency * Test fmt-debug=none --- .github/workflows/check.yml | 4 +++- src/dependencies.rs | 17 +++++++------- src/environment.rs | 31 +++++++++++++++++-------- src/git.rs | 4 ++-- src/lib.rs | 2 +- src/util.rs | 45 +++++++++++++++++++++++++++++++++++++ tests/testbox_tests.rs | 31 ++++++++++++------------- 7 files changed, 98 insertions(+), 36 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index d49a7497b..295341112 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -47,9 +47,11 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macos-latest] runs-on: ${{ matrix.os }} + env: + RUSTFLAGS: -Zfmt-debug=none steps: - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@nightly with: targets: x86_64-unknown-none - run: cargo test --no-default-features diff --git a/src/dependencies.rs b/src/dependencies.rs index 14ee2442a..bfac08ac7 100644 --- a/src/dependencies.rs +++ b/src/dependencies.rs @@ -1,3 +1,4 @@ +use crate::util::TupleArrayDisplay; use crate::{write_str_variable, write_variable}; use std::{collections, fs, io, path}; @@ -85,8 +86,8 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i write_variable!( w, "DEPENDENCIES", - format!("[(&str, &str); {}]", dependencies.deps.len()), - format!("{0:?}", dependencies.deps), + format_args!("[(&str, &str); {}]", dependencies.deps.len()), + TupleArrayDisplay(&dependencies.deps), "An array of effective dependencies as documented by `Cargo.lock`." ); write_str_variable!( @@ -104,8 +105,8 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i write_variable!( w, "DIRECT_DEPENDENCIES", - format!("[(&str, &str); {}]", dependencies.direct_deps.len()), - format!("{0:?}", dependencies.direct_deps), + format_args!("[(&str, &str); {}]", dependencies.direct_deps.len()), + TupleArrayDisplay(&dependencies.direct_deps), "An array of direct dependencies as documented by `Cargo.lock`." ); write_str_variable!( @@ -123,8 +124,8 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i write_variable!( w, "INDIRECT_DEPENDENCIES", - format!("[(&str, &str); {}]", dependencies.indirect_deps.len()), - format!("{0:?}", dependencies.indirect_deps), + format_args!("[(&str, &str); {}]", dependencies.indirect_deps.len()), + TupleArrayDisplay(&dependencies.indirect_deps), "An array of indirect dependencies as documented by `Cargo.lock`." ); write_str_variable!( @@ -155,8 +156,8 @@ pub fn write_dependencies(manifest_location: &path::Path, mut w: &fs::File) -> i write_variable!( w, "DEPENDENCIES", - format!("[(&str, &str); {}]", deps.len()), - format!("{deps:?}"), + format_args!("[(&str, &str); {}]", deps.len()), + TupleArrayDisplay(&deps), "An array of effective dependencies as documented by `Cargo.lock`." ); write_str_variable!( diff --git a/src/environment.rs b/src/environment.rs index 73d91eeed..9114e944d 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,3 +1,4 @@ +use crate::util::ArrayDisplay; use crate::{fmt_option_str, write_str_variable, write_variable}; use std::{collections, env, ffi, fmt, fs, io, process}; @@ -141,8 +142,8 @@ impl EnvironmentMap { write_variable!( w, "FEATURES", - format!("[&str; {}]", features.len()), - format!("{features:?}"), + format_args!("[&str; {}]", features.len()), + ArrayDisplay(&features, |t, f| write!(f, "\"{}\"", t.escape_default())), "The features that were enabled during compilation." ); let features_str = features.join(", "); @@ -162,8 +163,12 @@ impl EnvironmentMap { write_variable!( w, "FEATURES_LOWERCASE", - format!("[&str; {}]", lowercase_features.len()), - format!("{lowercase_features:?}"), + format_args!("[&str; {}]", lowercase_features.len()), + ArrayDisplay(&lowercase_features, |val, fmt| write!( + fmt, + "\"{}\"", + val.escape_default() + )), "The features as above, as lowercase strings." ); let lowercase_features_str = lowercase_features.join(", "); @@ -237,13 +242,21 @@ impl EnvironmentMap { let rustc_version = get_version_from_cmd(rustc.as_ref())?; let rustdoc_version = get_version_from_cmd(rustdoc.as_ref()).unwrap_or_default(); - let doc = format!("The output of `{rustc} -V`"); - write_str_variable!(w, "RUSTC_VERSION", rustc_version, doc); + write_str_variable!( + w, + "RUSTC_VERSION", + rustc_version, + format_args!("The output of `{rustc} -V`") + ); - let doc = format!( - "The output of `{rustdoc} -V`; empty string if `{rustdoc} -V` failed to execute" + write_str_variable!( + w, + "RUSTDOC_VERSION", + rustdoc_version, + format_args!( + "The output of `{rustdoc} -V`; empty string if `{rustdoc} -V` failed to execute" + ) ); - write_str_variable!(w, "RUSTDOC_VERSION", rustdoc_version, doc); Ok(()) } diff --git a/src/git.rs b/src/git.rs index 1b0c526b1..13d93cf50 100644 --- a/src/git.rs +++ b/src/git.rs @@ -202,7 +202,7 @@ mod tests { let commit_oid_short = binding.as_str().unwrap(); - let commit_hash = format!("{commit_oid}"); + let commit_hash = commit_oid.to_string(); let commit_hash_short = commit_oid_short.to_string(); assert!(commit_hash.starts_with(&commit_hash_short)); @@ -285,7 +285,7 @@ mod tests { let commit_oid_short = binding.as_str().unwrap(); - let commit_hash = format!("{commit_oid}"); + let commit_hash = commit_oid.to_string(); let commit_hash_short = commit_oid_short.to_string(); assert!(commit_hash.starts_with(&commit_hash_short)); diff --git a/src/lib.rs b/src/lib.rs index f66ad5a95..20b6dbf0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -336,7 +336,7 @@ macro_rules! write_str_variable { $writer, $name, "&str", - format!("r\"{}\"", $value.escape_default()), + format_args!("\"{}\"", $value.escape_default()), $doc ); }; diff --git a/src/util.rs b/src/util.rs index 67d835e9b..0991ab994 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,8 @@ //! Various convenience functions for `built` at runtime. +use std::fmt; +use std::fmt::Write; + #[cfg(feature = "git2")] pub use crate::git::{get_repo_description, get_repo_head}; @@ -55,3 +58,45 @@ where pub fn detect_ci() -> Option { crate::environment::EnvironmentMap::new().detect_ci() } + +pub(crate) struct ArrayDisplay<'a, T, F>(pub &'a [T], pub F) +where + F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result; + +impl fmt::Display for ArrayDisplay<'_, T, F> +where + F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_char('[')?; + for (i, v) in self.0.iter().enumerate() { + if i != 0 { + f.write_str(", ")?; + } + (self.1)(v, f)?; + } + f.write_char(']') + } +} + +#[cfg(feature = "cargo-lock")] +pub(crate) struct TupleArrayDisplay<'a, T>(pub &'a [(T, T)]); + +#[cfg(feature = "cargo-lock")] +impl fmt::Display for TupleArrayDisplay<'_, T> +where + T: AsRef, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + ArrayDisplay(self.0, |(a, b), fmt| write!( + fmt, + r#"("{}", "{}")"#, + a.as_ref().escape_default(), + b.as_ref().escape_default() + )) + ) + } +} diff --git a/tests/testbox_tests.rs b/tests/testbox_tests.rs index f4b2a2908..d5eb31063 100644 --- a/tests/testbox_tests.rs +++ b/tests/testbox_tests.rs @@ -46,8 +46,9 @@ version = "0.0.1" build = "build.rs" [build-dependencies] -built = {{ path = {:?}, features = {} }}"#, - &built_root, &features, +built = {{ path = "{}", features = {} }}"#, + built_root.display().to_string().escape_default(), + &features, ), ) .add_file( @@ -167,7 +168,7 @@ repository = "https://dev.example.com/sources/testbox/" license = "MIT" [build-dependencies] -built = {{ path = {:?}, default_features=false }} +built = {{ path = "{}", default_features=false }} [profile.dev] panic = "abort" @@ -179,7 +180,7 @@ panic = "abort" default = ["SuperAwesome", "MegaAwesome"] SuperAwesome = [] MegaAwesome = []"#, - &built_root, + built_root.display().to_string().escape_default(), ), ) .add_file( @@ -237,16 +238,16 @@ repository = "https://dev.example.com/sources/testbox/" license = "MIT" [dependencies] -built = {{ path = {:?}, default_features=false }} +built = {{ path = "{built_root}", default_features=false }} [build-dependencies] -built = {{ path = {:?}, default_features=false }} +built = {{ path = "{built_root}", default_features=false }} [features] default = ["SuperAwesome", "MegaAwesome"] SuperAwesome = [] MegaAwesome = []"#, - &built_root, &built_root + built_root = built_root.display().to_string().escape_default() ), ); @@ -327,8 +328,8 @@ version = "5.6.7" build = "build.rs" [build-dependencies] -built = {{ path = {:?} }}"#, - &built_root +built = {{ path = "{}" }}"#, + built_root.display().to_string().escape_default() ), ); p.add_file( @@ -371,16 +372,16 @@ repository = "https://dev.example.com/sources/testbox/" license = "MIT" [dependencies] -built = {{ path = {:?}, features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} +built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} [build-dependencies] -built = {{ path = {:?}, features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} +built = {{ path = "{built_root}", features=["cargo-lock", "dependency-tree", "git2", "chrono", "semver"] }} [features] default = ["SuperAwesome", "MegaAwesome"] SuperAwesome = [] MegaAwesome = []"#, - &built_root, &built_root + built_root = built_root.display().to_string().escape_default() ), ); @@ -485,11 +486,11 @@ build = "build.rs" description = "xobtset" [dependencies] -built = {{ path = {:?}, features=["chrono"] }} +built = {{ path = "{built_root}", features=["chrono"] }} [build-dependencies] -built = {{ path = {:?}, features=["chrono"] }}"#, - &built_root, &built_root +built = {{ path = "{built_root}", features=["chrono"] }}"#, + built_root = built_root.display().to_string().escape_default() ), );