Skip to content

Commit 5209ac1

Browse files
authored
Merge pull request #3157 from matthiaskrgr/Vinfo_impl_debug
impl std::fmt::Debug for VersionInfo
2 parents f30cf51 + 61249b3 commit 5209ac1

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

rustc_tools_util/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ impl std::fmt::Display for VersionInfo {
6161
}
6262
}
6363

64+
impl std::fmt::Debug for VersionInfo {
65+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
66+
write!(
67+
f,
68+
"VersionInfo {{ crate_name: \"{}\", major: {}, minor: {}, patch: {}",
69+
self.crate_name, self.major, self.minor, self.patch,
70+
)?;
71+
match self.commit_hash {
72+
Some(_) => {
73+
write!(
74+
f,
75+
", commit_hash: \"{}\", commit_date: \"{}\" }}",
76+
self.commit_hash.clone().unwrap_or_default().trim(),
77+
self.commit_date.clone().unwrap_or_default().trim()
78+
)?;
79+
},
80+
None => {
81+
write!(f, " }}")?;
82+
},
83+
}
84+
Ok(())
85+
}
86+
}
87+
6488
pub fn get_channel() -> Option<String> {
6589
if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
6690
Some(channel)
@@ -105,8 +129,17 @@ mod test {
105129
#[test]
106130
fn test_display_local() {
107131
let vi = get_version_info!();
108-
let fmt = format!("{}", vi);
109-
assert_eq!(fmt, "rustc_tools_util 0.1.0");
132+
assert_eq!(vi.to_string(), "rustc_tools_util 0.1.0");
133+
}
134+
135+
#[test]
136+
fn test_debug_local() {
137+
let vi = get_version_info!();
138+
let s = format!("{:?}", vi);
139+
assert_eq!(
140+
s,
141+
"VersionInfo { crate_name: \"rustc_tools_util\", major: 0, minor: 1, patch: 0 }"
142+
);
110143
}
111144

112145
}

0 commit comments

Comments
 (0)