ron2/schema/
mod.rs

1//! RON Schema - Schema types, validation, and storage for RON files.
2//!
3//! This crate provides:
4//! - Schema type definitions for representing Rust types
5//! - Trait-based schema system for custom types
6//! - Storage utilities for reading/writing schema files
7//! - Validation of RON values against schemas
8//!
9//! # Serialization and Deserialization
10//!
11//! The [`ToRon`] and [`FromRon`] traits (re-exported from `crate`) provide
12//! serde-independent serialization and deserialization for RON format:
13//!
14//! ```rust
15//! use crate::schema::{ToRon, FromRon};
16//!
17//! // Serialize (crate uses compact format without spaces)
18//! let values = vec![1, 2, 3];
19//! let ron_string = values.to_ron().unwrap();
20//! assert_eq!(ron_string, "[1,2,3]");
21//!
22//! // Deserialize
23//! let parsed: Vec<i32> = Vec::from_ron("[1, 2, 3]").unwrap();
24//! assert_eq!(parsed, vec![1, 2, 3]);
25//! ```
26//!
27//! # Trait-Based Schema System
28//!
29//! The schema system is built on the [`RonSchema`] trait:
30//!
31//! - [`RonSchema`] - Core trait for types representable in schemas
32//! - [`RonList`] - Marker trait for list/sequence-like types
33//! - [`RonMap`] - Marker trait for map/dictionary-like types
34//! - [`RonOptional`] - Marker trait for optional/nullable types
35//!
36//! ## `TypeRef` Canonicalization
37//!
38//! `RonSchema` derives emit `TypeRef` paths in a canonical form for consistent
39//! schema discovery and linking:
40//! - Generic parameters remain unqualified (e.g., `T`).
41//! - Standard collection types are normalized to short names (e.g., `Vec`, `HashMap`).
42//!
43//! ## Implementing Custom Types
44//!
45//! ```rust
46//! use crate::schema::{RonSchema, RonList, TypeKind};
47//!
48//! // A custom list type
49//! struct MyVec<T>(Vec<T>);
50//!
51//! impl<T: RonSchema> RonSchema for MyVec<T> {
52//!     fn type_kind() -> TypeKind {
53//!         TypeKind::List(Box::new(T::type_kind()))
54//!     }
55//! }
56//!
57//! // Mark it as a list type for additional type information
58//! impl<T: RonSchema> RonList for MyVec<T> {
59//!     type Element = T;
60//! }
61//! ```
62//!
63//! # Creating Schemas Manually
64//!
65//! ```rust
66//! use crate::schema::{Schema, TypeKind, Field};
67//!
68//! // Define a schema for a config struct
69//! let schema = Schema::with_doc(
70//!     "Application configuration",
71//!     TypeKind::Struct {
72//!         fields: vec![
73//!             Field::new("port", TypeKind::U16).with_doc("Server port"),
74//!             Field::optional("host", TypeKind::String).with_doc("Hostname"),
75//!         ],
76//!     },
77//! );
78//!
79//! // Serialize to RON using the ToRon trait
80//! use crate::schema::ToRon;
81//! let ron_str = schema.to_ron().unwrap();
82//! println!("{}", ron_str);
83//! ```
84
85pub mod collect;
86pub mod error;
87pub mod storage;
88pub mod traits;
89pub mod types;
90pub mod validation;
91
92// Re-export types (always available)
93// Re-export functions that require the derive feature
94#[cfg(feature = "derive")]
95pub use collect::write_schemas;
96pub use collect::{SchemaCatalog, SchemaEntry, collect_schemas};
97pub use error::{
98    PathSegment, Position, Result, SchemaError, Span, StorageError, ValidationError,
99    ValidationErrorKind, ValidationResult,
100};
101pub use storage::{SCHEMA_DIR_ENV, resolve_schema_dir, type_path_to_file_path};
102#[cfg(feature = "derive")]
103pub use storage::{find_schema, find_schema_in, read_schema, write_schema};
104pub use traits::{RonList, RonMap, RonOptional, RonSchema};
105pub use types::{Field, Schema, TypeKind, Variant, VariantKind};
106#[cfg(feature = "derive")]
107pub use validation::StorageResolver;
108pub use validation::{
109    AcceptAllResolver, SchemaResolver, validate, validate_expr, validate_expr_collect_all,
110    validate_expr_type, validate_expr_type_with_resolver, validate_expr_with_resolver,
111    validate_type, validate_type_with_resolver, validate_with_resolver,
112};
113
114// Re-export conversion traits from crate
115pub use crate::{AstMapAccess, FormatConfig, FromRon, ToRon};