Improve Debug impl for Config by using `debug_struct()`

Using the method `debug_struct()` of `fmt::Formatter` takes
care of the exact formatting for us. Additionally, it also handles
the "alternate" form of printing enabled with '#'. In the struct
case it prints the struct on multiple lines instead of just one.

Note that this commit changes the output slightly. Before, the
field `log_level` was printed with `log: {}`. I assumed this
is a simple oversight and replaced "log" with "log_level".
Additionally, the value of `environment` is now shown as a simple
struct field instead of being combined with the struct name.
This commit is contained in:
Lukas Kalbertodt 2017-09-04 00:57:52 +02:00
parent 237370533c
commit ec9f1618bb
1 changed files with 8 additions and 5 deletions

View File

@ -791,15 +791,18 @@ impl Config {
impl fmt::Debug for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Config[{}] {{ address: {}, port: {}, workers: {}, log: {:?}",
self.environment, self.address, self.port, self.workers,
self.log_level)?;
let mut s = f.debug_struct("Config");
s.field("environment", &self.environment);
s.field("address", &self.address);
s.field("port", &self.port);
s.field("workers", &self.workers);
s.field("log_level", &self.log_level);
for (key, value) in self.extras() {
write!(f, ", {}: {}", key, value)?;
s.field(key, &value);
}
write!(f, " }}")
s.finish()
}
}