Enable RON extensions
master#!enable(...) syntax. Extensions allow overriding default grammar rules. The syntax requires the #! prefix followed by the enable keyword and a comma-separated list of extension names inside parentheses.repository·master·Indexed 26 days ago
https://github.com/ron-rs/ronA serialization format designed as a human-readable and expressive alternative to JSON, specifically tailored for the Rust ecosystem. It supports Rust-native concepts such as enums, structs, and ranges. The ron crate provides tools for serializing Rust types to RON strings and deserializing from strings, bytes, or readers, with support for configurable extensions like implicit_some and explicit_struct_names.
#!enable(...) syntax. Extensions allow overriding default grammar rules. The syntax requires the #! prefix followed by the enable keyword and a comma-separated list of extension names inside parentheses.You can enable specific RON features by adding the #![enable(...)] attribute at the top of your RON document.
Additionally, this crate ignores the following attributes used by other tools like ron2 for compatibility:
#![type = "path::To::Type"]#![schema = "./path/to/schema.ron"]The RON serializer supports a compact numeric range syntax for certain struct fields. If a struct's fields match specific numeric range patterns, the serializer will emit a compact range instead of a standard struct representation.
Supported range types and their emitted separators:
RangeFrom: Emits start ..RangeTo or RangeToInclusive: Emits .. end or ..= endRange or RangeInclusive: Emits start .. end or start ..= endIf the fields do not strictly match these numeric patterns (e.g., if a non-numeric value is provided or a key mismatch occurs), the serializer automatically falls back to standard struct serialization.
path_meta within the PrettyConfig. When a field has associated documentation, the serializer will prepend /// to each line of the documentation and indent it according to the current nesting level.r prefix, followed by zero or more # characters, a quotation mark, the string content, a quotation mark, and the same number of # characters used at the start.br. They are useful when the content is not required to be valid UTF-8. They follow the same # delimiter rules as raw strings.Adding #![enable(implicit_some)] to your RON document enables automatic conversion of values to Some(value) if the target type is an Option.
When enabled, RON eagerly matches None and Some(..) even for nested options like Option<Option<Option<u32>>>:
5 -> Some(Some(Some(5)))None -> NoneSome(5) -> Some(Some(Some(5)))Some(None) -> Some(None)#![enable(implicit_some)]
(
value: 5,
)To avoid repeating #![enable(explicit_struct_names)] in every RON file, you can configure the RON parser to expect explicit struct names by default using the Options API.
use ron::extensions::Extensions;
use ron::options::Options;
let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES);
let foo: Foo = options.from_str(file_contents)?;Adding #![enable(unwrap_variant_newtypes)] to your RON document enables automatic unwrapping of the first structural layer inside a newtype enum variant.
Warning: When this extension is enabled, the first layer inside a newtype variant will always be unwrapped. It becomes impossible to write the explicit tuple/struct wrapper for that first layer (e.g., A(Inner(a: 4)) is no longer valid; you must use A(a: 4)).
#![enable(unwrap_variant_newtypes)]
(
variant: A(a: 4, b: true),
)Adding #![enable(unwrap_newtypes)] to your RON document allows RON to automatically unwrap simple tuple newtypes. This allows you to provide the inner value directly instead of wrapping it in a tuple.
Example:
For a struct struct NewType(u32);, instead of writing (5), you can simply write 5.
#![enable(unwrap_newtypes)]
(
new_type: 5,
)The explicit_struct_names extension affects both serialization and deserialization:
Foo(bar: Bar(42))).ExpectedStructName error.If you are parsing many files, instead of adding the attribute to every file, use Options::with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES) to enable it globally for the parser.
use ron::extensions::Extensions;
use ron::options::Options;
// Setup the options
let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES);
// Retrieve the contents of the file
let file_contents: &str = /* ... */;
// Parse the file's contents
let foo: Foo = options.from_str(file_contents)?;When serializing data to RON, you can control the output format using PrettyConfig. The serializer uses these settings to determine how to handle indentation, newlines, and compact representations for different data structures.
Key configuration behaviors observed in the serializer include:
indent level is within the configured depth_limit.compact_arrays, compact_maps, and compact_structs flags to decide whether to use multi-line formatting or single-line compact syntax.separator (e.g., a space) is used between keys and values in maps and structs, and between elements in sequences, depending on whether the structure is in compact mode.