Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce unneeded parens and newlines in Debug for Value #918

Merged
merged 1 commit into from Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/number.rs
Expand Up @@ -307,27 +307,15 @@ impl Display for Number {
impl Debug for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut debug = formatter.debug_tuple("Number");
match self.n {
N::PosInt(i) => {
debug.field(&i);
}
N::NegInt(i) => {
debug.field(&i);
}
N::Float(f) => {
debug.field(&f);
}
N::PosInt(_) | N::NegInt(_) => write!(formatter, "Number({})", self),
N::Float(f) => write!(formatter, "Number({:?})", f),
Comment on lines +311 to +312
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up in #919.

}
debug.finish()
}

#[cfg(feature = "arbitrary_precision")]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
.debug_tuple("Number")
.field(&format_args!("{}", self.n))
.finish()
write!(formatter, "Number({})", self)
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/value/mod.rs
Expand Up @@ -177,19 +177,17 @@ pub enum Value {
impl Debug for Value {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Null => formatter.debug_tuple("Null").finish(),
Value::Bool(v) => formatter.debug_tuple("Bool").field(v).finish(),
Value::Number(v) => Debug::fmt(v, formatter),
Value::String(v) => formatter.debug_tuple("String").field(v).finish(),
Value::Array(v) => {
formatter.write_str("Array(")?;
Debug::fmt(v, formatter)?;
formatter.write_str(")")
Value::Null => formatter.write_str("Null"),
Value::Bool(boolean) => write!(formatter, "Bool({})", boolean),
Value::Number(number) => Debug::fmt(number, formatter),
Value::String(string) => write!(formatter, "String({:?})", string),
Value::Array(vec) => {
formatter.write_str("Array ")?;
Debug::fmt(vec, formatter)
}
Value::Object(v) => {
formatter.write_str("Object(")?;
Debug::fmt(v, formatter)?;
formatter.write_str(")")
Value::Object(map) => {
formatter.write_str("Object ")?;
Debug::fmt(map, formatter)
}
}
}
Expand Down
32 changes: 12 additions & 20 deletions tests/debug.rs
Expand Up @@ -36,12 +36,12 @@ fn value_string() {

#[test]
fn value_array() {
assert_eq!(format!("{:?}", json!([])), "Array([])");
assert_eq!(format!("{:?}", json!([])), "Array []");
}

#[test]
fn value_object() {
assert_eq!(format!("{:?}", json!({})), "Object({})");
assert_eq!(format!("{:?}", json!({})), "Object {}");
}

#[test]
Expand All @@ -63,25 +63,17 @@ fn indented() {
"String": "...",
});
let expected = indoc! {r#"
Object({
"Array": Array([
Bool(
true,
),
]),
"Bool": Bool(
true,
),
"EmptyArray": Array([]),
"EmptyObject": Object({}),
Object {
"Array": Array [
Bool(true),
],
"Bool": Bool(true),
"EmptyArray": Array [],
"EmptyObject": Object {},
"Null": Null,
"Number": Number(
1,
),
"String": String(
"...",
),
})"#
"Number": Number(1),
"String": String("..."),
}"#
};
assert_eq!(format!("{:#?}", j), expected);
}