Skip to main content

cache_lite/
config.rs

1/*
2 * @filename: config.rs
3 * @description: Configuration structures for cache-lite library
4 * @author: TaimWay <taimway@gmail.com>
5 * 
6 * Copyright (C) 2026 TaimWay
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27use serde::{Deserialize, Serialize};
28use crate::{CacheError, CacheResult};
29
30/// Main configuration structure for cache behavior
31/// 
32/// # Fields
33/// - `path`: Platform-specific storage paths (Windows/Linux)
34/// - `format`: File naming format template
35/// - `lifecycle`: Cache lifecycle policy
36#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(default)]  
38pub struct CacheConfig {
39    pub path: CachePathConfig,
40    pub format: CacheFormatConfig,
41    pub max_size: u64,
42    pub max_files: usize
43}
44
45/// Platform-specific path configuration
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(default)]  
48pub struct CachePathConfig {
49    pub windows: String,
50    pub linux: String,
51}
52
53impl Default for CachePathConfig {
54    fn default() -> Self {
55        CachePathConfig {
56            windows: "%temp%/Rust/Cache".to_string(),
57            linux: "/tmp/Rust/Cache".to_string(),
58        }
59    }
60}
61
62/// File naming format configuration
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[serde(default)]  
65pub struct CacheFormatConfig {
66    pub filename: String,
67    pub time: String
68}
69
70impl Default for CacheFormatConfig {
71    fn default() -> Self {
72        CacheFormatConfig {
73            filename: "r{name}.{time}.cache".to_string(),
74            time: "%Y+%m+%d-%H+%M+%S".to_string()
75        }
76    }
77}
78
79impl Default for CacheConfig {
80    fn default() -> Self {
81        CacheConfig {
82            path: CachePathConfig::default(),
83            format: CacheFormatConfig::default(),
84            max_size: 0,  // 0 means no limit
85            max_files: 0, // 0 means no limit
86        }
87    }
88}
89
90impl CacheConfig {
91    /// Creates a new CacheConfig from JSON string
92    /// 
93    /// # Parameters
94    /// - `json_config: &str` - JSON configuration string
95    /// 
96    /// # Returns
97    /// New CacheConfig instance or error if parsing fails
98    pub fn new(json_config: &str) -> CacheResult<Self> {
99        let json_config = json_config
100            .trim()
101            .replace('\\', "/") 
102            .replace(r#"\""#, r#"""#); 
103        
104        serde_json::from_str(&json_config)
105            .map_err(|e| CacheError::ConfigParse(format!("Failed to parse config: {}\nInput: {}", e, json_config)))
106    }
107    
108    /// Creates a new CacheConfig from JSON string, falling back to default on error
109    /// 
110    /// # Parameters
111    /// - `json_config: &str` - JSON configuration string
112    /// 
113    /// # Returns
114    /// New CacheConfig instance (falls back to default on parse error)
115    pub fn new_or_default(json_config: &str) -> Self {
116        match Self::new(json_config) {
117            Ok(config) => config,
118            Err(_) => Self::default(),
119        }
120    }
121}