unistore_cache/error.rs
1//! 【错误类型】- 缓存相关错误定义
2//!
3//! 职责:
4//! - 定义缓存操作的错误类型
5
6use thiserror::Error;
7
8/// 缓存错误
9#[derive(Debug, Error)]
10pub enum CacheError {
11 /// 键不存在
12 #[error("键不存在: {key}")]
13 KeyNotFound { key: String },
14
15 /// 值已过期
16 #[error("值已过期")]
17 Expired,
18
19 /// 缓存已满且无法淘汰
20 #[error("缓存已满")]
21 CacheFull,
22
23 /// 无效的配置
24 #[error("无效的配置: {0}")]
25 InvalidConfig(String),
26}
27
28impl CacheError {
29 /// 检查是否是键不存在错误
30 pub fn is_not_found(&self) -> bool {
31 matches!(self, Self::KeyNotFound { .. })
32 }
33
34 /// 检查是否是过期错误
35 pub fn is_expired(&self) -> bool {
36 matches!(self, Self::Expired)
37 }
38}