Repository navigation
Expand file tree
/
Copy patherror.rs
More file actions
262 lines (225 loc) · 6.98 KB
/
Copy patherror.rs
File metadata and controls
262 lines (225 loc) · 6.98 KB
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
//! Error types for the cachekit library.
//!
//! ## Key Components
//!
//! - [`InvariantError`]: Returned when internal data-structure invariants are
//! violated (debug-only `check_invariants` methods).
//! - [`ConfigError`]: Returned when cache configuration parameters are invalid
//! (e.g. zero capacity, out-of-range ratios).
//!
//! ## Example Usage
//!
//! ```
//! use cachekit::error::ConfigError;
//! use cachekit::policy::s3_fifo::S3FifoCache;
//!
//! // Fallible constructor for user-configurable parameters
//! let cache: Result<S3FifoCache<String, i32>, ConfigError> =
//! S3FifoCache::try_with_ratios(100, 0.1, 0.9);
//! assert!(cache.is_ok());
//!
//! // Invalid ratio is caught without panicking
//! let bad = S3FifoCache::<String, i32>::try_with_ratios(100, 2.0, 0.9);
//! assert!(bad.is_err());
//! ```
use std::fmt;
// ---------------------------------------------------------------------------
// InvariantError
// ---------------------------------------------------------------------------
/// Error returned when internal cache invariants are violated.
///
/// Produced by debug-only `check_invariants` methods on cache types
/// (e.g. [`S3FifoCache::check_invariants`](crate::policy::s3_fifo::S3FifoCache::check_invariants)).
/// Carries a human-readable description of which invariant failed.
///
/// # Example
///
/// ```
/// use cachekit::error::InvariantError;
///
/// let err = InvariantError::new("queue length mismatch");
/// assert_eq!(err.to_string(), "queue length mismatch");
/// assert_eq!(err.message(), "queue length mismatch");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InvariantError(String);
impl InvariantError {
/// Creates a new `InvariantError` with the given description.
///
/// By convention, `msg` should be lowercase and without trailing
/// punctuation (e.g. `"queue length mismatch"`).
#[inline]
pub fn new(msg: impl Into<String>) -> Self {
Self(msg.into())
}
/// Returns the error description.
#[inline]
pub fn message(&self) -> &str {
&self.0
}
}
impl fmt::Display for InvariantError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for InvariantError {}
impl From<String> for InvariantError {
#[inline]
fn from(msg: String) -> Self {
Self(msg)
}
}
impl From<&str> for InvariantError {
#[inline]
fn from(msg: &str) -> Self {
Self(msg.to_owned())
}
}
// ---------------------------------------------------------------------------
// ConfigError
// ---------------------------------------------------------------------------
/// Error returned when cache configuration parameters are invalid.
///
/// Produced by fallible constructors such as
/// [`S3FifoCache::try_with_ratios`](crate::policy::s3_fifo::S3FifoCache::try_with_ratios)
/// and builder `try_build()` methods. Carries a human-readable description of
/// which parameter failed validation.
///
/// # Example
///
/// ```
/// use cachekit::error::ConfigError;
/// use cachekit::policy::s3_fifo::S3FifoCache;
///
/// let err = S3FifoCache::<u64, u64>::try_with_ratios(0, 0.1, 0.9).unwrap_err();
/// assert!(err.to_string().contains("capacity"));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConfigError(String);
impl ConfigError {
/// Creates a new `ConfigError` with the given description.
///
/// By convention, `msg` should be lowercase and without trailing
/// punctuation (e.g. `"capacity must be greater than zero"`).
#[inline]
pub fn new(msg: impl Into<String>) -> Self {
Self(msg.into())
}
/// Returns the error description.
#[inline]
pub fn message(&self) -> &str {
&self.0
}
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for ConfigError {}
impl From<String> for ConfigError {
#[inline]
fn from(msg: String) -> Self {
Self(msg)
}
}
impl From<&str> for ConfigError {
#[inline]
fn from(msg: &str) -> Self {
Self(msg.to_owned())
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
// -- InvariantError ---------------------------------------------------
#[test]
fn invariant_display_shows_message() {
let err = InvariantError::new("queue length mismatch");
assert_eq!(err.to_string(), "queue length mismatch");
}
#[test]
fn invariant_debug_includes_message() {
let err = InvariantError::new("bad pointer");
let dbg = format!("{:?}", err);
assert!(dbg.contains("bad pointer"));
}
#[test]
fn invariant_message_accessor() {
let err = InvariantError::new("test");
assert_eq!(err.message(), "test");
}
#[test]
fn invariant_clone_and_eq() {
let a = InvariantError::new("x");
let b = a.clone();
assert_eq!(a, b);
}
#[test]
fn invariant_implements_std_error() {
fn assert_error<T: std::error::Error>() {}
assert_error::<InvariantError>();
}
#[test]
fn invariant_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<InvariantError>();
}
#[test]
fn invariant_from_string() {
let err = InvariantError::from(String::from("from string"));
assert_eq!(err.message(), "from string");
}
#[test]
fn invariant_from_str() {
let err = InvariantError::from("from str");
assert_eq!(err.message(), "from str");
}
// -- ConfigError ------------------------------------------------------
#[test]
fn config_display_shows_message() {
let err = ConfigError::new("capacity must be > 0");
assert_eq!(err.to_string(), "capacity must be > 0");
}
#[test]
fn config_debug_includes_message() {
let err = ConfigError::new("bad ratio");
let dbg = format!("{:?}", err);
assert!(dbg.contains("bad ratio"));
}
#[test]
fn config_message_accessor() {
let err = ConfigError::new("test");
assert_eq!(err.message(), "test");
}
#[test]
fn config_clone_and_eq() {
let a = ConfigError::new("x");
let b = a.clone();
assert_eq!(a, b);
}
#[test]
fn config_implements_std_error() {
fn assert_error<T: std::error::Error>() {}
assert_error::<ConfigError>();
}
#[test]
fn config_is_send_and_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<ConfigError>();
}
#[test]
fn config_from_string() {
let err = ConfigError::from(String::from("from string"));
assert_eq!(err.message(), "from string");
}
#[test]
fn config_from_str() {
let err = ConfigError::from("from str");
assert_eq!(err.message(), "from str");
}
}