Skip to main content

cache_lite/
object.rs

1/*
2 * @filename: object.rs
3 * @description: Cache object implementation 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 std::path::{Path, PathBuf};
28use std::time::SystemTime;
29use crate::{CacheError, CacheResult};
30
31/// Represents an individual cache object with file operations
32pub struct CacheObject {
33    name: String,
34    path: PathBuf,
35    id: u32,
36    created_at: SystemTime
37}
38
39impl CacheObject {
40    /// Creates a new CacheObject
41    pub fn new(
42        name: String, 
43        path: PathBuf, 
44        id: u32
45    ) -> Self {
46        let obj = CacheObject {
47            name,
48            path,
49            id,
50            created_at: SystemTime::now()
51        };
52        
53        obj
54    }
55
56    /// Returns the cache object name
57    /// 
58    /// # Returns
59    /// `&str` - Cache object identifier
60    pub fn name(&self) -> &str {
61        &self.name
62    }
63
64    /// Returns the filesystem path of the cache object
65    /// 
66    /// # Returns
67    /// `&Path` - Path to cache file
68    pub fn path(&self) -> &Path {
69        &self.path
70    }
71
72    /// Returns the creation time of the cache object
73    /// 
74    /// # Returns
75    /// `SystemTime` - Creation timestamp
76    pub fn created_at(&self) -> SystemTime {
77        self.created_at
78    }
79
80    /// Returns the cache object ID
81    /// 
82    /// # Returns
83    /// `u32` - Unique identifier
84    pub fn id(&self) -> u32 {
85        self.id
86    }
87
88    /// Opens the cache file for reading/writing
89    /// 
90    /// # Returns
91    /// `CacheResult<std::fs::File>` - File handle or error
92    pub fn get_file(&self) -> CacheResult<std::fs::File> {
93        std::fs::OpenOptions::new()
94            .read(true)
95            .write(true)
96            .create(true)
97            .open(&self.path)
98            .map_err(|e| CacheError::Io(e))
99    }
100
101    /// Reads and returns the entire cache content as string
102    /// 
103    /// # Returns
104    /// `CacheResult<String>` - Cache content or error
105    pub fn get_string(&self) -> CacheResult<String> {
106        std::fs::read_to_string(&self.path)
107            .map_err(|e| CacheError::Io(e))
108    }
109
110    /// Writes string content to the cache file
111    /// 
112    /// # Parameters
113    /// - `content: &str` - Content to write
114    /// 
115    /// # Returns
116    /// `CacheResult<()>` - Success or error
117    pub fn write_string(&self, content: &str) -> CacheResult<()> {
118        std::fs::write(&self.path, content)
119            .map_err(|e| CacheError::Io(e))
120    }
121
122    /// Writes binary content to the cache file
123    /// 
124    /// # Parameters
125    /// - `content: &[u8]` - Binary content to write
126    /// 
127    /// # Returns
128    /// `CacheResult<()>` - Success or error
129    pub fn write_bytes(&self, content: &[u8]) -> CacheResult<()> {
130        std::fs::write(&self.path, content)
131            .map_err(|e| CacheError::Io(e))
132    }
133
134    /// Reads and returns the entire cache content as bytes
135    /// 
136    /// # Returns
137    /// `CacheResult<Vec<u8>>` - Cache content or error
138    pub fn get_bytes(&self) -> CacheResult<Vec<u8>> {
139        std::fs::read(&self.path)
140            .map_err(|e| CacheError::Io(e))
141    }
142
143    /// Deletes the cache object and its file
144    /// 
145    /// # Returns
146    /// `CacheResult<()>` - Success or error
147    pub fn delete(&self) -> CacheResult<()> {
148        if self.path.exists() {
149            std::fs::remove_file(&self.path)
150                .map_err(|e| CacheError::Io(e))?;
151        }
152        Ok(())
153    }
154
155    /// Checks if the cache file exists
156    /// 
157    /// # Returns
158    /// `bool` - True if the cache file exists
159    pub fn exists(&self) -> bool {
160        self.path.exists()
161    }
162
163    /// Gets the file size in bytes
164    /// 
165    /// # Returns
166    /// `CacheResult<u64>` - File size in bytes or error
167    pub fn size(&self) -> CacheResult<u64> {
168        std::fs::metadata(&self.path)
169            .map(|metadata| metadata.len())
170            .map_err(|e| CacheError::Io(e))
171    }
172
173    /// Checks if the cache has expired based on its lifecycle policy
174    /// 
175    /// # Returns
176    /// `bool` - True if expired, false otherwise
177    #[deprecated(note="This enumeration has been deprecated due to issues, and it now only returns false")]
178    pub fn is_expired(&self) -> bool {
179        false
180    }
181}
182
183impl Clone for CacheObject {
184    fn clone(&self) -> Self {
185        CacheObject {
186            name: self.name.clone(),
187            path: self.path.clone(),
188            id: self.id,
189            created_at: self.created_at
190        }
191    }
192}