Skip to main content

yggdrasil/models/
settings.rs

1//! 回收站与站点配置模型。
2
3/// 默认保留天数(天)。
4pub const DEFAULT_RETENTION_DAYS: i32 = 30;
5/// 默认不启用自动清理。
6pub const DEFAULT_AUTO_PURGE_ENABLED: bool = false;
7/// 保留天数下限(天)。
8#[cfg(feature = "server")]
9pub const MIN_RETENTION_DAYS: i32 = 1;
10/// 保留天数上限(天)。防止误填超大值导致永不清理。
11#[cfg(feature = "server")]
12pub const MAX_RETENTION_DAYS: i32 = 365;
13
14/// 回收站配置。
15#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
16pub struct TrashSettings {
17    /// 是否启用自动定时清理。
18    pub auto_purge_enabled: bool,
19    /// 已删除文章保留天数,超过后被后台任务物理删除。
20    pub retention_days: i32,
21}
22
23impl Default for TrashSettings {
24    fn default() -> Self {
25        Self {
26            auto_purge_enabled: DEFAULT_AUTO_PURGE_ENABLED,
27            retention_days: DEFAULT_RETENTION_DAYS,
28        }
29    }
30}
31
32impl TrashSettings {
33    /// 将保留天数钳制到合法范围 [MIN, MAX]。
34    #[cfg(feature = "server")]
35    pub fn clamp_retention(days: i32) -> i32 {
36        days.clamp(MIN_RETENTION_DAYS, MAX_RETENTION_DAYS)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn default_is_disabled_30_days() {
46        let s = TrashSettings::default();
47        assert!(!s.auto_purge_enabled);
48        assert_eq!(s.retention_days, 30);
49    }
50
51    #[test]
52    #[cfg(feature = "server")]
53    fn clamp_retention_keeps_valid() {
54        assert_eq!(TrashSettings::clamp_retention(7), 7);
55        assert_eq!(TrashSettings::clamp_retention(30), 30);
56    }
57
58    #[test]
59    #[cfg(feature = "server")]
60    fn clamp_retention_clamps_below_min() {
61        assert_eq!(TrashSettings::clamp_retention(0), MIN_RETENTION_DAYS);
62        assert_eq!(TrashSettings::clamp_retention(-5), MIN_RETENTION_DAYS);
63    }
64
65    #[test]
66    #[cfg(feature = "server")]
67    fn clamp_retention_clamps_above_max() {
68        assert_eq!(TrashSettings::clamp_retention(366), MAX_RETENTION_DAYS);
69        assert_eq!(TrashSettings::clamp_retention(i32::MAX), MAX_RETENTION_DAYS);
70    }
71
72    #[test]
73    #[cfg(feature = "server")]
74    fn clamp_retention_boundary() {
75        assert_eq!(
76            TrashSettings::clamp_retention(MIN_RETENTION_DAYS),
77            MIN_RETENTION_DAYS
78        );
79        assert_eq!(
80            TrashSettings::clamp_retention(MAX_RETENTION_DAYS),
81            MAX_RETENTION_DAYS
82        );
83    }
84}