chyh1990 / yaml-rust

A pure rust YAML implementation.
Apache License 2.0
611 stars 140 forks source link

`Parser` parses empty scalar as '~' #182

Open xnuk opened 2 years ago

xnuk commented 2 years ago

yaml-rust parses empty scalar as ~:

use yaml_rust::parser::{Event, EventReceiver, Parser};

struct Printer;
impl EventReceiver for Printer {
    fn on_event(&mut self, ev: Event) {
        println!("{:?}", ev);
    }
}

let text: &str = r#"
foo:
bar: ~
"#

Parser::new(text.chars()).load(&mut Recv, false)?;

/* output:
StreamStart
DocumentStart
MappingStart(0)
Scalar("foo", Plain, 0, None)
Scalar("~", Plain, 0, None)
Scalar("bar", Plain, 0, None)
Scalar("~", Plain, 0, None)
MappingEnd
DocumentEnd
*/

while libyaml's yaml_parser_parse does not (I did FFI by myself):

StreamStart
DocumentStart
MappingStart { anchor: None }
Scalar { anchor: None, value: "foo" }
Scalar { anchor: None, value: "" }
Scalar { anchor: None, value: "bar" }
Scalar { anchor: None, value: "~" }
MappingEnd
DocumentEnd
StreamEnd

Note: tag:yaml.org,2002:null is not in Failsafe schema^failsafe (and is in JSON schema^json).

xnuk commented 2 years ago

Made test case for this: #183