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// Re-export main types for convenience
58pub use config::Config;
59pub use enterprise::{EnterpriseConfig, ConfigManager};
60pub use error::{Error, Result};
61pub use value::Value;
62
63#[cfg(feature = "schema")]
64pub use schema::{Schema, SchemaBuilder};
65
66use std::path::Path;
67
68/// Parse configuration from a string, auto-detecting format
69///
70/// This is the main entry point for parsing configuration data. The format
71/// is automatically detected based on the content structure and syntax.
72///
73/// # Arguments
74///
75/// * `source` - Configuration text to parse
76/// * `format` - Optional format hint (auto-detected if None)
77///
78/// # Returns
79///
80/// Returns a [`Value`] containing the parsed configuration data.
81///
82/// # Examples
83///
84/// ```rust
85/// use config_lib::parse;
86///
87/// let config = parse(r#"
88///     app_name = "my-service"
89///     port = 8080
90///     debug = true
91///     
92///     [database]
93///     host = "localhost"
94///     max_connections = 100
95/// "#, None)?;
96///
97/// assert_eq!(config.get("app_name").unwrap().as_string().unwrap(), "my-service");
98/// assert_eq!(config.get("database.host").unwrap().as_string().unwrap(), "localhost");
99///
100/// # Ok::<(), config_lib::Error>(())
101/// ```
102pub fn parse(source: &str, format: Option<&str>) -> Result<Value> {
103    parsers::parse_string(source, format)
104}
105
106/// Parse configuration from a file, auto-detecting format from extension
107///
108/// Reads a configuration file from disk and automatically detects the format
109/// based on the file extension (.conf, .toml, .json, .noml).
110///
111/// # Arguments
112///
113/// * `path` - Path to the configuration file
114///
115/// # Returns
116///
117/// Returns a [`Value`] containing the parsed configuration data.
118///
119/// # Examples
120///
121/// ```rust,no_run
122/// use config_lib::parse_file;
123///
124/// let config = parse_file("app.conf")?;
125/// let port = config.get("server.port").unwrap().as_integer()?;
126///
127/// # Ok::<(), config_lib::Error>(())
128/// ```
129pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Value> {
130    parsers::parse_file(path)
131}
132
133/// Validate configuration against a schema
134///
135/// Performs comprehensive validation of configuration data against a provided
136/// schema definition.
137///
138/// # Arguments
139///
140/// * `config` - Configuration value to validate
141/// * `schema` - Schema definition for validation
142///
143/// # Returns
144///
145/// Returns `Ok(())` if validation passes, or an error describing the issue.
146///
147/// # Examples
148///
149/// ```rust
150/// # #[cfg(feature = "schema")]
151/// # {
152/// use config_lib::{parse, SchemaBuilder};
153///
154/// let config = parse(r#"
155///     name = "my-app"
156///     port = 8080
157/// "#, None)?;
158///
159/// let schema = SchemaBuilder::new()
160///     .require_string("name")
161///     .require_integer("port")
162///     .build();
163///
164/// config_lib::validate(&config, &schema)?;
165/// # }
166///
167/// # Ok::<(), config_lib::Error>(())
168/// ```
169#[cfg(feature = "schema")]
170pub fn validate(config: &Value, schema: &Schema) -> Result<()> {
171    schema.validate(config)
172}
173
174/// Async version of parse_file
175///
176/// Available when the `async` feature is enabled.
177#[cfg(feature = "async")]
178pub async fn parse_file_async<P: AsRef<Path>>(path: P) -> Result<Value> {
179    parsers::parse_file_async(path).await
180}