1use {crate::io, core::str::Utf8Error, thiserror::Error};
3
4#[derive(Error, Debug)]
5pub enum Error {
6 #[error(transparent)]
7 WriteError(#[from] WriteError),
8 #[error(transparent)]
9 ReadError(#[from] ReadError),
10}
11
12#[derive(Error, Debug)]
13pub enum WriteError {
14 #[error(transparent)]
15 Io(#[from] io::WriteError),
16 #[error(transparent)]
17 InvalidUtf8Encoding(#[from] Utf8Error),
18 #[error("Sequence length would overflow length encoding scheme: {0}")]
19 LengthEncodingOverflow(&'static str),
20 #[error(
21 "Encoded sequence length exceeded preallocation limit of {limit} bytes (needed {needed} \
22 bytes)"
23 )]
24 PreallocationSizeLimit { needed: usize, limit: usize },
25 #[error("Tag value would overflow tag encoding scheme: {0}")]
26 TagEncodingOverflow(&'static str),
27 #[error("Custom error: {0}")]
28 Custom(&'static str),
29}
30
31#[derive(Error, Debug)]
32pub enum ReadError {
33 #[error(transparent)]
34 Io(#[from] io::ReadError),
35 #[error(transparent)]
36 InvalidUtf8Encoding(#[from] Utf8Error),
37 #[error("Decoded UTF-8 value {0} is not a valid character")]
38 InvalidUtf8Code(u32),
39 #[error("Could not cast integer type to pointer sized type")]
40 PointerSizedReadError,
41 #[error(
42 "Encoded sequence length exceeded preallocation limit of {limit} bytes (needed {needed} \
43 bytes)"
44 )]
45 PreallocationSizeLimit { needed: usize, limit: usize },
46 #[error("Invalid tag encoding: {0}")]
47 InvalidTagEncoding(usize),
48 #[error("Invalid bool encoding: {0}")]
49 InvalidBoolEncoding(u8),
50 #[error("Sequence length would overflow length encoding scheme: {0}")]
51 LengthEncodingOverflow(&'static str),
52 #[error("Invalid value: {0}")]
53 InvalidValue(&'static str),
54 #[error("Invalid char lead: {0}")]
55 InvalidCharLead(u8),
56 #[error("Custom error: {0}")]
57 Custom(&'static str),
58 #[error("Zero-copy read would be unaligned")]
59 UnalignedPointerRead,
60 #[error("Tag value would overflow tag encoding scheme: {0}")]
61 TagEncodingOverflow(&'static str),
62}
63
64pub struct PreallocationError {
65 needed: usize,
66 limit: usize,
67}
68
69pub struct TagEncodingOverflow(pub &'static str);
70
71pub type Result<T> = core::result::Result<T, Error>;
72pub type WriteResult<T> = core::result::Result<T, WriteError>;
73pub type ReadResult<T> = core::result::Result<T, ReadError>;
74
75#[cold]
76pub const fn unaligned_pointer_read() -> ReadError {
77 ReadError::UnalignedPointerRead
78}
79
80#[cold]
81pub const fn preallocation_size_limit(needed: usize, limit: usize) -> PreallocationError {
82 PreallocationError { needed, limit }
83}
84
85#[cold]
86pub const fn read_length_encoding_overflow(max_length: &'static str) -> ReadError {
87 ReadError::LengthEncodingOverflow(max_length)
88}
89
90#[cold]
91pub const fn write_length_encoding_overflow(max_length: &'static str) -> WriteError {
92 WriteError::LengthEncodingOverflow(max_length)
93}
94
95#[cold]
96pub const fn pointer_sized_decode_error() -> ReadError {
97 ReadError::PointerSizedReadError
98}
99
100#[cold]
101pub const fn invalid_bool_encoding(byte: u8) -> ReadError {
102 ReadError::InvalidBoolEncoding(byte)
103}
104
105#[cold]
106pub const fn invalid_tag_encoding(tag: usize) -> ReadError {
107 ReadError::InvalidTagEncoding(tag)
108}
109
110#[cold]
111pub const fn invalid_utf8_encoding(error: Utf8Error) -> ReadError {
112 ReadError::InvalidUtf8Encoding(error)
113}
114
115#[cold]
116pub const fn invalid_char_lead(val: u8) -> ReadError {
117 ReadError::InvalidCharLead(val)
118}
119
120#[cold]
121pub const fn invalid_value(msg: &'static str) -> ReadError {
122 ReadError::InvalidValue(msg)
123}
124
125impl From<PreallocationError> for ReadError {
126 fn from(PreallocationError { needed, limit }: PreallocationError) -> ReadError {
127 ReadError::PreallocationSizeLimit { needed, limit }
128 }
129}
130
131impl From<PreallocationError> for WriteError {
132 fn from(PreallocationError { needed, limit }: PreallocationError) -> WriteError {
133 WriteError::PreallocationSizeLimit { needed, limit }
134 }
135}
136
137#[cold]
138pub const fn tag_encoding_overflow(encoding: &'static str) -> TagEncodingOverflow {
139 TagEncodingOverflow(encoding)
140}
141
142impl From<TagEncodingOverflow> for ReadError {
143 fn from(err: TagEncodingOverflow) -> Self {
144 ReadError::TagEncodingOverflow(err.0)
145 }
146}
147
148impl From<TagEncodingOverflow> for WriteError {
149 fn from(err: TagEncodingOverflow) -> Self {
150 WriteError::TagEncodingOverflow(err.0)
151 }
152}