cds/arraystring/
errors.rs

1//! `ArrayString` error types.
2
3use core::{
4    clone::Clone,
5    cmp::{Eq, Ord, PartialEq, PartialOrd},
6    fmt::{Debug, Display, Formatter},
7    hash::Hash,
8    marker::Copy,
9};
10
11// ---------------------------------------------------------------------------
12
13/// An error returned when there is no enough spare capacity.
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
15pub struct InsufficientCapacityError;
16
17impl Display for InsufficientCapacityError {
18    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
19        write!(f, "arraystring insufficient capacity")
20    }
21}
22
23#[cfg(feature = "std")]
24#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
25impl std::error::Error for InsufficientCapacityError {}
26
27// ---------------------------------------------------------------------------
28
29/// An error returned from [`try_insert`] and [`try_insert_str`] methods.
30///
31/// [`try_insert`]: super::ArrayString::try_insert
32/// [`try_insert_str`]: super::ArrayString::try_insert_str
33#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
34pub enum InsertError {
35    /// Index is out of bounds, or doesn't lie on a character boundary.
36    InvalidIndex,
37
38    /// There is no spare capacity to accommodate a new element.
39    InsufficientCapacity,
40}
41
42impl Display for InsertError {
43    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
44        let s = match *self {
45            InsertError::InvalidIndex => "invalid index",
46            InsertError::InsufficientCapacity => "insufficient capacity",
47        };
48        write!(f, "arraystring insert error: {}", s)
49    }
50}
51
52#[cfg(feature = "std")]
53#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
54impl std::error::Error for InsertError {}
55
56// ---------------------------------------------------------------------------
57
58/// Index is invalid.
59///
60/// This error is returned when an index is out of bounds, or doesn't lie on a character boundary.
61#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
62pub struct IndexError;
63
64impl core::fmt::Display for IndexError {
65    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
66        write!(
67            f,
68            "arraystring index error: index is out of bounds or doesn't lie on character boundary"
69        )
70    }
71}
72
73#[cfg(feature = "std")]
74#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
75impl std::error::Error for IndexError {}
76
77#[cfg(all(test, feature = "std"))]
78mod testing {
79    use super::*;
80
81    #[test]
82    fn insufficient_capacity_error_display() {
83        let e = InsufficientCapacityError {};
84        let s = format!("{}", e);
85        assert_eq!(s, "arraystring insufficient capacity");
86    }
87
88    #[test]
89    fn insert_error_display() {
90        let e = InsertError::InvalidIndex;
91        let s = format!("{}", e);
92        assert_eq!(s, "arraystring insert error: invalid index");
93
94        let e = InsertError::InsufficientCapacity;
95        let s = format!("{}", e);
96        assert_eq!(s, "arraystring insert error: insufficient capacity");
97    }
98
99    #[test]
100    fn index_error() {
101        let e = IndexError {};
102        let s = format!("{}", e);
103        assert_eq!(
104            s,
105            "arraystring index error: index is out of bounds or doesn't lie on character boundary"
106        );
107    }
108}