r/learnrust • u/BlossomingBeelz • 2h ago
Do you use Ron? (Rusty Object Notation)
This is just an FYI post, as I learned about Ron way too late, and I think it's an awesome crate. It's a static file (.ron) data format that lets you store/load information very similarly to how you would write the data in rust itself, using serde to directly serialize and deserialize data into existing structs.
Example from its repo: Your .ron file:
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,
),
)
Supported types from its repo:
- Numbers:
42,3.14,0xFF,0b0110 - Strings:
"Hello","with\\escapes\n",r#"raw string, great for regex\."# - Byte Strings:
b"Hello",b"with \x65\x73\x63\x61\x70\x65\x73\n",br#"raw, too"# - Booleans:
true,false - Chars:
'e','\n' - Optionals:
Some("string"),Some(Some(1.34)),None - Tuples:
("abc", 1.23, true),() - Lists:
["abc", "def"] - Structs:
( foo: 1.0, bar: ( baz: "I'm nested" ) ) - Enums:
Unit,Newtype(1.5),Tuple(1, "two"),Struct(x: 1.0, y: 2.0) - Maps:
{ "arbitrary": "keys", "are": "allowed" } - Ranges:
3..5,-2.1..=7.3,..-15,..10.5,..=13,..
IMO it takes away a lot of the tedious parsing from other formats, especially if you're dealing with optional values and enums.
[Obligatory I am not affiliated and this is not vibeslop, just a recommendation]