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
use super::Error;
/// Error when an operation expects exactly one record but finds multiple.
///
/// This occurs when:
/// - A query that should return one record returns multiple
/// - An operation explicitly requires a single result but gets more
#[derive(Debug)]
pub(super) struct InvalidRecordCount {
context: Option<Box<str>>,
}
impl std::error::Error for InvalidRecordCount {}
impl core::fmt::Display for InvalidRecordCount {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("invalid record count")?;
if let Some(ref ctx) = self.context {
write!(f, ": {}", ctx)?;
}
Ok(())
}
}
impl Error {
/// Creates an invalid record count error.
///
/// This is used when an operation expects exactly one record but finds multiple.
///
/// The context parameter provides information about the operation.
///
/// # Examples
///
/// ```
/// use toasty_core::Error;
///
/// let err = Error::invalid_record_count("expected 1 record, found 3");
/// assert!(err.is_invalid_record_count());
/// assert_eq!(err.to_string(), "invalid record count: expected 1 record, found 3");
/// ```
pub fn invalid_record_count(context: impl Into<String>) -> Error {
Error::from(super::ErrorKind::InvalidRecordCount(InvalidRecordCount {
context: Some(context.into().into()),
}))
}
/// Returns `true` if this error is an invalid record count error.
pub fn is_invalid_record_count(&self) -> bool {
matches!(self.kind(), super::ErrorKind::InvalidRecordCount(_))
}
}