cache-lite 0.2.1

A cross-platform caching library for Rust with configurable storage, lifecycle, and file formatting
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
/*
 * @filename: lib.rs
 * @description: A cross-platform caching library for Rust with configurable storage, lifecycle, and file formatting.
 * @author: TaimWay <taimway@gmail.com>
 * 
 * Copyright (C) 2026 TaimWay
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

//! # Rust Cache Library
//! 
//! A lightweight, cross-platform caching library for Rust applications.
//! Provides configurable cache storage, lifecycle management, and file formatting.
//! 
//! # Features
//! 
//! - **Cross-platform**: Automatic path handling for Windows and Linux
//! - **Configurable**: JSON-based configuration with runtime overrides
//! - **Simple API**: Easy-to-use interface for cache operations
//! - **File-based**: Persistent cache storage on disk
//! 
//! # Quick Start
//! 
//! ```no_run
//! use cache_lite::{Cache, CacheConfig};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create cache with default configuration
//!     let config = CacheConfig::default();
//!     let mut cache = Cache::new(config)?;
//!
//!     // Create a new cache object
//!     let cache_obj = cache.create("my_cache", None).unwrap();
//!
//!     // Write data to cache
//!     cache_obj.write_string("Cached data").unwrap();
//!
//!     // Read data from cache
//!     let data = cache_obj.get_string().unwrap();
//!     assert_eq!(data, "Cached data");
//!
//!     cache_obj.delete()?;
//!
//!     Ok(())
//! }
//! ```
//! 
//! # Configuration
//! 
//! The library supports JSON configuration for customizing cache behavior:
//! 
//! ```json
//! {
//!   "path": {
//!     "windows": "%temp%/MyApp/Cache",
//!     "linux": "/tmp/myapp/cache"
//!   },
//!   "format": {
//!     "filename": "{name}_{time}.cache",
//!     "time": "%Y%m%d_%H%M%S"
//!   }
//! }
//! ```
//! 
//! # Error Handling
//! 
//! The library provides a comprehensive error type `CacheError` for handling
//! various failure scenarios including I/O errors, invalid configurations,
//! permission issues, and more.

mod config;
mod object;
mod cache;
mod error;
mod utils;

// Re-export public API
pub use config::{CacheConfig, CachePathConfig, CacheFormatConfig};
pub use object::CacheObject;
pub use cache::Cache;
pub use error::CacheError;

/// Result type alias for cache operations
pub type CacheResult<T> = std::result::Result<T, CacheError>;

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_cache_config_default() {
        let config = CacheConfig::default();
        assert_eq!(config.max_size, 0);
        assert_eq!(config.max_files, 0);
        assert!(!config.path.windows.is_empty());
        assert!(!config.path.linux.is_empty());
        assert!(!config.format.filename.is_empty());
        assert!(!config.format.time.is_empty());
    }

    #[test]
    fn test_cache_config_from_json() {
        let json = r#"{
            "path": {
                "windows": "%temp%/TestCache",
                "linux": "/tmp/testcache"
            },
            "format": {
                "filename": "test_{name}.cache",
                "time": "%Y%m%d"
            },
            "max_size": 1024,
            "max_files": 10
        }"#;

        let config = CacheConfig::new(json).unwrap();
        assert_eq!(config.path.windows, "%temp%/TestCache");
        assert_eq!(config.path.linux, "/tmp/testcache");
        assert_eq!(config.format.filename, "test_{name}.cache");
        assert_eq!(config.format.time, "%Y%m%d");
        assert_eq!(config.max_size, 1024);
        assert_eq!(config.max_files, 10);
    }

    #[test]
    fn test_cache_config_from_partial_json() {
        let json = r#"{
            "path": {
                "linux": "/custom/path"
            },
            "format": {
                "filename": "custom_{name}.cache"
            }
        }"#;

        let config = CacheConfig::new(json).unwrap();
        assert_eq!(config.path.linux, "/custom/path");
        assert_eq!(config.format.filename, "custom_{name}.cache");
        // Windows path should use default
        assert!(!config.path.windows.is_empty());
        // Time format should use default
        assert!(!config.format.time.is_empty());
    }

    #[test]
    fn test_cache_config_new_or_default() {
        let json = "invalid json";
        let config = CacheConfig::new_or_default(json);
        // Should fall back to default
        assert_eq!(config.max_size, 0);
        assert_eq!(config.max_files, 0);
    }

    #[test]
    fn test_cache_creation() {
        let temp_dir = tempdir().unwrap();
        let config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let config = CacheConfig::new(&config_json).unwrap();
        let mut cache = Cache::new(config).unwrap();

        // Test create cache object
        let cache_obj = cache.create("test_cache", None).unwrap();
        assert_eq!(cache_obj.name(), "test_cache");
        
        // Write some data to ensure file exists
        cache_obj.write_string("test data").unwrap();
        assert!(cache_obj.exists());

        // Test duplicate creation fails
        let result = cache.create("test_cache", None);
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(matches!(e, CacheError::AlreadyExists(_)));
        }

        // Test get cache object
        let retrieved = cache.get("test_cache").unwrap();
        assert_eq!(retrieved.name(), "test_cache");
        assert_eq!(retrieved.id(), cache_obj.id());

        // Test get non-existent cache
        let result = cache.get("nonexistent");
        assert!(result.is_err());
        if let Err(e) = result {
            assert!(matches!(e, CacheError::NotFound(_)));
        }

        // Test length and empty
        assert_eq!(cache.len(), 1);
        assert!(!cache.is_empty());

        // Test iterator
        let objects: Vec<_> = cache.iter().collect();
        assert_eq!(objects.len(), 1);
        assert_eq!(objects[0].name(), "test_cache");
    }

    #[test]
    fn test_cache_operations() {
        let temp_dir = tempdir().unwrap();
        let config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let config = CacheConfig::new(&config_json).unwrap();
        let mut cache = Cache::new(config).unwrap();

        // Create multiple cache objects
        cache.create("cache1", None).unwrap();
        cache.create("cache2", None).unwrap();
        cache.create("cache3", None).unwrap();

        assert_eq!(cache.len(), 3);

        // Test remove operation
        cache.remove("cache2").unwrap();
        assert_eq!(cache.len(), 2);
        assert!(cache.get("cache2").is_err());

        // Test clear operation
        cache.clear().unwrap();
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
    }

    #[test]
    fn test_cache_object_operations() {
        let temp_dir = tempdir().unwrap();
        let config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let config = CacheConfig::new(&config_json).unwrap();
        let mut cache = Cache::new(config).unwrap();
        let cache_obj = cache.create("test_operations", None).unwrap();

        // Test string operations
        let test_string = "Hello, World!";
        cache_obj.write_string(test_string).unwrap();

        let read_string = cache_obj.get_string().unwrap();
        assert_eq!(read_string, test_string);

        // Test binary operations
        let test_bytes = vec![1, 2, 3, 4, 5];
        cache_obj.write_bytes(&test_bytes).unwrap();

        let read_bytes = cache_obj.get_bytes().unwrap();
        assert_eq!(read_bytes, test_bytes);

        // Test file operations
        let file = cache_obj.get_file().unwrap();
        assert!(file.metadata().is_ok());

        // Test size
        let size = cache_obj.size().unwrap();
        assert!(size > 0);

        // Test delete
        cache_obj.delete().unwrap();
        assert!(!cache_obj.exists());

        // Test creation time
        let new_obj = cache.create("new_cache", None).unwrap();
        let created_at = new_obj.created_at();
        assert!(created_at.elapsed().is_ok());
    }

    #[test]
    fn test_cache_with_custom_config() {
        let temp_dir = tempdir().unwrap();
        let base_config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "base_{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let base_config = CacheConfig::new(&base_config_json).unwrap();
        let mut cache = Cache::new(base_config).unwrap();

        let custom_config = r#"{
            "path": {
                "linux": "/custom/path"
            },
            "format": {
                "filename": "custom_{name}_{id}.cache"
            }
        }"#;

        let cache_obj = cache.create("custom_cache", Some(custom_config)).unwrap();
        let path_str = cache_obj.path().to_string_lossy().to_string();

        // Write data to ensure file exists
        cache_obj.write_string("test").unwrap();

        // Check that custom format is used
        assert!(path_str.contains("custom_cache"));
        assert!(path_str.contains(".cache"));
    }

    #[test]
    fn test_cache_config_updates() {
        let config = CacheConfig::default();
        let mut cache = Cache::new(config).unwrap();

        let new_config_json = r#"{
            "path": {
                "windows": "%temp%/UpdatedCache",
                "linux": "/tmp/updatedcache"
            },
            "format": {
                "filename": "updated_{name}.cache",
                "time": "%H%M%S"
            },
            "max_size": 2048,
            "max_files": 20
        }"#;

        let new_config = CacheConfig::new(new_config_json).unwrap();
        cache.set_config(new_config.clone());

        let retrieved_config = cache.get_config();
        assert_eq!(retrieved_config.max_size, 2048);
        assert_eq!(retrieved_config.max_files, 20);
        assert_eq!(retrieved_config.format.filename, "updated_{name}.cache");
    }

    #[test]
    fn test_validate_name() {
        // Valid names
        assert!(crate::utils::validate_name("valid_name").is_ok());
        assert!(crate::utils::validate_name("valid123").is_ok());
        assert!(crate::utils::validate_name("a").is_ok());

        // Invalid names
        assert!(crate::utils::validate_name("").is_err());
        assert!(crate::utils::validate_name(&"a".repeat(256)).is_err());
        assert!(crate::utils::validate_name("test/name").is_err());
        assert!(crate::utils::validate_name("test\\name").is_err());
        assert!(crate::utils::validate_name("test..name").is_err());
        
        #[cfg(windows)]
        {
            assert!(crate::utils::validate_name("CON").is_err());
            assert!(crate::utils::validate_name("test:name").is_err());
            assert!(crate::utils::validate_name("test<name").is_err());
        }
    }

    #[test]
    fn test_error_handling() {
        // Test error creation
        let io_error = CacheError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "test"));
        assert_eq!(io_error.kind(), "io");

        let generic_error = CacheError::new("Test error");
        assert_eq!(generic_error.kind(), "generic");
        assert_eq!(generic_error.message(), "Test error");

        // Test error conversions
        let io_err: std::io::Error = std::io::Error::new(std::io::ErrorKind::Other, "test");
        let cache_err: CacheError = io_err.into();
        assert!(cache_err.is_io_error());

        let json_err = serde_json::from_str::<CacheConfig>("invalid json");
        assert!(json_err.is_err());
    }

    #[test]
    fn test_cache_object_clone() {
        let temp_dir = tempdir().unwrap();
        let config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let config = CacheConfig::new(&config_json).unwrap();
        let mut cache = Cache::new(config).unwrap();
        let cache_obj = cache.create("clone_test", None).unwrap();

        // Write data first
        cache_obj.write_string("test data").unwrap();

        // Test cloning
        let cloned = cache_obj.clone();
        assert_eq!(cloned.name(), cache_obj.name());
        assert_eq!(cloned.id(), cache_obj.id());
        assert_eq!(cloned.path(), cache_obj.path());

        // Check clone sees the same content
        let cloned_content = cloned.get_string().unwrap();
        assert_eq!(cloned_content, "test data");
    }

    #[test]
    fn test_expand_path() {
        // Test tilde expansion
        let path_with_tilde = "~/test/path";
        let expanded = crate::utils::expand_path(path_with_tilde);
        if let Some(home) = dirs::home_dir() {
            let home_str = home.to_string_lossy();
            assert!(expanded.starts_with(&*home_str));
        }

        // Test Windows env var expansion (only on Windows)
        #[cfg(windows)]
        {
            let path_with_env = "%temp%/test";
            let expanded = crate::utils::expand_path(path_with_env);
            assert!(!expanded.contains("%temp%"));
        }

        // Test path separator conversion
        let unix_path = "path/to/file";
        let expanded = crate::utils::expand_path(unix_path);
        
        #[cfg(windows)]
        assert!(expanded.contains('\\'));
        
        #[cfg(unix)]
        assert!(expanded.contains('/'));
    }

    #[test]
    fn test_cache_clear_with_errors() {
        let temp_dir = tempdir().unwrap();
        let config_json = format!(
            r#"{{
                "path": {{
                    "windows": "{}",
                    "linux": "{}"
                }},
                "format": {{
                    "filename": "{{name}}.cache",
                    "time": "%Y%m%d"
                }},
                "max_size": 0,
                "max_files": 0
            }}"#,
            temp_dir.path().to_string_lossy(),
            temp_dir.path().to_string_lossy()
        );

        let config = CacheConfig::new(&config_json).unwrap();
        let mut cache = Cache::new(config).unwrap();

        // Create a cache object
        let cache_obj = cache.create("test_clear", None).unwrap();
        
        // Write data to ensure file exists
        cache_obj.write_string("test data").unwrap();
        assert!(cache_obj.exists());

        // Clear should work
        cache.clear().unwrap();
        
        // Cache should be empty
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());
        
        // File should be deleted
        assert!(!cache_obj.exists());
    }

    #[test]
    fn test_error_matches() {
        // Test error matches
        let io_error = CacheError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "test"));
        assert!(io_error.is_io_error());

        let not_found_error = CacheError::NotFound("test".to_string());
        assert!(not_found_error.is_not_found());

        let permission_error = CacheError::PermissionDenied("test".to_string());
        assert!(permission_error.is_permission_denied());
    }

    #[test]
    fn test_config_serde_roundtrip() {
        let config = CacheConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        let parsed_config = CacheConfig::new(&json).unwrap();
        
        assert_eq!(config.max_size, parsed_config.max_size);
        assert_eq!(config.max_files, parsed_config.max_files);
        assert_eq!(config.path.windows, parsed_config.path.windows);
        assert_eq!(config.path.linux, parsed_config.path.linux);
        assert_eq!(config.format.filename, parsed_config.format.filename);
        assert_eq!(config.format.time, parsed_config.format.time);
    }
}