Skip to content

Commit

Permalink
Formatting without fmt::Debug (#76)
Browse files Browse the repository at this point in the history
* More efficient string formatting

* Fixed escaping

* Format without fmt::Debug dependency

* Test fmt-debug=none
  • Loading branch information
kornelski authored Dec 10, 2024
1 parent 9270bd2 commit e8b9e55
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 36 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::util::TupleArrayDisplay;
use crate::{write_str_variable, write_variable};
use std::{collections, fs, io, path};

Expand Down Expand Up @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down Expand Up @@ -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!(
Expand Down
31 changes: 22 additions & 9 deletions src/environment.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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(", ");
Expand All @@ -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(", ");
Expand Down Expand Up @@ -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(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ macro_rules! write_str_variable {
$writer,
$name,
"&str",
format!("r\"{}\"", $value.escape_default()),
format_args!("\"{}\"", $value.escape_default()),
$doc
);
};
Expand Down
45 changes: 45 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -55,3 +58,45 @@ where
pub fn detect_ci() -> Option<super::CIPlatform> {
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<T, F> 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<T> fmt::Display for TupleArrayDisplay<'_, T>
where
T: AsRef<str>,
{
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()
))
)
}
}
31 changes: 16 additions & 15 deletions tests/testbox_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"
Expand All @@ -179,7 +180,7 @@ panic = "abort"
default = ["SuperAwesome", "MegaAwesome"]
SuperAwesome = []
MegaAwesome = []"#,
&built_root,
built_root.display().to_string().escape_default(),
),
)
.add_file(
Expand Down Expand Up @@ -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()
),
);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
),
);

Expand Down Expand Up @@ -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()
),
);

Expand Down

0 comments on commit e8b9e55

Please sign in to comment.