quoting possible booleans

fixes #53
This commit is contained in:
Hendrik Sollich 2017-05-22 19:30:01 +02:00
parent b5f5b66d6e
commit 3fa9a24739
2 changed files with 48 additions and 12 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "yaml-rust"
version = "0.3.5"
version = "0.3.6"
authors = ["Yuheng Chen <yuhengchen@sensetime.com>"]
homepage = "http://chyh1990.github.io/yaml-rust/"
documentation = "http://chyh1990.github.io/yaml-rust/doc/yaml_rust/"

View file

@ -137,15 +137,12 @@ impl<'a> YamlEmitter<'a> {
Yaml::Array(ref v) => {
try!(write!(self.writer, "["));
if self.level >= 0 {
try!(write!(self.writer, "+ "));
try!(write!(self.writer, ""));
}
self.level += 1;
for (cnt, x) in v.iter().enumerate() {
try!(self.write_indent());
if cnt > 0 { try!(write!(self.writer, ", ")); }
try!(self.emit_node(x));
}
self.level -= 1;
try!(write!(self.writer, "]"));
Ok(())
},
@ -181,8 +178,7 @@ impl<'a> YamlEmitter<'a> {
Yaml::String(ref v) => {
if need_quotes(v) {
try!(escape_str(self.writer, v));
}
else {
} else {
try!(write!(self.writer, "{}", v));
}
Ok(())
@ -306,10 +302,13 @@ fn need_quotes(string: &str) -> bool {
_ => false,
}
})
|| string == "true"
|| string == "false"
|| string == "null"
|| string == "~"
|| [// http://yaml.org/type/bool.html
"y","Y","yes","Yes","YES","n","N","no","No","NO",
"True", "TRUE", "true", "False", "FALSE", "false",
"on","On","ON","off","Off","OFF",
// http://yaml.org/type/null.html
"null","Null","NULL", "~"
].contains(&string)
|| string.starts_with('.')
|| string.parse::<i64>().is_ok()
|| string.parse::<f64>().is_ok()
@ -421,7 +420,8 @@ products:
"true": bool key
"{}": empty hash key
x: test
y: string with spaces"#;
"y": "can't avoid quoting here"
z: string with spaces"#;
let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
@ -434,6 +434,42 @@ y: string with spaces"#;
assert_eq!(s, writer, "actual:\n\n{}\n", writer);
}
#[test]
fn emit_quoted_bools() {
let input = r#"---
string0: yes
string1: no
string2: "true"
string3: "false"
string4: "~"
null0: ~
[true, false]: real_bools
[True, TRUE, False, FALSE, y,Y,yes,Yes,YES,n,N,no,No,NO,on,On,ON,off,Off,OFF]: false_bools
bool0: true
bool1: false"#;
let expected = r#"---
string0: "yes"
string1: "no"
string2: "true"
string3: "false"
string4: "~"
null0: ~
[true, false]: real_bools
["True", "TRUE", "False", "FALSE", "y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO", "on", "On", "ON", "off", "Off", "OFF"]: false_bools
bool0: true
bool1: false"#;
let docs = YamlLoader::load_from_str(&input).unwrap();
let doc = &docs[0];
let mut writer = String::new();
{
let mut emitter = YamlEmitter::new(&mut writer);
emitter.dump(doc).unwrap();
}
assert_eq!(expected, writer, "actual:\n\n{}\n", writer);
}
#[test]
fn test_empty_and_nested() {
let s = r#"---