s2n_quic_core/buffer/
error.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4#[derive(Debug, PartialEq, Eq, Clone, Copy)]
5pub enum Error<E = core::convert::Infallible> {
6    /// An invalid data range was provided
7    OutOfRange,
8    /// The provided final size was invalid for the buffer's state
9    InvalidFin,
10    /// The provided reader failed
11    ReaderError(E),
12}
13
14impl<E> From<E> for Error<E> {
15    #[inline]
16    fn from(reader: E) -> Self {
17        Self::ReaderError(reader)
18    }
19}
20
21impl Error {
22    /// Maps from an infallible error into a more specific error
23    #[inline]
24    pub fn mapped<E>(error: Error) -> Error<E> {
25        match error {
26            Error::OutOfRange => Error::OutOfRange,
27            Error::InvalidFin => Error::InvalidFin,
28            Error::ReaderError(_) => unreachable!(),
29        }
30    }
31}
32
33#[cfg(feature = "std")]
34impl<E: std::error::Error> std::error::Error for Error<E> {}
35
36impl<E: core::fmt::Display> core::fmt::Display for Error<E> {
37    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
38        match self {
39            Self::OutOfRange => write!(f, "write extends out of the maximum possible offset"),
40            Self::InvalidFin => write!(
41                f,
42                "write modifies the final offset in a non-compliant manner"
43            ),
44            Self::ReaderError(reader) => write!(f, "the provided reader failed with: {reader}"),
45        }
46    }
47}
48
49#[cfg(feature = "std")]
50impl<E: 'static + std::error::Error + Send + Sync> From<Error<E>> for std::io::Error {
51    #[inline]
52    fn from(error: Error<E>) -> Self {
53        let kind = match &error {
54            Error::OutOfRange => std::io::ErrorKind::InvalidData,
55            Error::InvalidFin => std::io::ErrorKind::InvalidData,
56            Error::ReaderError(_) => std::io::ErrorKind::Other,
57        };
58        Self::new(kind, error)
59    }
60}