This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from dtolnay/debug
Handwrite a better Debug for Value
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::value::{Number, Value}; | ||
use std::fmt::{self, Debug, Display}; | ||
|
||
impl Debug for Value { | ||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Value::Null => formatter.write_str("Null"), | ||
Value::Bool(boolean) => write!(formatter, "Bool({})", boolean), | ||
Value::Number(number) => write!(formatter, "Number({})", number), | ||
Value::String(string) => write!(formatter, "String({:?})", string), | ||
Value::Sequence(sequence) => { | ||
formatter.write_str("Sequence ")?; | ||
formatter.debug_list().entries(sequence).finish() | ||
} | ||
Value::Mapping(mapping) => { | ||
formatter.write_str("Mapping ")?; | ||
let mut debug = formatter.debug_map(); | ||
for (k, v) in mapping { | ||
let tmp; | ||
debug.entry( | ||
match k { | ||
Value::Bool(boolean) => boolean, | ||
Value::Number(number) => { | ||
tmp = DisplayNumber(number); | ||
&tmp | ||
} | ||
Value::String(string) => string, | ||
_ => k, | ||
}, | ||
v, | ||
); | ||
} | ||
debug.finish() | ||
} | ||
Value::Tagged(tagged) => Debug::fmt(tagged, formatter), | ||
} | ||
} | ||
} | ||
|
||
struct DisplayNumber<'a>(&'a Number); | ||
|
||
impl<'a> Debug for DisplayNumber<'a> { | ||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
Display::fmt(self.0, formatter) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters