nuts_bytes/
error.rs

1// MIT License
2//
3// Copyright (c) 2023 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23use std::string::FromUtf8Error;
24use thiserror::Error;
25
26use crate::put_bytes::PutBytesError;
27use crate::take_bytes::TakeBytesError;
28
29/// Error type of the library.
30#[derive(Debug, Error)]
31pub enum Error {
32    /// Errors coming from [`TakeBytes`](crate::take_bytes::TakeBytes).
33    #[error(transparent)]
34    TakeBytes(#[from] TakeBytesError),
35
36    /// Errors coming from [`PutBytes`](crate::put_bytes::PutBytes).
37    #[error(transparent)]
38    PutBytes(#[from] PutBytesError),
39
40    /// Failed to deserialize into a `char`. The source `u32` cannot be
41    /// converted into a `char`.
42    #[error("the char is invalid, {0} is not a char")]
43    InvalidChar(u32),
44
45    /// Failed to deserialize into a string. The source byte data are not valid
46    /// UTF-8.
47    #[error("the string is invalid: {0}")]
48    InvalidString(#[source] FromUtf8Error),
49
50    /// A custom error occured.
51    #[error("{0}")]
52    Custom(Box<dyn std::error::Error + Send + Sync>),
53
54    /// Deserialized an invalid variant index.
55    /// There is no enum variant at the given index.
56    #[cfg(feature = "derive")]
57    #[error("invalid enum, no variant at {0}")]
58    InvalidVariantIndex(u32),
59}