sublime_standard_tools 0.0.15

A collection of utilities for working with Node.js projects from Rust applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Configuration manager implementation.
//!
//! This module provides a generic configuration manager that can handle any
//! type implementing the `Configurable` trait, with support for multiple
//! configuration sources and hierarchical loading.

use std::collections::HashMap;
use std::marker::PhantomData;
use std::path::Path;
use std::sync::Arc;

use serde::Serialize;
use serde::de::DeserializeOwned;
use tokio::sync::RwLock;

use crate::error::{ConfigError, ConfigResult};
use crate::filesystem::AsyncFileSystem;

use super::format::ConfigFormat;
use super::source::{
    ConfigSource, ConfigSourcePriority, DefaultProvider, EnvironmentProvider, FileProvider,
    MemoryProvider,
};
use super::traits::{ConfigProvider, Configurable};
use super::value::ConfigValue;

/// Generic configuration manager for any Configurable type.
///
/// This manager handles loading configuration from multiple sources,
/// merging them according to priority, and caching the result.
///
/// # Type Parameters
///
/// * `T` - The configuration type, must implement `Configurable`
///
/// # Examples
///
/// ```ignore
/// use sublime_standard_tools::config::{ConfigManager, Configurable, ConfigResult};
/// use sublime_standard_tools::filesystem::FileSystemManager;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, Clone, Serialize, Deserialize)]
/// struct MyConfig {
///     name: String,
///     timeout: u64,
/// }
///
/// impl Configurable for MyConfig {
///     fn validate(&self) -> ConfigResult<()> {
///         if self.timeout == 0 {
///             return Err("Timeout must be greater than 0".into());
///         }
///         Ok(())
///     }
///
///     fn merge_with(&mut self, other: Self) -> ConfigResult<()> {
///         if !other.name.is_empty() {
///             self.name = other.name;
///         }
///         if other.timeout > 0 {
///             self.timeout = other.timeout;
///         }
///         Ok(())
///     }
/// }
///
/// async fn example() -> ConfigResult<MyConfig> {
///     let fs = FileSystemManager::new();
///     let manager = ConfigManager::<MyConfig>::builder()
///         .with_defaults()
///         .with_file("config.toml")
///         .with_env_prefix("MY_APP")
///         .build(fs)?;
///
///     manager.load().await
/// }
/// ```
pub struct ConfigManager<T: Configurable> {
    providers: Vec<Box<dyn ConfigProvider>>,
    cache: Arc<RwLock<Option<T>>>,
    _phantom: PhantomData<T>,
}

impl<T: Configurable + Clone> ConfigManager<T> {
    /// Creates a new configuration builder.
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::ConfigManager;
    /// use sublime_standard_tools::config::standard::StandardConfig;
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder();
    /// ```
    pub fn builder() -> ConfigBuilder<T> {
        ConfigBuilder::new()
    }

    /// Creates a new configuration manager with the given providers.
    ///
    /// # Arguments
    ///
    /// * `providers` - Configuration providers sorted by priority
    fn new(providers: Vec<Box<dyn ConfigProvider>>) -> Self {
        Self { providers, cache: Arc::new(RwLock::new(None)), _phantom: PhantomData }
    }

    /// Loads configuration from all sources and merges them.
    ///
    /// Sources are loaded in priority order, with higher priority sources
    /// overriding values from lower priority ones.
    ///
    /// # Returns
    ///
    /// The loaded and merged configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Loading from any required source fails
    /// - Deserialization fails
    /// - Validation fails
    /// - Merging fails
    pub async fn load(&self) -> ConfigResult<T> {
        // Check cache first
        {
            let cache = self.cache.read().await;
            if let Some(config) = cache.as_ref() {
                return Ok(config.clone());
            }
        }

        // Load from all providers
        let mut merged_value = ConfigValue::Map(HashMap::default());

        for provider in &self.providers {
            match provider.load().await {
                Ok(value) => {
                    merged_value.merge(value);
                }
                Err(e) => {
                    // Log error but continue with other providers
                    log::warn!("Failed to load from provider '{}': {}", provider.name(), e);
                }
            }
        }

        // Deserialize the merged value
        let config: T = serde_json::from_value(serde_json::to_value(&merged_value)?)
            .map_err(|e| ConfigError::parse("configuration", e.to_string()))?;

        // Validate the configuration
        config.validate()?;

        // Cache the result
        {
            let mut cache = self.cache.write().await;
            *cache = Some(config.clone());
        }

        Ok(config)
    }

    /// Loads configuration from all sources without using cache.
    ///
    /// This forces a fresh load from all sources.
    ///
    /// # Returns
    ///
    /// The loaded and merged configuration.
    ///
    /// # Errors
    ///
    /// Same as `load()`.
    pub async fn reload(&self) -> ConfigResult<T> {
        // Clear cache
        {
            let mut cache = self.cache.write().await;
            *cache = None;
        }

        self.load().await
    }

    /// Saves the configuration to all providers that support saving.
    ///
    /// # Arguments
    ///
    /// * `config` - The configuration to save
    ///
    /// # Errors
    ///
    /// Returns an error if saving to any provider fails.
    pub async fn save(&self, config: &T) -> ConfigResult<()> {
        // Validate before saving
        config.validate()?;

        // Convert to ConfigValue
        let value = serde_json::to_value(config)?;
        let config_value: ConfigValue = serde_json::from_value(value)?;

        // Save to all providers that support it
        for provider in &self.providers {
            if provider.supports_save() {
                provider
                    .save(&config_value)
                    .await
                    .map_err(|e| ConfigError::provider(provider.name(), e.to_string()))?;
            }
        }

        // Update cache
        {
            let mut cache = self.cache.write().await;
            *cache = Some(config.clone());
        }

        Ok(())
    }

    /// Gets a value by key path from the cached configuration.
    ///
    /// # Arguments
    ///
    /// * `key` - Dot-separated key path (e.g., "database.host")
    ///
    /// # Returns
    ///
    /// The value at the given key path, if it exists and can be deserialized.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - No configuration is loaded
    /// - The key doesn't exist
    /// - Deserialization fails
    pub async fn get<V: DeserializeOwned>(&self, key: &str) -> ConfigResult<Option<V>> {
        let cache = self.cache.read().await;
        let config = cache.as_ref().ok_or_else(|| ConfigError::other("No configuration loaded"))?;

        // Convert to ConfigValue to access by key
        let value = serde_json::to_value(config)?;
        let config_value: ConfigValue = serde_json::from_value(value)?;

        // Navigate to the key
        let parts: Vec<&str> = key.split('.').collect();
        let mut current = &config_value;

        for part in parts {
            match current.get(part) {
                Some(v) => current = v,
                None => return Ok(None),
            }
        }

        // Deserialize the value
        let result: V = serde_json::from_value(serde_json::to_value(current)?)
            .map_err(|e| ConfigError::parse("value", e.to_string()))?;

        Ok(Some(result))
    }

    /// Sets a value by key path and saves the configuration.
    ///
    /// # Arguments
    ///
    /// * `key` - Dot-separated key path (e.g., "database.host")
    /// * `value` - The value to set
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - No configuration is loaded
    /// - Serialization fails
    /// - Validation fails
    /// - Saving fails
    pub async fn set<V: Serialize>(&self, key: &str, value: V) -> ConfigResult<()> {
        let mut cache = self.cache.write().await;
        let config = cache.as_mut().ok_or_else(|| ConfigError::other("No configuration loaded"))?;

        // Convert to ConfigValue for manipulation
        let mut config_value: ConfigValue =
            serde_json::from_value(serde_json::to_value(&*config)?)?;

        // Navigate to the key and set the value
        let parts: Vec<&str> = key.split('.').collect();
        let new_value: ConfigValue = serde_json::from_value(serde_json::to_value(&value)?)?;

        // Handle nested keys
        let mut current = &mut config_value;
        for (i, part) in parts.iter().enumerate() {
            if i == parts.len() - 1 {
                // Last part - set the value
                if let ConfigValue::Map(map) = current {
                    map.insert((*part).to_string(), new_value.clone());
                } else {
                    return Err(ConfigError::type_error("map", current.type_name()));
                }
            } else {
                // Navigate deeper, creating maps as needed
                if let ConfigValue::Map(map) = current {
                    let entry = map
                        .entry((*part).to_string())
                        .or_insert(ConfigValue::Map(HashMap::default()));
                    current = entry;
                } else {
                    return Err(ConfigError::type_error("map", current.type_name()));
                }
            }
        }

        // Convert back to T
        *config = serde_json::from_value(serde_json::to_value(&config_value)?)
            .map_err(|e| ConfigError::parse("configuration", e.to_string()))?;

        // Validate the new configuration
        config.validate()?;

        // Save is handled separately by the caller if needed
        Ok(())
    }

    /// Merges another configuration into the current one.
    ///
    /// # Arguments
    ///
    /// * `other` - The configuration to merge
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - No configuration is loaded
    /// - Merging fails
    /// - Validation fails
    pub async fn merge(&self, other: T) -> ConfigResult<()> {
        let mut cache = self.cache.write().await;
        let config = cache.as_mut().ok_or_else(|| ConfigError::other("No configuration loaded"))?;

        config.merge_with(other)?;
        config.validate()?;

        Ok(())
    }

    /// Validates the current configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - No configuration is loaded
    /// - Validation fails
    pub async fn validate(&self) -> ConfigResult<()> {
        let cache = self.cache.read().await;
        let config = cache.as_ref().ok_or_else(|| ConfigError::other("No configuration loaded"))?;

        config.validate()
    }

    /// Clears the configuration cache.
    pub async fn clear_cache(&self) {
        let mut cache = self.cache.write().await;
        *cache = None;
    }
}

/// Builder for creating a ConfigManager.
///
/// This builder provides a fluent API for configuring the sources
/// from which configuration should be loaded.
pub struct ConfigBuilder<T: Configurable + Clone> {
    sources: Vec<ConfigSource>,
    _phantom: PhantomData<T>,
}

impl<T: Configurable + Clone> ConfigBuilder<T> {
    /// Creates a new configuration builder.
    fn new() -> Self {
        Self { sources: Vec::new(), _phantom: PhantomData }
    }

    /// Adds default configuration values.
    ///
    /// This uses the `Configurable::default_values()` method if available.
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::ConfigManager;
    /// use sublime_standard_tools::config::standard::StandardConfig;
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder()
    ///     .with_defaults();
    /// ```
    #[must_use]
    pub fn with_defaults(mut self) -> Self
    where
        T: Default,
    {
        if let Some(defaults) = T::default_values() {
            let value = match serde_json::to_value(defaults) {
                Ok(serialized) => match serde_json::from_value(serialized) {
                    Ok(config_value) => config_value,
                    Err(e) => {
                        log::warn!(
                            "Failed to deserialize default configuration values: {e}. Using empty defaults."
                        );
                        ConfigValue::Map(HashMap::default())
                    }
                },
                Err(e) => {
                    log::warn!(
                        "Failed to serialize default configuration values: {e}. Using empty defaults."
                    );
                    ConfigValue::Map(HashMap::default())
                }
            };

            self.sources.push(ConfigSource::defaults(value));
        }
        self
    }

    /// Adds a configuration file source.
    ///
    /// The format is auto-detected from the file extension.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the configuration file
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::ConfigManager;
    /// use sublime_standard_tools::config::standard::StandardConfig;
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder()
    ///     .with_file("config.toml");
    /// ```
    #[must_use]
    pub fn with_file(mut self, path: impl AsRef<Path>) -> Self {
        let path = path.as_ref();
        let priority = if path.to_string_lossy().contains(".sublime") {
            ConfigSourcePriority::Project
        } else {
            ConfigSourcePriority::Global
        };

        self.sources.push(ConfigSource::file(path, priority));
        self
    }

    /// Adds a configuration file source with a specific priority.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the configuration file
    /// * `priority` - Priority of this source
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::{ConfigManager, ConfigSourcePriority};
    /// use sublime_standard_tools::config::standard::StandardConfig;
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder()
    ///     .with_file_priority("~/.config/sublime/config.toml", ConfigSourcePriority::Global);
    /// ```
    #[must_use]
    pub fn with_file_priority(
        mut self,
        path: impl AsRef<Path>,
        priority: ConfigSourcePriority,
    ) -> Self {
        self.sources.push(ConfigSource::file(path, priority));
        self
    }

    /// Adds an environment variable source.
    ///
    /// # Arguments
    ///
    /// * `prefix` - Prefix for environment variables (e.g., "SUBLIME")
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::ConfigManager;
    /// use sublime_standard_tools::config::standard::StandardConfig;
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder()
    ///     .with_env_prefix("SUBLIME");
    /// ```
    #[must_use]
    pub fn with_env_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.sources.push(ConfigSource::environment(prefix, ConfigSourcePriority::Environment));
        self
    }

    /// Adds a custom configuration source.
    ///
    /// # Arguments
    ///
    /// * `source` - The configuration source to add
    ///
    /// # Examples
    ///
    /// ```
    /// use sublime_standard_tools::config::{ConfigManager, ConfigSource, ConfigValue, ConfigSourcePriority};
    /// use sublime_standard_tools::config::standard::StandardConfig;
    /// use std::collections::HashMap;
    ///
    /// let mut runtime_config = HashMap::new();
    /// runtime_config.insert("debug".to_string(), ConfigValue::Boolean(true));
    ///
    /// let builder = ConfigManager::<StandardConfig>::builder()
    ///     .with_source(ConfigSource::memory(runtime_config, ConfigSourcePriority::Runtime));
    /// ```
    #[must_use]
    pub fn with_source(mut self, source: ConfigSource) -> Self {
        self.sources.push(source);
        self
    }

    /// Builds the configuration manager.
    ///
    /// This method requires an async filesystem implementation to be available.
    ///
    /// # Arguments
    ///
    /// * `fs` - The async filesystem implementation to use
    ///
    /// # Returns
    ///
    /// A configured `ConfigManager` instance.
    ///
    /// # Errors
    ///
    /// Returns an error if provider creation fails.
    #[allow(clippy::needless_pass_by_value)] // fs needs to be cloned for multiple providers
    pub fn build<FS: AsyncFileSystem + Clone + 'static>(
        self,
        fs: FS,
    ) -> ConfigResult<ConfigManager<T>> {
        let mut providers: Vec<Box<dyn ConfigProvider>> = Vec::new();

        // Sort sources by priority
        let mut sources = self.sources;
        sources.sort_by_key(ConfigSource::priority);

        // Create providers for each source
        for source in sources {
            match source {
                ConfigSource::File { path, format, priority } => {
                    let fmt =
                        format.or_else(|| ConfigFormat::from_path(&path)).ok_or_else(|| {
                            ConfigError::other(format!(
                                "Cannot determine format for file: {}",
                                path.display()
                            ))
                        })?;

                    providers.push(Box::new(FileProvider::new(path, fmt, priority, fs.clone())));
                }
                ConfigSource::Environment { prefix, priority } => {
                    providers.push(Box::new(EnvironmentProvider::new(prefix, priority)));
                }
                ConfigSource::Default { values, priority } => {
                    providers.push(Box::new(DefaultProvider::new(values, priority)));
                }
                ConfigSource::Memory { values, priority } => {
                    providers.push(Box::new(MemoryProvider::new(values, priority)));
                }
            }
        }

        Ok(ConfigManager::new(providers))
    }
}

impl<T: Configurable + Clone> Default for ConfigBuilder<T> {
    fn default() -> Self {
        Self::new()
    }
}