diff --git a/saphyr/Cargo.toml b/saphyr/Cargo.toml index 0c79598..a4e2fda 100644 --- a/saphyr/Cargo.toml +++ b/saphyr/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yaml-rust" -version = "0.3.5" +version = "0.3.6" authors = ["Yuheng Chen "] homepage = "http://chyh1990.github.io/yaml-rust/" documentation = "http://chyh1990.github.io/yaml-rust/doc/yaml_rust/" diff --git a/saphyr/src/emitter.rs b/saphyr/src/emitter.rs index 45f2024..91c94b2 100644 --- a/saphyr/src/emitter.rs +++ b/saphyr/src/emitter.rs @@ -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::().is_ok() || string.parse::().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#"---