Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #383 from dtolnay/leadingzero
Browse files Browse the repository at this point in the history
Use quoted style for strings consisting of digits with leading zero
  • Loading branch information
dtolnay authored Jul 20, 2023
2 parents c1b1eac + a38768f commit fda96c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,12 @@ where
Ok(ScalarStyle::SingleQuoted)
}

fn visit_str<E>(self, _v: &str) -> Result<Self::Value, E> {
Ok(ScalarStyle::Any)
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> {
Ok(if crate::de::digits_but_not_number(v) {
ScalarStyle::SingleQuoted
} else {
ScalarStyle::Any
})
}

fn visit_unit<E>(self) -> Result<Self::Value, E> {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,19 @@ fn test_strings_needing_quote() {
boolean: String,
integer: String,
void: String,
leading_zeros: String,
}
let thing = Struct {
boolean: "true".to_owned(),
integer: "1".to_owned(),
void: "null".to_owned(),
leading_zeros: "007".to_owned(),
};
let yaml = indoc! {r#"
boolean: 'true'
integer: '1'
void: 'null'
leading_zeros: '007'
"#};
test_serde(&thing, yaml);
}
Expand Down

0 comments on commit fda96c7

Please sign in to comment.