@@ -60,10 +60,10 @@ fn find_repo_root(start: &Path) -> Option<PathBuf> {
6060/// Write `contents` to `path`, but only if the file doesn't already exist
6161/// with identical content. Avoids triggering unnecessary rebuilds.
6262fn write_if_changed ( path : & Path , contents : & [ u8 ] ) -> io:: Result < ( ) > {
63- if let Ok ( existing) = fs:: read ( path) {
64- if existing == contents {
65- return Ok ( ( ) ) ;
66- }
63+ if let Ok ( existing) = fs:: read ( path)
64+ && existing == contents
65+ {
66+ return Ok ( ( ) ) ;
6767 }
6868 if let Some ( parent) = path. parent ( ) {
6969 fs:: create_dir_all ( parent) ?;
@@ -193,48 +193,49 @@ fn main() {
193193 let out_dir = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
194194 let dest = out_dir. join ( "build_info.rs" ) ;
195195
196+ // Pre-format values as Rust literals to avoid nested format!() calls.
197+ let version_string_lit = format ! ( "{:?}" , version_string) ;
198+ let pkg_version_lit = format ! ( "{:?}" , pkg_version) ;
199+ let git_describe_lit = opt_str ( & git_describe) ;
200+ let git_sha_lit = opt_str ( & git_sha) ;
201+ let git_sha_short_lit = opt_str ( & git_sha_short) ;
202+ let git_branch_lit = opt_str ( & git_branch) ;
203+ let git_dirty_lit = match git_dirty {
204+ Some ( b) => format ! ( "Some({b})" ) ,
205+ None => "None" . to_string ( ) ,
206+ } ;
207+
196208 let rs = format ! (
197209 r#"// @generated by build.rs — do not edit.
198210
199211/// Composite version string for display. Prefers `git describe` output
200212/// when available, falls back to `CARGO_PKG_VERSION` + short SHA, or just
201213/// `CARGO_PKG_VERSION` if no git info is available at all.
202- pub const VERSION: &str = {version_string };
214+ pub const VERSION: &str = {version_string_lit };
203215
204216/// Cargo package version from Cargo.toml (always present).
205- pub const PKG_VERSION: &str = {pkg_version };
217+ pub const PKG_VERSION: &str = {pkg_version_lit };
206218
207219/// Full output of `git describe --tags --dirty --always`, if available.
208220/// Examples: "v0.3.1", "v0.3.1-7-gabcdef1", "v0.3.1-dirty", "abcdef1".
209- pub const GIT_DESCRIBE: Option<&str> = {git_describe };
221+ pub const GIT_DESCRIBE: Option<&str> = {git_describe_lit };
210222
211223/// Full commit SHA, if available.
212- pub const GIT_SHA: Option<&str> = {git_sha };
224+ pub const GIT_SHA: Option<&str> = {git_sha_lit };
213225
214226/// Abbreviated commit SHA, if available.
215- pub const GIT_SHA_SHORT: Option<&str> = {git_sha_short };
227+ pub const GIT_SHA_SHORT: Option<&str> = {git_sha_short_lit };
216228
217229/// Current branch name, if on a branch (None for detached HEAD).
218- pub const GIT_BRANCH: Option<&str> = {git_branch };
230+ pub const GIT_BRANCH: Option<&str> = {git_branch_lit };
219231
220232/// Whether the working tree had uncommitted changes at build time.
221- pub const GIT_DIRTY: Option<bool> = {git_dirty };
233+ pub const GIT_DIRTY: Option<bool> = {git_dirty_lit };
222234
223235/// Year for copyright notices. From the git commit date when available,
224236/// otherwise the calendar year at build time.
225237pub const BUILD_YEAR: u16 = {build_year};
226238"# ,
227- version_string = format!( "{:?}" , version_string) ,
228- pkg_version = format!( "{:?}" , pkg_version) ,
229- git_describe = opt_str( & git_describe) ,
230- git_sha = opt_str( & git_sha) ,
231- git_sha_short = opt_str( & git_sha_short) ,
232- git_branch = opt_str( & git_branch) ,
233- git_dirty = match git_dirty {
234- Some ( b) => format!( "Some({b})" ) ,
235- None => "None" . to_string( ) ,
236- } ,
237- build_year = build_year,
238239 ) ;
239240
240241 write_if_changed ( & dest, rs. as_bytes ( ) ) . expect ( "failed to write build_info.rs" ) ;
0 commit comments