Expand description
Derive macros for rcman settings library.
This crate provides #[derive(SettingsSchema)] for automatically generating
settings schema implementations from Rust structs. It translates strongly-typed
native Rust definitions directly into runtime rcman::SettingMetadata, preventing bugs
and ensuring absolute schema correctness via compile-time semantic validation.
§Features
- Native Type Binding: Automatically translates
String,PathBuf, integers, floats,bool, andVec<T>into their correspondingrcman::SettingType. - Strict Verification: The macro prevents contradictory constraints at compile time (e.g.
min > maxoroptionsonbool). - Dynamic UI Metadata: Every unknown attribute literal (e.g.,
label = "Server") is automatically injected into the schema as customizable metadata. #[cfg]Forwarding: Safely obeys macro feature flags attached to struct fields.
§Usage
use rcman::DeriveSettingsSchema as SettingsSchema;
use serde::{Serialize, Deserialize};
#[derive(SettingsSchema, Default, Serialize, Deserialize)]
#[schema(category = "network")] // Required: sets the root prefix for the UI
struct NetworkSettings {
#[setting(rename = "server-auth-port")]
pub port: u16,
#[setting(rename = "enable_tls")]
pub tls: bool,
#[setting(rename = "server-url")]
pub url: String,
pub roles: Vec<String>,
}
fn main() {}§Attribute Reference
§Container Attributes (#[schema(...)])
Apply these directly to the struct.
| Attribute | Description | Required | Example |
|---|---|---|---|
category | The root grouping prefix used for all fields. | Yes | #[schema(category = "general")] |
§Field Attributes (#[setting(...)])
Apply these to individual struct fields.
| Attribute | Type Mapping | Description | Example |
|---|---|---|---|
rename | All | Overrides the field name when constructing the schema key (category.rename) | #[setting(rename = "App-Theme")] |
skip | All | Silently ignores the field; it will not appear in the settings schema | #[setting(skip)] |
secret | All | Asserts the field contains sensitive data, diverting it to the OS Keychain backing | #[setting(secret)] |
category | All | Overrides the container category specifically for this single field | #[setting(category = "overridden")] |
nested | Structs | Extracts the schema from an inner struct and flattens it upward | #[setting(nested)] |
min | Number | Sets a numeric minimum constraint (must be <= max) | #[setting(min = 1.0)] |
max | Number | Sets a numeric maximum constraint (must be >= min) | #[setting(max = 100.0)] |
step | Number | Defines valid increment stepping | #[setting(step = 5.0)] |
pattern | Text | Enforces standard Regex validation string | #[setting(pattern = "^[a-z]+$")] |
options | Text/Num | Enforces strict dropdown alternatives mappings | #[setting(options(("val", "Label")))] |
§Dynamic Metadata
Any key = value assignment in #[setting(...)] that isn’t functionally reserved above is transparently forwarded into the resulting SettingMetadata map for your UI components to access dynamically.
use rcman::DeriveSettingsSchema as SettingsSchema;
use serde::{Serialize, Deserialize};
#[derive(SettingsSchema, Default, Serialize, Deserialize)]
#[schema(category = "network")]
struct ServerSettings {
#[setting(
min = 1024, // 1. Reserved constraint
label = "Server Port", // 2. -> .meta_str("label", "Server Port")
order = 1, // 3. -> .meta_num("order", 1)
advanced = false // 4. -> .meta_bool("advanced", false)
)]
pub port: u16,
}
fn main() {}§Panics
This macro performs completely safe compile-time error reporting (yielding syn::Error) returning targeted IDE-friendly error underlines instead of panicking. It blocks:
- Setting
min/max/stepon non-numeric types (bool,Vec,String). - Setting
patternon non-Text types (bool,Vec,i32). - Unknown/Unsupported types missing
#[setting(skip)](e.g.DurationorHashMap) so that you never accidentally leak invalid config metadata to the UI.
Derive Macros§
- Settings
Schema - Derive macro for generating
SettingsSchemaimplementations. See the crate-level documentation for full attribute reference.