Debian Codegraph @

third_party/rust/zerovec/src/error.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use core::any;
6 use core::fmt;
7
8 /// A generic error type to be used for decoding slices of ULE types
9 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
10 #[non_exhaustive]
11 pub enum ZeroVecError {
12 /// Attempted to parse a buffer into a slice of the given ULE type but its
13 /// length was not compatible
14 InvalidLength { ty: &'static str, len: usize },
15 /// The byte sequence provided for `ty` failed to parse correctly
16 ParseError { ty: &'static str },
17 /// The byte buffer was not in the appropriate format for VarZeroVec
18 VarZeroVecFormatError,
19 }
20
21 impl fmt::Display for ZeroVecError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
23 match *self {
24 ZeroVecError::InvalidLength { ty, len } => {
25 write!(f, "Invalid length {len} for slice of type {ty}")
26 }
27 ZeroVecError::ParseError { ty } => {
28 write!(f, "Could not parse bytes to slice of type {ty}")
29 }
30 ZeroVecError::VarZeroVecFormatError => {
31 write!(f, "Invalid format for VarZeroVec buffer")
32 }
33 }
34 }
35 }
36
37 impl ZeroVecError {
38 /// Construct a parse error for the given type
39 pub fn parse<T: ?Sized + 'static>() -> ZeroVecError {
40 ZeroVecError::ParseError {
41 ty: any::type_name::<T>(),
42 }
43 }
44
45 /// Construct an "invalid length" error for the given type and length
46 pub fn length<T: ?Sized + 'static>(len: usize) -> ZeroVecError {
47 ZeroVecError::InvalidLength {
48 ty: any::type_name::<T>(),
49 len,
50 }
51 }
52 }
53
54 #[cfg(feature = "std")]
55 impl ::std::error::Error for ZeroVecError {}