Skip to main content

serde_fixint/
error.rs

1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! Error type for the fixint codec.
6
7use serde::{de, ser};
8use std::fmt::{self, Display};
9
10/// Errors produced while serializing or deserializing with the fixint codec.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// A `serde` (de)serialization error carrying a message.
14    #[error("{0}")]
15    Message(String),
16
17    /// Reached the end of the input buffer before a value was fully read.
18    #[error("unexpected end of input")]
19    Eof,
20
21    /// A byte that must encode a `bool` was neither 0 nor 1.
22    #[error("invalid bool encoding: {0}")]
23    InvalidBool(u8),
24
25    /// The tag byte of an `Option` was neither 0 nor 1.
26    #[error("invalid option tag: {0}")]
27    InvalidOptionTag(u8),
28
29    /// A `char`/`str` payload was not valid UTF-8.
30    #[error("invalid utf-8 while decoding")]
31    InvalidUtf8,
32
33    /// A `char` payload did not contain exactly one scalar value.
34    #[error("invalid char encoding")]
35    InvalidChar,
36
37    /// The codec was asked to decode a self-describing value, which this
38    /// non-self-describing format cannot support.
39    #[error("deserialize_any is not supported by the fixint format")]
40    NotSupported,
41
42    /// Trailing bytes remained after a value was fully decoded.
43    #[error("{0} trailing byte(s) after decoded value")]
44    TrailingBytes(usize),
45}
46
47/// Convenience alias for codec results.
48pub type Result<T> = std::result::Result<T, Error>;
49
50impl ser::Error for Error {
51    fn custom<T: Display>(msg: T) -> Self {
52        Error::Message(msg.to_string())
53    }
54}
55
56impl de::Error for Error {
57    fn custom<T: Display>(msg: T) -> Self {
58        Error::Message(msg.to_string())
59    }
60}
61
62impl From<fmt::Error> for Error {
63    fn from(_: fmt::Error) -> Self {
64        Error::Message("formatting error".to_string())
65    }
66}