Expand description
RON derive macros for serialization, deserialization, and schema generation.
This crate provides derive macros:
#[derive(Ron)]- Implements ToRon, FromRon, and RonSchema (all three)#[derive(ToRon)]- Serialize to RON without serde#[derive(FromRon)]- Deserialize from RON without serde#[derive(RonSchema)]- Generate RON schema definitions
All derives share the #[ron(...)] attribute namespace.
§Example
ⓘ
use ron2_derive::Ron;
#[derive(Debug, PartialEq, Ron)]
/// Application configuration
struct AppConfig {
/// Server port
port: u16,
/// Optional hostname
#[ron(default)]
host: Option<String>,
}§Container Attributes
#[ron(rename = "Name")]- Rename the type in RON output#[ron(rename_all = "camelCase")]- Rename all fields (camelCase, snake_case, PascalCase, etc.)#[ron(deny_unknown_fields)]- Error on unknown fields during deserialization#[ron(transparent)]- Serialize/deserialize as the single inner field
§Field Attributes
#[ron(rename = "name")]- Rename this field#[ron(skip)]- Skip this field entirely#[ron(skip_serializing)]- Skip during serialization only#[ron(skip_deserializing)]- Skip during deserialization (use default)#[ron(default)]- UseDefault::default()if missing#[ron(default = "path::to::fn")]- Use custom default function#[ron(flatten)]- Flatten nested struct fields into parent#[ron(skip_serializing_if = "path::to::fn")]- Skip if predicate returns true#[ron(explicit)]- Require explicitSome(...)orNonefor Option fields#[ron(opt)]- Use default if missing, skip serialization if equals default
§Variant Attributes
#[ron(rename = "Name")]- Rename this variant#[ron(skip)]- Skip this variant
§Extension Behavior
§Implicit Some (Default)
Option<T> fields accept bare values without Some(...):
ⓘ
#[derive(FromRon)]
struct Config {
name: Option<String>,
}
// Accepts: (name: "Alice") or (name: Some("Alice")) or (name: None)§Explicit Option (#[ron(explicit)])
Require Some(...) or None syntax for disambiguation:
ⓘ
#[derive(FromRon)]
struct Config {
#[ron(explicit)]
value: Option<Option<bool>>,
}
// Requires: (value: Some(Some(true))) or (value: Some(None)) or (value: None)§Transparent Newtypes (#[ron(transparent)])
Single-field structs serialize as their inner type:
ⓘ
#[derive(FromRon, ToRon)]
#[ron(transparent)]
struct UserId(u64);
// Serializes as: 42
// Not as: UserId(42)