Skip to main content

tokio_agent_memory/
error.rs

1#[derive(Debug, thiserror::Error)]
2#[non_exhaustive]
3pub enum MemoryError {
4    #[error("Memory item '{0}' not found")]
5    NotFound(String),
6    #[error("Working memory at capacity ({0} slots)")]
7    CapacityExceeded(usize),
8    #[error("Serialization failed: {0}")]
9    Serialization(String),
10    #[error("IO error: {0}")]
11    Io(#[from] std::io::Error),
12    #[error("Lock acquisition failed for key '{0}'")]
13    LockFailed(String),
14    #[error("Lease expired for key '{0}'")]
15    LeaseExpired(String),
16    #[error("Concept not found: '{0}'")]
17    ConceptNotFound(String),
18    #[error("Invalid confidence value {0}: must be 0.0–1.0")]
19    InvalidConfidence(f32),
20    #[error("Invalid decay rate {0}: must be > 0.0")]
21    InvalidDecayRate(f64),
22    #[error("Send error: channel closed")]
23    ChannelClosed,
24    #[error("Embedding failed: {0}")]
25    Embedding(String),
26    #[error("Working memory capacity must be > 0")]
27    ZeroCapacity,
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_not_found_display() {
36        let e = MemoryError::NotFound("abc".into());
37        assert!(e.to_string().contains("abc"));
38    }
39
40    #[test]
41    fn test_capacity_exceeded_display() {
42        let e = MemoryError::CapacityExceeded(8);
43        assert!(e.to_string().contains("8"));
44    }
45
46    #[test]
47    fn test_invalid_confidence_display() {
48        let e = MemoryError::InvalidConfidence(1.5);
49        assert!(e.to_string().contains("1.5"));
50    }
51
52    #[test]
53    fn test_invalid_decay_rate_display() {
54        let e = MemoryError::InvalidDecayRate(-0.1);
55        assert!(e.to_string().contains("-0.1"));
56    }
57
58    #[test]
59    fn test_lease_expired_display() {
60        let e = MemoryError::LeaseExpired("my-key".into());
61        assert!(e.to_string().contains("my-key"));
62    }
63
64    #[test]
65    fn test_channel_closed_display() {
66        let e = MemoryError::ChannelClosed;
67        assert!(e.to_string().contains("channel closed"));
68    }
69
70    #[test]
71    fn test_zero_capacity_display() {
72        let e = MemoryError::ZeroCapacity;
73        assert!(!e.to_string().is_empty());
74    }
75}