1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[non_exhaustive]
12pub enum Error {
13 EmptyData,
15 NotSorted,
17 InvalidKey {
19 detail: String,
21 },
22}
23
24impl fmt::Display for Error {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::EmptyData => write!(f, "input data is empty"),
28 Self::NotSorted => write!(f, "input data is not sorted"),
29 Self::InvalidKey { detail } => write!(f, "invalid key: {detail}"),
30 }
31 }
32}
33
34impl std::error::Error for Error {}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn error_display() {
42 assert_eq!(Error::EmptyData.to_string(), "input data is empty");
43 assert_eq!(Error::NotSorted.to_string(), "input data is not sorted");
44 assert_eq!(
45 Error::InvalidKey {
46 detail: "overflow".into()
47 }
48 .to_string(),
49 "invalid key: overflow"
50 );
51 }
52
53 #[test]
54 fn error_is_send_sync() {
55 fn assert_send_sync<T: Send + Sync>() {}
56 assert_send_sync::<Error>();
57 }
58}