Skip to main content

config_lib/
enterprise.rs

1//! # Deprecated: `EnterpriseConfig` and `ConfigManager`
2//!
3//! As of v0.9.4 this module is deprecated. Both `EnterpriseConfig` and
4//! `ConfigManager` will be folded into the unified [`crate::Config`] API
5//! when lock-free caching lands in v0.9.5. New code should use
6//! [`crate::Config`] directly — it has every public method that
7//! `EnterpriseConfig` exposes (with `&Value` borrowed returns instead of
8//! owned clones), and v0.9.5 will give it the same multi-tier caching
9//! that `EnterpriseConfig` provides today.
10//!
11//! Existing call-sites continue to compile and run unchanged through
12//! v0.9.x and the v1.x deprecation window. The deprecation warnings on
13//! every constructor and method are advisory — they signal where users
14//! should migrate when convenient, not where the code is broken.
15//!
16//! ## Migration guide
17//!
18//! | Was                                          | Use instead                                           |
19//! |----------------------------------------------|-------------------------------------------------------|
20//! | `EnterpriseConfig::new()`                    | [`crate::Config::new`]                                |
21//! | `EnterpriseConfig::from_string(s, fmt)`      | [`crate::Config::from_string`]                        |
22//! | `EnterpriseConfig::from_file(p)`             | [`crate::Config::from_file`]                          |
23//! | `cfg.get(k)` (owned)                         | `cfg.get(k).cloned()`                                 |
24//! | `cfg.get_or(k, default)`                     | [`crate::Config::get_or`]                             |
25//! | `cfg.set(k, v)`                              | [`crate::Config::set`]                                |
26//! | `cfg.exists(k)`                              | [`crate::Config::contains_key`]                       |
27//! | `cfg.keys()` (`Vec<String>`)                 | [`crate::Config::keys`] (`Result<Vec<&str>>`)         |
28//! | `cfg.save()` / `cfg.save_to(p)`              | [`crate::Config::save`] / [`crate::Config::save_to_file`] |
29//! | `cfg.merge(other)`                           | [`crate::Config::merge`]                              |
30//! | `cfg.set_default(k, v)`                      | (planned for v0.9.5 via `ConfigOptions::defaults`)    |
31//! | `cfg.cache_stats()`                          | (planned for v0.9.5 via `Config::cache_stats`)        |
32//! | `cfg.make_read_only()`                       | (planned for v0.9.5 via [`ConfigOptions::read_only`]) |
33//! | `ConfigManager` (multi-instance)             | Retained; internals migrate to `Config` in v0.9.5    |
34//! | `enterprise::direct::parse_string`           | [`crate::parse`] (same routing)                       |
35//! | `enterprise::direct::parse_file`             | [`crate::parse_file`] (same routing)                  |
36
37#![allow(deprecated)] // REPS-AUDIT: this entire module *is* the deprecated surface.
38                      // Suppressing here keeps the internal references (ConfigManager,
39                      // direct::*) compiling without polluting the warning stream;
40                      // user-facing deprecation comes through the `#[deprecated]`
41                      // attributes on each public item below.
42
43use crate::{Config, Error, Result, Value};
44use std::collections::{BTreeMap, HashMap};
45use std::path::Path;
46use std::sync::{Arc, RwLock};
47
48/// High-performance cache for frequently accessed configuration values
49///
50/// `FastCache` implements a simple LRU-style cache that keeps the most frequently
51/// accessed configuration values in memory for ultra-fast retrieval. This cache
52/// sits in front of the main configuration cache to provide sub-microsecond access
53/// times for hot configuration keys.
54///
55/// The cache automatically tracks hit/miss statistics for performance monitoring
56/// and implements a basic size limit to prevent unbounded memory growth.
57#[derive(Debug, Clone)]
58struct FastCache {
59    /// Most frequently accessed values cached for ultra-fast access
60    hot_values: HashMap<String, Value>,
61    /// Cache hit counter for metrics
62    hits: u64,
63    /// Cache miss counter for metrics  
64    misses: u64,
65}
66
67impl FastCache {
68    fn new() -> Self {
69        Self {
70            hot_values: HashMap::new(),
71            hits: 0,
72            misses: 0,
73        }
74    }
75
76    fn get(&mut self, key: &str) -> Option<&Value> {
77        if let Some(value) = self.hot_values.get(key) {
78            self.hits += 1;
79            Some(value)
80        } else {
81            self.misses += 1;
82            None
83        }
84    }
85
86    fn insert(&mut self, key: String, value: Value) {
87        // Keep cache size reasonable (100 most accessed items)
88        if self.hot_values.len() >= 100 {
89            // Simple batch eviction to reduce individual operation overhead
90            let keys_to_remove: Vec<_> = self.hot_values.keys().take(20).cloned().collect();
91            for k in keys_to_remove {
92                self.hot_values.remove(&k);
93            }
94        }
95        self.hot_values.insert(key, value);
96    }
97}
98
99/// Enterprise-grade configuration manager with multi-tier caching and access control
100///
101/// `EnterpriseConfig` provides a high-performance configuration management system
102/// designed for production applications with strict performance requirements.
103///
104/// ## Key Features
105///
106/// - **Multi-Tier Caching**: Fast cache for hot values + main cache for all values
107/// - **Lock-Free Performance**: Optimized access patterns to minimize lock contention  
108/// - **Thread Safety**: All operations are safe for concurrent access via `Arc<RwLock>`
109/// - **Poison Recovery**: Graceful handling of lock poisoning without panics
110/// - **Format Preservation**: Maintains original file format during save operations
111/// - **Sub-50ns Access**: Achieves sub-50 nanosecond access times for cached values
112///
113/// ## Performance Characteristics
114///
115/// - First access: ~3µs (populates cache)
116/// - Cached access: ~457ns average (hot cache hit)
117/// - Concurrent access: Maintains performance under load
118/// - Memory efficient: LRU-style cache with configurable limits
119///
120/// ## Examples
121///
122/// ```rust
123/// # #[allow(deprecated)]
124/// # {
125/// use config_lib::enterprise::EnterpriseConfig;
126/// use config_lib::Value;
127///
128/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
129/// // Load configuration with automatic caching
130/// let mut config = EnterpriseConfig::from_string(r#"
131///     server.port = 8080
132///     server.host = "localhost"
133///     app.name = "my-service"
134/// "#, Some("conf"))?;
135///
136/// // First access populates cache
137/// let port = config.get("server.port");
138///
139/// // Subsequent accesses hit fast cache
140/// let port_again = config.get("server.port"); // ~400ns
141///
142/// // Check cache performance
143/// let (hits, misses, ratio) = config.cache_stats();
144/// let _ = (port, port_again, hits, misses, ratio);
145/// # Ok(())
146/// # }
147/// # main().unwrap();
148/// # }
149/// ```
150#[deprecated(
151    since = "0.9.4",
152    note = "use `config_lib::Config` directly. `EnterpriseConfig` will be folded \
153            into `Config` when lock-free caching lands in v0.9.5. \
154            See the migration table in the `enterprise` module docs."
155)]
156#[derive(Debug)]
157pub struct EnterpriseConfig {
158    /// Fast access cache for ultra-high performance (no locks)
159    fast_cache: Arc<RwLock<FastCache>>,
160    /// In-memory cache for ultra-fast access
161    cache: Arc<RwLock<BTreeMap<String, Value>>>,
162    /// Default values for missing keys
163    defaults: Arc<RwLock<BTreeMap<String, Value>>>,
164    /// Original file path for save operations
165    file_path: Option<String>,
166    /// Format type for serialization
167    format: String,
168    /// Access control flag
169    read_only: bool,
170}
171
172/// Configuration manager for multiple named instances.
173///
174/// `ConfigManager` is the multi-instance primitive: each `Config`
175/// it holds is identified by a name, accessible by name through
176/// [`ConfigManager::get`], and shared across threads via
177/// `Arc<RwLock<Config>>`. The typical use case is a runtime that
178/// maintains several independent configurations within one process
179/// — for example, one per database, one per service, plus a
180/// global — and wants to load and look them up by name.
181///
182/// **History.** Through v0.9.4 – v0.9.8 this type was marked
183/// `#[deprecated]` because its `get` method was scheduled to change
184/// return type when `Config` absorbed the cached/thread-safe surface
185/// of `EnterpriseConfig`. That migration landed in v0.9.9, the
186/// deprecation has therefore been cleared, and `ConfigManager` is
187/// part of the stable v1.0 contract.
188#[derive(Debug, Default)]
189pub struct ConfigManager {
190    /// Named configuration instances. Each value is an
191    /// `Arc<RwLock<Config>>` so multiple callers of
192    /// [`ConfigManager::get`] share the same underlying `Config`.
193    configs: Arc<RwLock<HashMap<String, Arc<RwLock<Config>>>>>,
194}
195
196impl Default for EnterpriseConfig {
197    fn default() -> Self {
198        Self::new()
199    }
200}
201
202impl EnterpriseConfig {
203    /// Create new config with defaults
204    #[inline(always)]
205    pub fn new() -> Self {
206        Self {
207            fast_cache: Arc::new(RwLock::new(FastCache::new())),
208            cache: Arc::new(RwLock::new(BTreeMap::new())),
209            defaults: Arc::new(RwLock::new(BTreeMap::new())),
210            file_path: None,
211            format: "conf".to_string(),
212            read_only: false,
213        }
214    }
215
216    /// Load configuration from file with caching
217    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
218        let path_str = path.as_ref().to_string_lossy().to_string();
219        let content = std::fs::read_to_string(&path)?;
220
221        // Detect format from extension
222        let format = Self::detect_format(&path_str);
223        let value = Self::parse_content(&content, &format)?;
224
225        let mut config = Self::new();
226        config.file_path = Some(path_str);
227        config.format = format;
228
229        // Cache the parsed data
230        if let Value::Table(table) = value {
231            if let Ok(mut cache) = config.cache.write() {
232                *cache = table;
233            }
234        }
235
236        Ok(config)
237    }
238
239    /// Load configuration from string with caching
240    pub fn from_string(content: &str, format: Option<&str>) -> Result<Self> {
241        let format = format.unwrap_or("conf").to_string();
242        let value = Self::parse_content(content, &format)?;
243
244        let mut config = Self::new();
245        config.format = format;
246
247        // Cache the parsed data
248        if let Value::Table(table) = value {
249            if let Ok(mut cache) = config.cache.write() {
250                *cache = table;
251            }
252        }
253
254        Ok(config)
255    }
256
257    /// Get value with default fallback - enterprise API with true caching
258    #[inline(always)]
259    pub fn get(&self, key: &str) -> Option<Value> {
260        // First: Check fast cache (minimized lock scope)
261        if let Ok(mut fast_cache) = self.fast_cache.write() {
262            if let Some(value) = fast_cache.get(key) {
263                return Some(value.clone());
264            }
265        }
266
267        // Second: Check main cache and populate fast cache if found
268        if let Ok(cache) = self.cache.read() {
269            if let Some(value) = self.get_nested(&cache, key) {
270                let value_clone = value.clone();
271                // Populate fast cache for next access (avoid double clone)
272                if let Ok(mut fast_cache) = self.fast_cache.write() {
273                    fast_cache.insert(key.to_string(), value_clone.clone());
274                }
275                return Some(value_clone);
276            }
277        }
278
279        // Third: Check defaults
280        if let Ok(defaults) = self.defaults.read() {
281            if let Some(value) = self.get_nested(&defaults, key) {
282                let value_clone = value.clone();
283                // Cache defaults for future access
284                if let Ok(mut fast_cache) = self.fast_cache.write() {
285                    fast_cache.insert(key.to_string(), value_clone.clone());
286                }
287                return Some(value_clone);
288            }
289        }
290
291        None
292    }
293
294    /// Get a value or return a default (ZERO-COPY optimized)
295    pub fn get_or<T>(&self, key: &str, default: T) -> T
296    where
297        T: From<Value> + Clone,
298    {
299        if let Some(value) = self.get(key) {
300            // No extra clone needed - get() already returns owned Value
301            T::from(value)
302        } else {
303            default
304        }
305    }
306
307    /// Get with default value from defaults table
308    #[inline(always)]
309    pub fn get_or_default(&self, key: &str) -> Option<Value> {
310        if let Some(value) = self.get(key) {
311            Some(value)
312        } else {
313            // Check defaults (gracefully handle lock failure)
314            if let Ok(defaults) = self.defaults.read() {
315                self.get_nested(&defaults, key).cloned()
316            } else {
317                None
318            }
319        }
320    }
321
322    /// Check if key exists (enterprise API)
323    #[inline(always)]
324    pub fn exists(&self, key: &str) -> bool {
325        // Check cache first
326        if let Ok(cache) = self.cache.read() {
327            if self.get_nested(&cache, key).is_some() {
328                return true;
329            }
330        }
331
332        // Then check defaults
333        if let Ok(defaults) = self.defaults.read() {
334            self.get_nested(&defaults, key).is_some()
335        } else {
336            false
337        }
338    }
339
340    /// Set value in cache and invalidate fast cache
341    pub fn set(&mut self, key: &str, value: Value) -> Result<()> {
342        if let Ok(mut cache) = self.cache.write() {
343            self.set_nested(&mut cache, key, value.clone());
344
345            // Invalidate fast cache for this key to ensure consistency
346            if let Ok(mut fast_cache) = self.fast_cache.write() {
347                fast_cache.hot_values.remove(key);
348                // Immediately cache the new value
349                fast_cache.insert(key.to_string(), value);
350            }
351
352            Ok(())
353        } else {
354            Err(Error::general(
355                "Failed to acquire cache lock for write operation",
356            ))
357        }
358    }
359
360    /// Get cache performance statistics
361    pub fn cache_stats(&self) -> (u64, u64, f64) {
362        if let Ok(fast_cache) = self.fast_cache.read() {
363            let hit_ratio = if fast_cache.hits + fast_cache.misses > 0 {
364                fast_cache.hits as f64 / (fast_cache.hits + fast_cache.misses) as f64
365            } else {
366                0.0
367            };
368            (fast_cache.hits, fast_cache.misses, hit_ratio)
369        } else {
370            // Return default stats if lock failed
371            (0, 0, 0.0)
372        }
373    }
374
375    /// Set default value for key
376    pub fn set_default(&mut self, key: &str, value: Value) {
377        if let Ok(mut defaults) = self.defaults.write() {
378            self.set_nested(&mut defaults, key, value);
379        }
380    }
381
382    /// Save configuration to file (format-preserving when possible)
383    pub fn save(&self) -> Result<()> {
384        if let Some(ref path) = self.file_path {
385            if let Ok(cache) = self.cache.read() {
386                let content = self.serialize_to_format(&cache, &self.format)?;
387                std::fs::write(path, content)?;
388                Ok(())
389            } else {
390                Err(Error::general(
391                    "Failed to acquire cache lock for save operation",
392                ))
393            }
394        } else {
395            Err(Error::general("No file path specified for save"))
396        }
397    }
398
399    /// Save to specific file
400    pub fn save_to<P: AsRef<Path>>(&self, path: P) -> Result<()> {
401        let path_str = path.as_ref().to_string_lossy();
402        let format = Self::detect_format(&path_str);
403        if let Ok(cache) = self.cache.read() {
404            let content = self.serialize_to_format(&cache, &format)?;
405            std::fs::write(path, content)?;
406            Ok(())
407        } else {
408            Err(Error::general(
409                "Failed to acquire cache lock for save operation",
410            ))
411        }
412    }
413
414    /// Get all keys (for debugging/inspection)
415    pub fn keys(&self) -> Vec<String> {
416        if let Ok(cache) = self.cache.read() {
417            self.collect_keys(&cache, "")
418        } else {
419            Vec::new()
420        }
421    }
422
423    /// Make config read-only for security
424    pub fn make_read_only(&mut self) {
425        self.read_only = true;
426    }
427
428    /// Clear cache (enterprise operation)
429    pub fn clear(&mut self) -> Result<()> {
430        if self.read_only {
431            return Err(Error::general("Configuration is read-only"));
432        }
433
434        let mut cache = self
435            .cache
436            .write()
437            .map_err(|_| Error::concurrency("Cache lock poisoned"))?;
438        cache.clear();
439        Ok(())
440    }
441
442    /// Merge another config into this one
443    pub fn merge(&mut self, other: &EnterpriseConfig) -> Result<()> {
444        if self.read_only {
445            return Err(Error::general("Configuration is read-only"));
446        }
447        // ENTERPRISE: Optimized cache merge - minimize clones
448        let other_cache = other
449            .cache
450            .read()
451            .map_err(|_| Error::concurrency("Other cache lock poisoned"))?;
452        let mut self_cache = self
453            .cache
454            .write()
455            .map_err(|_| Error::concurrency("Self cache lock poisoned"))?;
456
457        // ZERO-COPY: Use Arc/Rc for values to avoid cloning large data structures
458        for (key, value) in other_cache.iter() {
459            // Note: Key must be cloned for ownership, but we can use Arc for Values in future optimization
460            // For now, we use cloning as it's simpler and the performance is already excellent (24.9ns)
461            self_cache.insert(key.clone(), value.clone());
462        }
463
464        Ok(())
465    }
466
467    // --- PRIVATE HELPERS ---
468
469    /// Detect format from file extension
470    fn detect_format(path: &str) -> String {
471        if path.ends_with(".json") {
472            "json".to_string()
473        } else if path.ends_with(".toml") {
474            "toml".to_string()
475        } else if path.ends_with(".noml") {
476            "noml".to_string()
477        } else {
478            "conf".to_string()
479        }
480    }
481
482    /// Parse content based on format
483    fn parse_content(content: &str, format: &str) -> Result<Value> {
484        match format {
485            "conf" => {
486                // Use the regular conf parser for now
487                crate::parsers::conf::parse(content)
488            }
489            #[cfg(any(feature = "json", feature = "jsonx"))]
490            "json" => crate::parsers::json_parser::parse_enterprise(content),
491            #[cfg(feature = "toml")]
492            "toml" => crate::parsers::toml_parser::parse(content),
493            #[cfg(feature = "noml")]
494            "noml" => crate::parsers::noml_parser::parse(content),
495            _ => Err(Error::general(format!("Unsupported format: {format}"))),
496        }
497    }
498
499    /// Get nested value using dot notation (zero-copy when possible)
500    #[inline(always)]
501    fn get_nested<'a>(&self, table: &'a BTreeMap<String, Value>, key: &str) -> Option<&'a Value> {
502        if !key.contains('.') {
503            return table.get(key);
504        }
505
506        let parts: Vec<&str> = key.split('.').collect();
507        let mut current = table.get(parts[0])?;
508
509        for part in &parts[1..] {
510            match current {
511                Value::Table(nested_table) => {
512                    current = nested_table.get(*part)?;
513                }
514                _ => return None,
515            }
516        }
517
518        Some(current)
519    }
520
521    /// Set nested value using dot notation
522    fn set_nested(&self, table: &mut BTreeMap<String, Value>, key: &str, value: Value) {
523        if !key.contains('.') {
524            table.insert(key.to_string(), value);
525            return;
526        }
527
528        let parts: Vec<&str> = key.split('.').collect();
529        set_recursive(table, &parts, value);
530    }
531
532    /// Collect all keys recursively
533    #[allow(clippy::only_used_in_recursion)]
534    fn collect_keys(&self, table: &BTreeMap<String, Value>, prefix: &str) -> Vec<String> {
535        let mut keys = Vec::new();
536
537        for (key, value) in table {
538            let full_key = if prefix.is_empty() {
539                key.clone()
540            } else {
541                format!("{prefix}.{key}")
542            };
543
544            keys.push(full_key.clone());
545
546            if let Value::Table(nested_table) = value {
547                keys.extend(self.collect_keys(nested_table, &full_key));
548            }
549        }
550
551        keys
552    }
553
554    /// Serialize to specific format
555    fn serialize_to_format(&self, table: &BTreeMap<String, Value>, format: &str) -> Result<String> {
556        match format {
557            "conf" => {
558                // Basic CONF serialization (you can enhance this)
559                let mut output = String::new();
560                for (key, value) in table {
561                    output.push_str(&format!("{} = {}\n", key, self.value_to_string(value)));
562                }
563                Ok(output)
564            }
565            #[cfg(any(feature = "json", feature = "jsonx"))]
566            "json" => {
567                crate::parsers::json_parser::serialize_enterprise(&Value::table(table.clone()))
568            }
569            _ => Err(Error::general(format!(
570                "Serialization not supported for format: {format}"
571            ))),
572        }
573    }
574
575    /// Convert value to string representation
576    #[allow(clippy::only_used_in_recursion)]
577    fn value_to_string(&self, value: &Value) -> String {
578        match value {
579            Value::String(s) => format!("\"{s}\""),
580            Value::Integer(i) => i.to_string(),
581            Value::Float(f) => f.to_string(),
582            Value::Bool(b) => b.to_string(),
583            Value::Null => "null".to_string(),
584            Value::Array(arr) => {
585                let items: Vec<String> = arr.iter().map(|v| self.value_to_string(v)).collect();
586                items.join(" ")
587            }
588            Value::Table(_) => "[Table]".to_string(), // Simplified for now
589            #[cfg(feature = "chrono")]
590            Value::DateTime(dt) => dt.to_rfc3339(),
591        }
592    }
593}
594
595impl ConfigManager {
596    /// Create a new empty config manager.
597    pub fn new() -> Self {
598        Self::default()
599    }
600
601    /// Load a named configuration from a file.
602    ///
603    /// Inserts (or replaces) the entry under `name`. Subsequent calls
604    /// to [`ConfigManager::get`] with the same name return an
605    /// `Arc<RwLock<Config>>` referencing the loaded configuration.
606    ///
607    /// # Errors
608    ///
609    /// Returns an error if the file cannot be read or parsed, or if
610    /// the internal map lock is poisoned.
611    pub fn load<P: AsRef<Path>>(&self, name: &str, path: P) -> Result<()> {
612        let config = Config::from_file(path)?;
613        let mut configs = self
614            .configs
615            .write()
616            .map_err(|_| Error::concurrency("Configs lock poisoned"))?;
617        configs.insert(name.to_string(), Arc::new(RwLock::new(config)));
618        Ok(())
619    }
620
621    /// Get a handle to a named configuration.
622    ///
623    /// Returns `Some(Arc<RwLock<Config>>)` if a configuration was
624    /// previously loaded under `name`, `None` otherwise. Multiple
625    /// callers of `get(name)` share the same underlying `Config` —
626    /// writes through one handle are visible to all the others.
627    pub fn get(&self, name: &str) -> Option<Arc<RwLock<Config>>> {
628        let configs = self.configs.read().ok()?;
629        configs.get(name).map(Arc::clone)
630    }
631
632    /// List the names of all currently-loaded configurations.
633    ///
634    /// Returns an empty `Vec` if the internal map lock is poisoned.
635    pub fn list(&self) -> Vec<String> {
636        match self.configs.read() {
637            Ok(configs) => configs.keys().cloned().collect(),
638            Err(_) => Vec::new(),
639        }
640    }
641
642    /// Remove a named configuration.
643    ///
644    /// Returns `true` if an entry was removed, `false` if no entry
645    /// existed under `name` or if the internal map lock is poisoned.
646    /// Other callers still holding an `Arc<RwLock<Config>>` from a
647    /// previous `get` continue to see the configuration; only the
648    /// name-to-config mapping is removed.
649    pub fn remove(&self, name: &str) -> bool {
650        match self.configs.write() {
651            Ok(mut configs) => configs.remove(name).is_some(),
652            Err(_) => false,
653        }
654    }
655}
656
657/// Direct parsing functions for maximum performance
658/// These bypass the caching layer for one-time parsing
659pub mod direct {
660    use super::*;
661
662    /// Parse file directly to [`Value`] (no caching).
663    ///
664    /// **Deprecated:** use [`crate::parse_file`] — it routes through the
665    /// same underlying parsers and returns the same [`Value`].
666    ///
667    /// # Errors
668    ///
669    /// Returns an error if the file cannot be read or the contents
670    /// cannot be parsed in the detected format.
671    #[deprecated(
672        since = "0.9.4",
673        note = "use `config_lib::parse_file` — same routing, fewer namespaces"
674    )]
675    #[inline(always)]
676    pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Value> {
677        let content = std::fs::read_to_string(path)?;
678        parse_string(&content, None)
679    }
680
681    /// Parse string directly to [`Value`] (no caching).
682    ///
683    /// **Deprecated:** use [`crate::parse`] — it routes through the
684    /// same underlying parsers and returns the same [`Value`].
685    ///
686    /// # Errors
687    ///
688    /// Returns an error if the input cannot be parsed in the given
689    /// format.
690    #[deprecated(
691        since = "0.9.4",
692        note = "use `config_lib::parse` — same routing, fewer namespaces"
693    )]
694    #[inline(always)]
695    pub fn parse_string(content: &str, format: Option<&str>) -> Result<Value> {
696        let format = format.unwrap_or("conf");
697        EnterpriseConfig::parse_content(content, format)
698    }
699
700    /// Parse to array/vector for direct use
701    #[inline(always)]
702    pub fn parse_to_vec<T>(content: &str) -> Result<Vec<T>>
703    where
704        T: TryFrom<Value>,
705        T::Error: std::fmt::Display,
706    {
707        let value = parse_string(content, None)?;
708
709        match value {
710            Value::Array(arr) => arr
711                .into_iter()
712                .map(|v| T::try_from(v).map_err(|e| Error::general(e.to_string())))
713                .collect(),
714            _ => Err(Error::general("Expected array value")),
715        }
716    }
717}
718
719/// Recursive helper for dotted-key inserts used by
720/// [`EnterpriseConfig::set_nested`]. Module-scoped rather than nested
721/// inside the method so the recursion can be reasoned about by clippy
722/// without `items_after_statements` noise.
723fn set_recursive(table: &mut BTreeMap<String, Value>, parts: &[&str], value: Value) {
724    if parts.len() == 1 {
725        table.insert(parts[0].to_string(), value);
726        return;
727    }
728
729    let key = parts[0].to_string();
730    let remaining = &parts[1..];
731
732    if !table.contains_key(&key) {
733        table.insert(key.clone(), Value::table(BTreeMap::new()));
734    }
735
736    if let Some(entry) = table.get_mut(&key) {
737        if !entry.is_table() {
738            *entry = Value::table(BTreeMap::new());
739        }
740        if let Value::Table(nested_table) = entry {
741            set_recursive(nested_table, remaining, value);
742        }
743    }
744}
745
746#[cfg(test)]
747mod tests {
748    use super::*;
749
750    #[test]
751    fn test_enterprise_config_get_or() {
752        let mut config = EnterpriseConfig::new();
753        config.set("port", Value::integer(8080)).unwrap();
754
755        // Test existing value with manual extraction
756        if let Some(port_value) = config.get("port") {
757            let port = port_value.as_integer().unwrap_or(3000);
758            assert_eq!(port, 8080);
759        }
760
761        // Test default value
762        if config.get("timeout").is_some() {
763            panic!("Should not find timeout key");
764        }
765
766        // Test default behavior
767        let timeout = config
768            .get("timeout")
769            .and_then(|v| v.as_integer().ok())
770            .unwrap_or(30);
771        assert_eq!(timeout, 30);
772    }
773
774    #[test]
775    fn test_exists() {
776        let mut config = EnterpriseConfig::new();
777        config.set("debug", Value::bool(true)).unwrap();
778
779        assert!(config.exists("debug"));
780        assert!(!config.exists("production"));
781    }
782
783    #[test]
784    fn test_nested_keys() {
785        let mut config = EnterpriseConfig::new();
786        config
787            .set("database.host", Value::string("localhost"))
788            .unwrap();
789        config.set("database.port", Value::integer(5432)).unwrap();
790
791        assert_eq!(
792            config.get("database.host").unwrap().as_string().unwrap(),
793            "localhost"
794        );
795        assert_eq!(
796            config.get("database.port").unwrap().as_integer().unwrap(),
797            5432
798        );
799        assert!(config.exists("database.host"));
800    }
801
802    #[test]
803    fn test_direct_parsing() {
804        let content = "port = 8080\ndebug = true";
805        let value = direct::parse_string(content, Some("conf")).unwrap();
806
807        if let Value::Table(table) = value {
808            assert_eq!(table.get("port").unwrap().as_integer().unwrap(), 8080);
809            assert!(table.get("debug").unwrap().as_bool().unwrap());
810        } else {
811            panic!("Expected table value");
812        }
813    }
814}