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};
28
29/// Main configuration structure for cache behavior
30/// 
31/// # Fields
32/// - `path`: Platform-specific storage paths (Windows/Linux)
33/// - `format`: File naming format template
34/// - `lifecycle`: Cache lifecycle policy
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct CacheConfig {
37    pub path: CachePathConfig,
38    pub format: CacheFormatConfig
39}
40
41/// Platform-specific path configuration
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct CachePathConfig {
44    pub windows: String,
45    pub linux: String,
46}
47
48/// File naming format configuration
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct CacheFormatConfig {
51    pub filename: String,
52    pub time: String
53}
54
55/// Cache lifecycle policy
56#[deprecated(note="This enumeration has been temporarily deprecated due to issues. You can use the CacheObject::delete() function to delete cache files.")]
57#[derive(Debug, Clone, Serialize, Deserialize)]
58#[allow(dead_code)]
59pub enum LifecyclePolicy {
60    /// Cache persists until program termination
61    ProgramTerminated,
62    /// Cache persists until it goes out of scope
63    Scope,
64    /// Cache never expires (default)
65    Never
66}
67
68impl CacheConfig {
69    /// Creates a new CacheConfig from JSON string
70    /// 
71    /// # Parameters
72    /// - `json_config: &str` - JSON configuration string
73    /// 
74    /// # Returns
75    /// New CacheConfig instance
76    pub fn new(json_config: &str) -> Self {
77        // Parse JSON configuration
78        serde_json::from_str(json_config)
79            .unwrap_or_else(|_| Self::default())
80    }
81    
82    /// Creates a new CacheConfig with default values
83    pub fn default() -> Self {
84        CacheConfig {
85            path: CachePathConfig {
86                windows: "%temp%/Rust/Cache".to_string(),
87                linux: "/tmp/Rust/Cache".to_string(),
88            },
89            format: CacheFormatConfig {
90                filename: "r{name}.{time}.cache".to_string(),
91                time: "%Y+%m+%d-%H+%M+%S".to_string()
92            }
93        }
94    }
95}