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)]
37pub struct CacheConfig {
38 pub path: CachePathConfig,
39 pub format: CacheFormatConfig
40}
41
42/// Platform-specific path configuration
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct CachePathConfig {
45 pub windows: String,
46 pub linux: String,
47}
48
49/// File naming format configuration
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct CacheFormatConfig {
52 pub filename: String,
53 pub time: String
54}
55
56/// Cache lifecycle policy
57#[deprecated(note="This enumeration has been temporarily deprecated due to issues. You can use the CacheObject::delete() function to delete cache files.")]
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[allow(dead_code)]
60pub enum LifecyclePolicy {
61 /// Cache persists until program termination
62 ProgramTerminated,
63 /// Cache persists until it goes out of scope
64 Scope,
65 /// Cache never expires (default)
66 Never
67}
68
69impl CacheConfig {
70 /// Creates a new CacheConfig from JSON string
71 ///
72 /// # Parameters
73 /// - `json_config: &str` - JSON configuration string
74 ///
75 /// # Returns
76 /// New CacheConfig instance or error if parsing fails
77 pub fn new(json_config: &str) -> CacheResult<Self> {
78 let json_config = json_config
79 .trim()
80 .replace('\\', "/")
81 .replace(r#"\""#, r#"""#);
82
83 serde_json::from_str(&json_config)
84 .map_err(|e| CacheError::ConfigParse(format!("Failed to parse config: {}\nInput: {}", e, json_config)))
85 }
86
87 /// Creates a new CacheConfig from JSON string, falling back to default on error
88 ///
89 /// # Parameters
90 /// - `json_config: &str` - JSON configuration string
91 ///
92 /// # Returns
93 /// New CacheConfig instance (falls back to default on parse error)
94 pub fn new_or_default(json_config: &str) -> Self {
95 match Self::new(json_config) {
96 Ok(config) => config,
97 Err(_) => Self::default(),
98 }
99 }
100
101 /// Creates a new CacheConfig with default values
102 pub fn default() -> Self {
103 CacheConfig {
104 path: CachePathConfig {
105 windows: "%temp%/Rust/Cache".to_string(),
106 linux: "/tmp/Rust/Cache".to_string(),
107 },
108 format: CacheFormatConfig {
109 filename: "r{name}.{time}.cache".to_string(),
110 time: "%Y+%m+%d-%H+%M+%S".to_string()
111 }
112 }
113 }
114}