Skip to main content

wincode/
error.rs

1//! Error types and helpers.
2use {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("Could not cast integer type to pointer sized type")]
38    PointerSizedReadError,
39    #[error(
40        "Encoded sequence length exceeded preallocation limit of {limit} bytes (needed {needed} \
41         bytes)"
42    )]
43    PreallocationSizeLimit { needed: usize, limit: usize },
44    #[error("Invalid tag encoding: {0}")]
45    InvalidTagEncoding(usize),
46    #[error("Invalid bool encoding: {0}")]
47    InvalidBoolEncoding(u8),
48    #[error("Sequence length would overflow length encoding scheme: {0}")]
49    LengthEncodingOverflow(&'static str),
50    #[error("Invalid value: {0}")]
51    InvalidValue(&'static str),
52    #[error("Invalid char lead: {0}")]
53    InvalidCharLead(u8),
54    #[error("Custom error: {0}")]
55    Custom(&'static str),
56    #[error("Zero-copy read would be unaligned")]
57    UnalignedPointerRead,
58    #[error("Tag value would overflow tag encoding scheme: {0}")]
59    TagEncodingOverflow(&'static str),
60}
61
62pub struct PreallocationError {
63    needed: usize,
64    limit: usize,
65}
66
67pub struct TagEncodingOverflow(pub &'static str);
68
69pub type Result<T> = core::result::Result<T, Error>;
70pub type WriteResult<T> = core::result::Result<T, WriteError>;
71pub type ReadResult<T> = core::result::Result<T, ReadError>;
72
73#[cold]
74pub const fn unaligned_pointer_read() -> ReadError {
75    ReadError::UnalignedPointerRead
76}
77
78#[cold]
79pub const fn preallocation_size_limit(needed: usize, limit: usize) -> PreallocationError {
80    PreallocationError { needed, limit }
81}
82
83#[cold]
84pub const fn read_length_encoding_overflow(max_length: &'static str) -> ReadError {
85    ReadError::LengthEncodingOverflow(max_length)
86}
87
88#[cold]
89pub const fn write_length_encoding_overflow(max_length: &'static str) -> WriteError {
90    WriteError::LengthEncodingOverflow(max_length)
91}
92
93#[cold]
94pub const fn pointer_sized_decode_error() -> ReadError {
95    ReadError::PointerSizedReadError
96}
97
98#[cold]
99pub const fn invalid_bool_encoding(byte: u8) -> ReadError {
100    ReadError::InvalidBoolEncoding(byte)
101}
102
103#[cold]
104pub const fn invalid_tag_encoding(tag: usize) -> ReadError {
105    ReadError::InvalidTagEncoding(tag)
106}
107
108#[cold]
109pub const fn invalid_utf8_encoding(error: Utf8Error) -> ReadError {
110    ReadError::InvalidUtf8Encoding(error)
111}
112
113#[cold]
114pub const fn invalid_char_lead(val: u8) -> ReadError {
115    ReadError::InvalidCharLead(val)
116}
117
118#[cold]
119pub const fn invalid_value(msg: &'static str) -> ReadError {
120    ReadError::InvalidValue(msg)
121}
122
123impl From<PreallocationError> for ReadError {
124    fn from(PreallocationError { needed, limit }: PreallocationError) -> ReadError {
125        ReadError::PreallocationSizeLimit { needed, limit }
126    }
127}
128
129impl From<PreallocationError> for WriteError {
130    fn from(PreallocationError { needed, limit }: PreallocationError) -> WriteError {
131        WriteError::PreallocationSizeLimit { needed, limit }
132    }
133}
134
135#[cold]
136pub const fn tag_encoding_overflow(encoding: &'static str) -> TagEncodingOverflow {
137    TagEncodingOverflow(encoding)
138}
139
140impl From<TagEncodingOverflow> for ReadError {
141    fn from(err: TagEncodingOverflow) -> Self {
142        ReadError::TagEncodingOverflow(err.0)
143    }
144}
145
146impl From<TagEncodingOverflow> for WriteError {
147    fn from(err: TagEncodingOverflow) -> Self {
148        WriteError::TagEncodingOverflow(err.0)
149    }
150}