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 with optional format hint
87///
88/// This is the primary entry point for parsing configuration data from strings.
89/// Automatically detects format if not specified.
90///
91/// # Arguments
92///
93/// * `source` - The configuration data as a string
94/// * `format` - Optional format hint ("conf", "toml", "json", "noml")
95///
96/// # Returns
97///
98/// Returns a [`Value`] containing the parsed configuration data.
99///
100/// # Errors
101///
102/// Returns an error if:
103/// - The input format is unknown or unsupported
104/// - The input contains syntax errors
105/// - Required features are not enabled for the detected format
106///
107/// # Examples
108///
109/// ```rust
110/// use config_lib::parse;
111///
112/// let config = parse("port = 8080\nname = \"MyApp\"", Some("conf"))?;
113/// let port = config.get("port").unwrap().as_integer()?;
114///
115/// # Ok::<(), config_lib::Error>(())
116/// ```
117pub fn parse(source: &str, format: Option<&str>) -> Result<Value> {
118    parsers::parse_string(source, format)
119}
120
121/// Parse configuration from a file, auto-detecting format from extension
122///
123/// Reads a configuration file from disk and automatically detects the format
124/// based on the file extension (.conf, .toml, .json, .noml).
125///
126/// # Arguments
127///
128/// * `path` - Path to the configuration file
129///
130/// # Returns
131///
132/// Returns a [`Value`] containing the parsed configuration data.
133///
134/// # Errors
135///
136/// Returns an error if:
137/// - The file cannot be read (I/O error)
138/// - The file format cannot be detected
139/// - The file contains syntax errors
140/// - Required features are not enabled for the detected format
141///
142/// # Examples
143///
144/// ```rust,no_run
145/// use config_lib::parse_file;
146///
147/// let config = parse_file("app.conf")?;
148/// let port = config.get("server.port").unwrap().as_integer()?;
149///
150/// # Ok::<(), config_lib::Error>(())
151/// ```
152pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Value> {
153    parsers::parse_file(path)
154}
155
156/// Validate configuration against a schema
157///
158/// Performs comprehensive validation of configuration data against a provided
159/// schema definition.
160///
161/// # Arguments
162///
163/// * `config` - Configuration value to validate
164/// * `schema` - Schema definition for validation
165///
166/// # Returns
167///
168/// Returns `Ok(())` if validation passes, or an error describing the issue.
169///
170/// # Examples
171///
172/// ```rust
173/// # #[cfg(feature = "schema")]
174/// # {
175/// use config_lib::{parse, SchemaBuilder};
176///
177/// let config = parse(r#"
178///     name = "my-app"
179///     port = 8080
180/// "#, None)?;
181///
182/// let schema = SchemaBuilder::new()
183///     .require_string("name")
184///     .require_integer("port")
185///     .build();
186///
187/// config_lib::validate(&config, &schema)?;
188/// # }
189///
190/// # Ok::<(), config_lib::Error>(())
191/// ```
192#[cfg(feature = "schema")]
193pub fn validate(config: &Value, schema: &Schema) -> Result<()> {
194    schema.validate(config)
195}
196
197/// Async version of parse_file
198///
199/// Available when the `async` feature is enabled.
200#[cfg(feature = "async")]
201pub async fn parse_file_async<P: AsRef<Path>>(path: P) -> Result<Value> {
202    parsers::parse_file_async(path).await
203}