serde_beve/error.rs
1use crate::headers::{ArrayKind, ObjectKind};
2
3#[derive(Debug, thiserror::Error)]
4#[non_exhaustive]
5/// Errors that can occur during serialization or deserialization.
6pub enum Error {
7 #[error("{0}")]
8 /// A custom error message.
9 ///
10 /// This variant is never directly constructed by this library, but rather is used for serde's
11 /// `Error` traits[^1].
12 ///
13 /// [^1]: [`serde::ser::Error`] and [`serde::de::Error`]
14 Custom(String),
15
16 #[error("IO error")]
17 /// An IO error produced by an internal [`Read`](std::io::Read)er or [`Write`](std::io::Write)r.
18 Io(
19 #[from]
20 #[source]
21 std::io::Error,
22 ),
23
24 #[error("Keys must be strings or integers")]
25 /// Returned when attempting to serialize an object key that is not a string or integer.
26 InvalidKey,
27
28 #[error("Unsupported data type: {0}")]
29 /// Returned when trying to deserialize an unsupported data type.
30 ///
31 /// This can occur when trying to deserialize an [`f16`](half::f16) or a [`bf16`](half::bf16)
32 /// without the `half` feature enabled. It can also occur when trying to deserialize a 128-bit
33 /// float, as these are not yet stable in Rust.
34 UnsupportedDataType(SpecialType),
35
36 #[error("Invalid type. Expected {expected}, found {found}.")]
37 /// Returned when a type is found during deserialization that doesn't match the expected type
38 /// (e.g. [`deserialize_u8`](serde::Deserializer::deserialize_u8) is called but a `u16` header
39 /// is encountered).
40 WrongType {
41 expected: &'static str,
42 found: &'static str,
43 },
44
45 #[error("Mismatched key types. Expected {expected}, found {found}.")]
46 /// Returned when, during the serialization or deserialization of an object, a key is
47 /// encountered that doesn't match the first encountered key.
48 /// Returned when, during the serialization or deserialization of an object, a key is
49 ///
50 /// Objects in BEVE can be keyed by strings or integers, but all fields must be of the same
51 /// type.
52 MismatchedKeyType {
53 expected: ObjectKind,
54 found: ObjectKind,
55 },
56
57 #[error("Mismatched array type. Expected {expected}, found {found}.")]
58 MismatchedElementType {
59 expected: ArrayKind,
60 found: ArrayKind,
61 },
62
63 #[error("Object, array, or string too long")]
64 /// Returned either when trying to serialize data whose size is greater than (2^62)-1
65 /// bits or when trying to deserialize a size that is larger than the platform's pointer width
66 /// (e.g. trying to deserialize a 62-bit size on a 32-bit platform).
67 TooLong,
68
69 #[error("Invalid header: {0:08b}")]
70 /// Returned when a header is encountered that does not fit the BEVE format.
71 InvalidHeader(u8),
72
73 #[error("Invalid UTF-8 sequence")]
74 /// Returned when the deserialization of a [`str`] fails.
75 Utf8(
76 #[from]
77 #[source]
78 std::str::Utf8Error,
79 ),
80
81 #[error("Invalid UTF-8 sequence")]
82 /// Returned when the deserialization of a [`String`] fails.
83 FromUtf8(
84 #[from]
85 #[source]
86 std::string::FromUtf8Error,
87 ),
88
89 #[error("Cannot deserialize reserved")]
90 /// Returned when attempting to deserialize a reserved header.
91 Reserved,
92
93 #[error("Enum variant tags must be deserialized as identifiers")]
94 /// Returned when attempting to deserialize an enum tag as something other than an identifier.
95 InvalidTag,
96
97 #[error("No character")]
98 /// Returned when an empty string is attempted to be deserialized as a character.
99 NoChar,
100
101 #[error("Invalid complex header")]
102 /// Returned when an invalid complex header is encountered.
103 InvalidComplexHeader,
104
105 #[error("Invalid matrix member type")]
106 /// Returned when attempting to deserialize part of a matrix as an improper type.
107 InvalidMatrixType,
108}
109
110#[derive(Debug)]
111pub enum SpecialType {
112 /// A 16-bit float.
113 HalfFloat,
114 /// A [brain float](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format).
115 BrainFloat,
116 /// A 128-bit float.
117 F128,
118}
119
120impl std::fmt::Display for SpecialType {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 match self {
123 SpecialType::HalfFloat => write!(f, "16-bit float"),
124 SpecialType::F128 => write!(f, "128-bit float"),
125 SpecialType::BrainFloat => write!(f, "brain float"),
126 }
127 }
128}
129
130impl serde::ser::Error for Error {
131 fn custom<T>(msg: T) -> Self
132 where
133 T: std::fmt::Display,
134 {
135 Error::Custom(msg.to_string())
136 }
137}
138
139impl serde::de::Error for Error {
140 fn custom<T>(msg: T) -> Self
141 where
142 T: std::fmt::Display,
143 {
144 Error::Custom(msg.to_string())
145 }
146}
147
148pub type Result<T> = std::result::Result<T, Error>;