Rebased and resolved conflicts with the following:
https://github.com/chyh1990/yaml-rust/pull/66 https://github.com/chyh1990/yaml-rust/pull/62 (closed in favor of 66)
This commit is contained in:
parent
a9ab3ad82e
commit
ff8572352d
2 changed files with 85 additions and 49 deletions
|
@ -132,48 +132,6 @@ impl<'a> YamlEmitter<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit_node_compact(&mut self, node: &Yaml) -> EmitResult {
|
|
||||||
match *node {
|
|
||||||
Yaml::Array(ref v) => {
|
|
||||||
try!(write!(self.writer, "["));
|
|
||||||
if self.level >= 0 {
|
|
||||||
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(())
|
|
||||||
},
|
|
||||||
Yaml::Hash(ref h) => {
|
|
||||||
try!(self.writer.write_str("{"));
|
|
||||||
self.level += 1;
|
|
||||||
for (cnt, (k, v)) in h.iter().enumerate() {
|
|
||||||
if cnt > 0 {
|
|
||||||
try!(write!(self.writer, ", "));
|
|
||||||
}
|
|
||||||
match *k {
|
|
||||||
// complex key is not supported
|
|
||||||
Yaml::Array(_) | Yaml::Hash(_) => {
|
|
||||||
return Err(EmitError::BadHashmapKey);
|
|
||||||
},
|
|
||||||
_ => { try!(self.emit_node(k)); }
|
|
||||||
}
|
|
||||||
try!(write!(self.writer, ": "));
|
|
||||||
try!(self.emit_node(v));
|
|
||||||
}
|
|
||||||
try!(self.writer.write_str("}"));
|
|
||||||
self.level -= 1;
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
_ => self.emit_node(node),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn emit_node(&mut self, node: &Yaml) -> EmitResult {
|
fn emit_node(&mut self, node: &Yaml) -> EmitResult {
|
||||||
match *node {
|
match *node {
|
||||||
Yaml::Array(ref v) => self.emit_array(v),
|
Yaml::Array(ref v) => self.emit_array(v),
|
||||||
|
@ -236,28 +194,37 @@ impl<'a> YamlEmitter<'a> {
|
||||||
} else {
|
} else {
|
||||||
self.level += 1;
|
self.level += 1;
|
||||||
for (cnt, (k, v)) in h.iter().enumerate() {
|
for (cnt, (k, v)) in h.iter().enumerate() {
|
||||||
|
let complex_key = match *k {
|
||||||
|
Yaml::Hash(_) | Yaml::Array(_) => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
if cnt > 0 {
|
if cnt > 0 {
|
||||||
try!(write!(self.writer, "\n"));
|
try!(write!(self.writer, "\n"));
|
||||||
try!(self.write_indent());
|
try!(self.write_indent());
|
||||||
}
|
}
|
||||||
match *k {
|
if complex_key {
|
||||||
Yaml::Array(_) | Yaml::Hash(_) => {
|
try!(write!(self.writer, "? "));
|
||||||
try!(self.emit_node_compact(k));
|
self.level += 1;
|
||||||
}
|
try!(self.emit_node(k));
|
||||||
_ => {
|
self.level -= 1;
|
||||||
|
try!(write!(self.writer, "\n"));
|
||||||
|
try!(self.write_indent());
|
||||||
|
} else {
|
||||||
try!(self.emit_node(k));
|
try!(self.emit_node(k));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
match *v {
|
match *v {
|
||||||
Yaml::Array(ref v) => {
|
Yaml::Array(ref v) => {
|
||||||
|
if complex_key { self.level += 1; }
|
||||||
if v.is_empty() {
|
if v.is_empty() {
|
||||||
try!(write!(self.writer, ": "));
|
try!(write!(self.writer, ": "));
|
||||||
} else {
|
} else {
|
||||||
try!(write!(self.writer, ":\n"));
|
try!(write!(self.writer, ":\n"));
|
||||||
}
|
}
|
||||||
try!(self.emit_array(v));
|
try!(self.emit_array(v));
|
||||||
|
if complex_key { self.level -= 1; }
|
||||||
}
|
}
|
||||||
Yaml::Hash(ref h) => {
|
Yaml::Hash(ref h) => {
|
||||||
|
if complex_key { self.level += 1; }
|
||||||
if h.is_empty() {
|
if h.is_empty() {
|
||||||
try!(write!(self.writer, ": "));
|
try!(write!(self.writer, ": "));
|
||||||
} else {
|
} else {
|
||||||
|
@ -267,10 +234,13 @@ impl<'a> YamlEmitter<'a> {
|
||||||
self.level -= 1;
|
self.level -= 1;
|
||||||
}
|
}
|
||||||
try!(self.emit_hash(h));
|
try!(self.emit_hash(h));
|
||||||
|
if complex_key { self.level -= 1; }
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
if complex_key { self.level += 1; }
|
||||||
try!(write!(self.writer, ": "));
|
try!(write!(self.writer, ": "));
|
||||||
try!(self.emit_node(v));
|
try!(self.emit_node(v));
|
||||||
|
if complex_key { self.level -= 1; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,3 +73,69 @@ include!("spec_test.rs.inc");
|
||||||
//#[test]
|
//#[test]
|
||||||
//fn test_hc_alias() {
|
//fn test_hc_alias() {
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mapvec_legal() {
|
||||||
|
use yaml_rust::yaml::{Array, Hash, Yaml};
|
||||||
|
use yaml_rust::{YamlLoader, YamlEmitter};
|
||||||
|
|
||||||
|
// Emitting a `map<map<seq<_>>, _>` should result in legal yaml that
|
||||||
|
// we can parse.
|
||||||
|
|
||||||
|
let mut key = Array::new();
|
||||||
|
key.push(Yaml::Integer(1));
|
||||||
|
key.push(Yaml::Integer(2));
|
||||||
|
key.push(Yaml::Integer(3));
|
||||||
|
|
||||||
|
let mut keyhash = Hash::new();
|
||||||
|
keyhash.insert(Yaml::String("key".into()), Yaml::Array(key));
|
||||||
|
|
||||||
|
let mut val = Array::new();
|
||||||
|
val.push(Yaml::Integer(4));
|
||||||
|
val.push(Yaml::Integer(5));
|
||||||
|
val.push(Yaml::Integer(6));
|
||||||
|
|
||||||
|
let mut hash = Hash::new();
|
||||||
|
hash.insert(Yaml::Hash(keyhash), Yaml::Array(val));
|
||||||
|
|
||||||
|
let mut out_str = String::new();
|
||||||
|
{
|
||||||
|
let mut emitter = YamlEmitter::new(&mut out_str);
|
||||||
|
emitter.dump(&Yaml::Hash(hash)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point, we are tempted to naively render like this:
|
||||||
|
//
|
||||||
|
// ```yaml
|
||||||
|
// ---
|
||||||
|
// {key:
|
||||||
|
// - 1
|
||||||
|
// - 2
|
||||||
|
// - 3}:
|
||||||
|
// - 4
|
||||||
|
// - 5
|
||||||
|
// - 6
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// However, this doesn't work, because the key sequence [1, 2, 3] is
|
||||||
|
// rendered in block mode, which is not legal (as far as I can tell)
|
||||||
|
// inside the flow mode of the key. We need to either fully render
|
||||||
|
// everything that's in a key in flow mode (which may make for some
|
||||||
|
// long lines), or use the explicit map identifier '?':
|
||||||
|
//
|
||||||
|
// ```yaml
|
||||||
|
// ---
|
||||||
|
// ?
|
||||||
|
// key:
|
||||||
|
// - 1
|
||||||
|
// - 2
|
||||||
|
// - 3
|
||||||
|
// :
|
||||||
|
// - 4
|
||||||
|
// - 5
|
||||||
|
// - 6
|
||||||
|
// ```
|
||||||
|
|
||||||
|
YamlLoader::load_from_str(&out_str).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue