Skip to main content

osal_rs_serde/
error.rs

1/***************************************************************************
2 *
3 * osal-rs-serde
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
18 *
19 ***************************************************************************/
20
21//! Error types for serialization and deserialization operations.
22
23use core::fmt::{self, Display, Debug};
24
25/// Result type for serialization/deserialization operations.
26pub type Result<T> = core::result::Result<T, Error>;
27
28/// Error types that can occur during serialization or deserialization.
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum Error {
31    /// Buffer is too small for the operation
32    BufferTooSmall,
33    
34    /// Unexpected end of data
35    UnexpectedEof,
36    
37    /// Invalid data format encountered
38    InvalidData,
39    
40    /// Type mismatch during deserialization
41    TypeMismatch,
42    
43    /// Value out of valid range
44    OutOfRange,
45    
46    /// Custom error with a static message
47    Custom(&'static str),
48    
49    /// Unsupported operation
50    Unsupported,
51}
52
53impl Display for Error {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        match self {
56            Error::BufferTooSmall => write!(f, "Buffer too small"),
57            Error::UnexpectedEof => write!(f, "Unexpected end of data"),
58            Error::InvalidData => write!(f, "Invalid data format"),
59            Error::TypeMismatch => write!(f, "Type mismatch"),
60            Error::OutOfRange => write!(f, "Value out of range"),
61            Error::Custom(msg) => write!(f, "Custom error: {}", msg),
62            Error::Unsupported => write!(f, "Unsupported operation"),
63        }
64    }
65}
66
67#[cfg(feature = "std")]
68impl std::error::Error for Error {}