Yamlette crate - comprehensive YAML 1.2 processor


#1

Hi all!

I’ve been doing the project 4fun in my leisure time for almost 1.5 years (started doing it with Rust of version 1.2 back then). And finally I’m happy to announce it’s very first public version. (well, second actually, the first one is broken for 1.15.0, since I forgot to test it before publishing). Anyway, now Travis is happy :slight_smile:

That’s it: Yamlette - YAML 1.2 processor
Features:

  • YAML read and write supported
  • On read - complete support of YAML 1.2 specification (every single example from the YAML spec covered by a test)
  • On write - most of the basic features implemented already, but the goal is complete support of the spec
  • Multithreading - tokenizing data in one stream, parsing and building up staff in others.
  • For bravest people it’s possible not to use data collectors, but rather to intercept event emitter and use it as is
  • Modular, flexible and extensible architecture

Hopefully it’s very easy to use: there is a macro that describes data. Now I see that quite a similar idea has been implemented in serde_json::json!, however in Yamlette it’s not only for writing data, but for reading as well.

Here is an example:

#[macro_use]
extern crate yamlette;

fn main () {
    let yaml_string = yamlette! ( write ; [[ ["one", "two"] ]]).ok ().unwrap ();

    assert_eq! (yaml_string, "- one\n- two\n");

    yamlette! ( read ; yaml_string ; [[ [ (one:&str), (two:String) ] ]] );

    assert_eq! (one, Some ("one"));
    assert_eq! (two, Some (String::from ("two")));
}

Thank you reading this!
Happy to answer any questions if there are some.
Cheers


#2

Will this allow me to read and write YAML, while preserving any comments?


#3

Hi Dentad,

Well, at this stage of the project comments are not preserved by the reader. Even though it should not be troublesome to add such functionality, it’s not clear how comments should be represented in the data tree. If it would be a simple text (!!str), I’m not sure what use-case would require this instead of using a simple !!str node. If you could explain your use-case, I’d be glad to think about it :slight_smile:
Also, there is a requirement in YAML reference around comments which says they must be ignored by the processor - so, it’s highly possible there are no ones that keep comments as nodes.

About writing with comments - it is not implemented at the moment, although there are such plans for future versions.

Cheers


#4

For example if the yaml file were used as a config file, editable by users. You want to let them have the flexibility to add whatever comments they wish and have them preserved, while being able to load, change values, and re-save the file in your software. Even a defaults config file with large comments they user can delete as they see fit.

Refer to the python package ruamel, near fully backwards compatible with package yaml.


#5

I see. Yes, this is not backwards incompatible change, since it just extends YAML reference, not breaking it.
I agree, this is a valid use-case. If you’d like you might create an issue for this functionality on the yamlette github page. If not - I’ll do it later.

Thank you for the feedback anyway!