backbone_bucket/domain/entity/
file_lock.ext.rs1#[allow(unused_imports)]
4use super::*;
5
6impl FileLock {
7
8 pub fn is_valid(&self) -> bool {
14 self.status == LockStatus::Active && !self.is_expired()
15 }
16
17 pub fn is_expired(&self) -> bool {
19 self.expires_at <= Utc::now()
20 }
21
22 pub fn is_owned_by(&self, user_id: Uuid) -> bool {
24 self.user_id == user_id
25 }
26
27 pub fn can_refresh(&self) -> bool {
29 self.is_valid()
30 }
31
32 pub fn refresh(&mut self, duration: Duration) -> Result<(), LockError> {
34 if !self.can_refresh() {
35 return Err(LockError::ValidationFailed(
36 "Cannot refresh lock: lock is not active or has expired".to_string(),
37 ));
38 }
39
40 let now = Utc::now();
41 self.expires_at = now + duration;
42 self.refreshed_at = Some(now);
43 self.metadata.touch();
44
45 Ok(())
46 }
47
48 pub fn time_remaining(&self) -> Duration {
50 let remaining = self.expires_at - Utc::now();
51 if remaining < Duration::zero() {
52 Duration::zero()
53 } else {
54 remaining
55 }
56 }
57
58 pub fn check_invariants(&self) -> Result<(), Vec<&'static str>> {
60 let mut errors = Vec::new();
61
62 if self.expires_at <= self.locked_at {
64 errors.push("expires_at must be after locked_at");
65 }
66
67 if self.status == LockStatus::Active && self.is_expired() {
69 errors.push("active lock must not be expired (expires_at must be in the future)");
70 }
71
72 if errors.is_empty() { Ok(()) } else { Err(errors) }
73 }
74}