ron-rs / ron

Rusty Object Notation
https://docs.rs/ron
Apache License 2.0
3.38k stars 124 forks source link
configs data-format rust serde serialization

Rusty Object Notation

MSRV Crates.io Docs

CI Coverage Fuzzing

Matrix

RON is a simple readable data serialization format that looks similar to Rust syntax. It's designed to support all of Serde's data model, so structs, enums, tuples, arrays, generic maps, and primitive values.

Example

GameConfig( // optional struct name
    window_size: (800, 600),
    window_title: "PAC-MAN",
    fullscreen: false,

    mouse_sensitivity: 1.4,
    key_bindings: {
        "up": Up,
        "down": Down,
        "left": Left,
        "right": Right,

        // Uncomment to enable WASD controls
        /*
        "W": Up,
        "S": Down,
        "A": Left,
        "D": Right,
        */
    },

    difficulty_options: (
        start_difficulty: Easy,
        adaptive: false,
    ),
)

RON syntax overview

Note: Serde's data model represents fixed-size Rust arrays as tuple (instead of as list)

RON also supports several extensions, which are documented here.

Specification

RON's formal and complete grammar is available here.

There also is a very basic, work in progress specification available on the wiki page.

Why RON?

Example in JSON

{
   "materials": {
        "metal": {
            "reflectivity": 1.0
        },
        "plastic": {
            "reflectivity": 0.5
        }
   },
   "entities": [
        {
            "name": "hero",
            "material": "metal"
        },
        {
            "name": "monster",
            "material": "plastic"
        }
   ]
}

Same example in RON

Scene( // class name is optional
    materials: { // this is a map
        "metal": (
            reflectivity: 1.0,
        ),
        "plastic": (
            reflectivity: 0.5,
        ),
    },
    entities: [ // this is an array
        (
            name: "hero",
            material: "metal",
        ),
        (
            name: "monster",
            material: "plastic",
        ),
    ],
)

Note the following advantages of RON over JSON:

Quickstart

Cargo.toml

[dependencies]
ron = "0.8"
serde = { version = "1", features = ["derive"] }

main.rs

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct MyStruct {
    boolean: bool,
    float: f32,
}

fn main() {
    let x: MyStruct = ron::from_str("(boolean: true, float: 1.23)").unwrap();

    println!("RON: {}", ron::to_string(&x).unwrap());

    println!("Pretty RON: {}", ron::ser::to_string_pretty(
        &x, ron::ser::PrettyConfig::default()).unwrap(),
    );
}

Tooling

Editor Plugin
IntelliJ intellij-ron
VS Code a5huynh/vscode-ron
Sublime Text RON
Atom language-ron
Vim ron-rs/ron.vim
EMACS emacs-ron

Limitations

RON is not designed to be a fully self-describing format (unlike JSON) and is thus not guaranteed to work when deserialize_any is used instead of its typed alternatives. In particular, the following Serde attributes only have limited support:

While data structures with any of these attributes should generally roundtrip through RON, some restrictions apply [^serde-restrictions] and their textual representation may not always match your expectation:

Furthermore, serde imposes the following restrictions for data to roundtrip:

Please file a new issue if you come across a use case which is not listed among the above restrictions but still breaks.

While RON guarantees roundtrips like Rust -> RON -> Rust for Rust types using non-deserialize_any-based implementations, RON does not yet make any guarantees about roundtrips through ron::Value. For instance, even when RON -> Rust works, RON -> ron::Value -> Rust, or RON -> ron::Value -> RON -> Rust may not work. We plan on improving ron::Value in an upcoming version of RON, though this work is partially blocked on serde#1183.

[^serde-enum-hack]: Deserialising an internally, adjacently, or un-tagged enum requires detecting serde's internal serde::__private::de::content::Content content type so that RON can describe the deserialised data structure in serde's internal JSON-like format. This detection only works for the automatically-derived Deserialize impls on enums. See #451 for more details.

[^serde-flatten-hack]: Deserialising a flattened struct from a map requires that the struct's Visitor::expecting implementation formats a string starting with "struct ". This is the case for automatically-derived Deserialize impls on structs. See #455 for more details.

[^serde-restrictions]: Most of these restrictions are currently blocked on serde#1183, which limits non-self-describing formats from roundtripping format-specific information through internally (or adjacently) tagged or untagged enums or #[serde(flatten)]ed fields.

License

RON is dual-licensed under Apache-2.0 and MIT.

Any contribution intentionally submitted for inclusion in the work must be provided under the same dual-license terms.