1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::fmt::{Debug, Display};
use std::io;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

use serde::{de, ser};

use crate::format::{Kind, UnknownSpecial};

/// All errors that Pot may return.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Payload is not a Pot payload.
    NotAPot,
    /// Data was written with an incompatible version.
    IncompatibleVersion,
    /// A generic error occurred.
    Message(String),
    /// Extra data appeared at the end of the input.
    TrailingBytes,
    /// Expected more data but encountered the end of the input.
    Eof,
    /// A numerical value could not be handled without losing precision or truncation.
    ImpreciseCastWouldLoseData,
    /// An IO error occurred.
    Io(io::Error),
    /// A sequence of unknown size cannot be serialized.
    SequenceSizeMustBeKnown,
    /// String data contained invalid UTF-8 characters.
    InvalidUtf8(String),
    /// An unknown kind was encountered. Generally a sign that something else has been parsed incorrectly.
    InvalidKind(u8),
    /// Encountered an unexpected atom kind.
    UnexpectedKind(Kind, Kind),
    /// A requested symbol id was not found.
    UnknownSymbol(u64),
    /// An unsupported byte count for a numeric type was encountered.
    UnsupportedByteCount(Kind, usize),
    /// An atom header was incorrectly formatted.
    InvalidAtomHeader,
    /// The amount of data read exceeds the configured maximum number of bytes.
    TooManyBytesRead,
    /// An unknown [`Special`](crate::format::Special) was encountered.
    UnknownSpecial(UnknownSpecial),
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NotAPot => f.write_str("not a pot: invalid header"),
            Error::IncompatibleVersion => f.write_str("incompatible version"),
            Error::Message(message) => f.write_str(message),
            Error::TrailingBytes => f.write_str("extra data at end of input"),
            Error::Eof => f.write_str("unexpected end of file"),
            Error::ImpreciseCastWouldLoseData => f.write_str("numerical data cannot fit"),
            Error::Io(io) => write!(f, "io error: {io}"),
            Error::SequenceSizeMustBeKnown => {
                f.write_str("serializing sequences of unknown size is unsupported")
            }
            Error::InvalidUtf8(err) => write!(f, "invalid utf8: {err}"),
            Error::InvalidKind(kind) => write!(f, "invalid kind: {kind}"),
            Error::UnexpectedKind(encountered, expected) => write!(
                f,
                "encountered atom kind {encountered:?}, expected {expected:?}"
            ),
            Error::UnknownSymbol(sym) => write!(f, "unknown symbol {sym}"),
            Error::InvalidAtomHeader => f.write_str("an atom header was incorrectly formatted"),
            Error::TooManyBytesRead => {
                f.write_str("the deserialized value is larger than the allowed allocation limit")
            }
            Error::UnsupportedByteCount(kind, count) => {
                write!(f, "unexpected {kind:?} byte count ({count})")
            }
            Error::UnknownSpecial(err) => Display::fmt(err, f),
        }
    }
}

impl std::error::Error for Error {}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Self::Io(err)
    }
}

impl ser::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

impl de::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

impl From<Utf8Error> for Error {
    fn from(err: Utf8Error) -> Self {
        Self::InvalidUtf8(err.to_string())
    }
}

impl From<FromUtf8Error> for Error {
    fn from(err: FromUtf8Error) -> Self {
        Self::InvalidUtf8(err.to_string())
    }
}

impl From<UnknownSpecial> for Error {
    fn from(err: UnknownSpecial) -> Self {
        Self::UnknownSpecial(err)
    }
}