config_lib/lib.rs
1//! # config-lib - Multi-Format Configuration Library
2//!
3//! A high-performance configuration management library supporting multiple formats
4//! including CONF, NOML, TOML, and JSON with advanced features like format preservation,
5//! async operations, and schema validation.
6//!
7//! ## Quick Start
8//!
9//! ```rust
10//! use config_lib::Config;
11//!
12//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
13//! // Parse any supported format automatically
14//! let mut config = Config::from_string("port = 8080\nname = \"MyApp\"", None)?;
15//!
16//! // Access values with type safety
17//! let port = config.get("port").unwrap().as_integer()?;
18//! let name = config.get("name").unwrap().as_string()?;
19//!
20//! // Modify and save (preserves format and comments)
21//! config.set("port", 9000)?;
22//! # Ok(())
23//! # }
24//! ```
25//!
26//! ## Supported Formats
27//!
28//! - **CONF** - Built-in parser for standard .conf files (default)
29//! - **NOML** - Advanced configuration with dynamic features (feature: `noml`)
30//! - **TOML** - Standard TOML format with format preservation (feature: `toml`)
31//! - **JSON** - JSON format with edit capabilities (feature: `json`)
32//!
33//! ## Features
34//!
35//! - **🚀 High Performance** - Zero-copy parsing where possible
36//! - **💾 Format Preservation** - Maintains comments, whitespace, and formatting
37//! - **⚡ Async Native** - Full async/await support (feature: `async`)
38//! - **🔍 Schema Validation** - Type safety and validation (feature: `schema`)
39//! - **🌐 Cross Platform** - Linux, macOS, and Windows support
40//! - **🔧 Type Safety** - Rich type system with automatic conversions
41
42#![warn(missing_docs)]
43#![warn(clippy::all)]
44
45pub mod config;
46/// Enterprise-grade configuration management with advanced caching, performance optimizations,
47/// and multi-instance support. Provides thread-safe caching with `Arc<RwLock>` for high-concurrency
48/// environments and sub-50ns access times for cached values.
49pub mod enterprise; // Enterprise API with caching and performance
50pub mod error;
51pub mod parsers;
52pub mod value;
53
54#[cfg(feature = "schema")]
55pub mod schema;
56
57#[cfg(feature = "validation")]
58pub mod validation;
59
60/// Hot reloading system for zero-downtime configuration updates
61pub mod hot_reload;
62
63/// Comprehensive audit logging system for configuration operations
64pub mod audit;
65
66/// Environment variable override system for smart configuration overrides
67#[cfg(feature = "env-override")]
68pub mod env_override;
69
70// Re-export main types for convenience
71pub use config::{Config, ConfigBuilder, ConfigValue};
72pub use enterprise::{ConfigManager, EnterpriseConfig};
73pub use error::{Error, Result};
74pub use value::Value;
75
76#[cfg(feature = "schema")]
77pub use schema::{Schema, SchemaBuilder};
78
79#[cfg(feature = "validation")]
80pub use validation::{
81 ValidationError, ValidationResult, ValidationRule, ValidationRuleSet, ValidationSeverity,
82};
83
84use std::path::Path;
85
86/// Parse configuration from a string, auto-detecting format
87///
88/// This is the main entry point for parsing configuration data. The format
89/// is automatically detected based on the content structure and syntax.
90///
91/// # Arguments
92///
93/// * `source` - Configuration text to parse
94/// * `format` - Optional format hint (auto-detected if None)
95///
96/// # Returns
97///
98/// Returns a [`Value`] containing the parsed configuration data.
99///
100/// # Examples
101///
102/// ```rust
103/// use config_lib::parse;
104///
105/// let value = parse(r#"
106/// app_name = "my-service"
107/// port = 8080
108/// debug = true
109/// "#, Some("conf"))?;
110///
111/// // Access values from the parsed Value
112/// if let Ok(table) = value.as_table() {
113/// if let Some(app_name) = table.get("app_name") {
114/// assert_eq!(app_name.as_string().unwrap(), "my-service");
115/// }
116/// if let Some(port) = table.get("port") {
117/// assert_eq!(port.as_integer().unwrap(), 8080);
118/// }
119/// }
120///
121/// # Ok::<(), config_lib::Error>(())
122/// ```
123pub fn parse(source: &str, format: Option<&str>) -> Result<Value> {
124 parsers::parse_string(source, format)
125}
126
127/// Parse configuration from a file, auto-detecting format from extension
128///
129/// Reads a configuration file from disk and automatically detects the format
130/// based on the file extension (.conf, .toml, .json, .noml).
131///
132/// # Arguments
133///
134/// * `path` - Path to the configuration file
135///
136/// # Returns
137///
138/// Returns a [`Value`] containing the parsed configuration data.
139///
140/// # Examples
141///
142/// ```rust,no_run
143/// use config_lib::parse_file;
144///
145/// let config = parse_file("app.conf")?;
146/// let port = config.get("server.port").unwrap().as_integer()?;
147///
148/// # Ok::<(), config_lib::Error>(())
149/// ```
150pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Value> {
151 parsers::parse_file(path)
152}
153
154/// Validate configuration against a schema
155///
156/// Performs comprehensive validation of configuration data against a provided
157/// schema definition.
158///
159/// # Arguments
160///
161/// * `config` - Configuration value to validate
162/// * `schema` - Schema definition for validation
163///
164/// # Returns
165///
166/// Returns `Ok(())` if validation passes, or an error describing the issue.
167///
168/// # Examples
169///
170/// ```rust
171/// # #[cfg(feature = "schema")]
172/// # {
173/// use config_lib::{parse, SchemaBuilder};
174///
175/// let config = parse(r#"
176/// name = "my-app"
177/// port = 8080
178/// "#, None)?;
179///
180/// let schema = SchemaBuilder::new()
181/// .require_string("name")
182/// .require_integer("port")
183/// .build();
184///
185/// config_lib::validate(&config, &schema)?;
186/// # }
187///
188/// # Ok::<(), config_lib::Error>(())
189/// ```
190#[cfg(feature = "schema")]
191pub fn validate(config: &Value, schema: &Schema) -> Result<()> {
192 schema.validate(config)
193}
194
195/// Async version of parse_file
196///
197/// Available when the `async` feature is enabled.
198#[cfg(feature = "async")]
199pub async fn parse_file_async<P: AsRef<Path>>(path: P) -> Result<Value> {
200 parsers::parse_file_async(path).await
201}