1use core::fmt;
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum CodecErrorKind {
9 UnexpectedEof,
11 InvalidValue,
13 LengthOverflow,
15 TrailingBytes,
17 WriteFailed,
19 ReadFailed,
21}
22
23#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25pub struct CodecError {
26 kind: CodecErrorKind,
27 message: &'static str,
28}
29
30impl CodecError {
31 pub const fn new(kind: CodecErrorKind, message: &'static str) -> Self {
33 Self { kind, message }
34 }
35
36 pub const fn kind(&self) -> CodecErrorKind {
38 self.kind
39 }
40
41 pub const fn message(&self) -> &'static str {
43 self.message
44 }
45
46 pub const fn unexpected_eof() -> Self {
48 Self::new(
49 CodecErrorKind::UnexpectedEof,
50 "input ended before the requested bytes could be read",
51 )
52 }
53
54 pub const fn invalid_value(message: &'static str) -> Self {
56 Self::new(CodecErrorKind::InvalidValue, message)
57 }
58
59 pub const fn length_overflow(message: &'static str) -> Self {
61 Self::new(CodecErrorKind::LengthOverflow, message)
62 }
63
64 pub const fn trailing_bytes() -> Self {
66 Self::new(
67 CodecErrorKind::TrailingBytes,
68 "decode completed but trailing bytes remain",
69 )
70 }
71
72 pub const fn write_failed() -> Self {
74 Self::new(CodecErrorKind::WriteFailed, "failed to write encoded bytes")
75 }
76
77 pub const fn read_failed() -> Self {
79 Self::new(CodecErrorKind::ReadFailed, "failed to read encoded bytes")
80 }
81}
82
83impl fmt::Display for CodecError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.write_str(self.message)
86 }
87}
88
89#[cfg(feature = "std")]
90impl std::error::Error for CodecError {}