aws_multipart_upload/codec/
error.rs

1use std::convert::Infallible;
2use std::fmt::{Display, Formatter, Result};
3
4/// A trait that implements errors coming from encoding an item in a part.
5pub trait EncodeError: std::error::Error {
6    /// Human-readable error message.
7    fn message(&self) -> String;
8
9    /// Category of error.
10    fn kind(&self) -> EncodeErrorKind;
11}
12
13/// Categorizes the cause of an encoding error.
14#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
15pub enum EncodeErrorKind {
16    /// An I/O error.
17    Io,
18    /// Error in a representation of the data.
19    Data,
20    /// Received fewer bytes than expected.
21    Eof,
22    /// The origin of the error is not known.
23    #[default]
24    Unknown,
25}
26
27impl Display for EncodeErrorKind {
28    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
29        let x = match self {
30            Self::Io => "io",
31            Self::Data => "data",
32            Self::Eof => "eof",
33            Self::Unknown => "unknown",
34        };
35        write!(f, "{x}")
36    }
37}
38
39impl EncodeError for std::io::Error {
40    fn message(&self) -> String {
41        self.to_string()
42    }
43
44    fn kind(&self) -> EncodeErrorKind {
45        EncodeErrorKind::Io
46    }
47}
48
49impl EncodeError for Infallible {
50    fn message(&self) -> String {
51        "unbelievable".into()
52    }
53
54    fn kind(&self) -> EncodeErrorKind {
55        EncodeErrorKind::Unknown
56    }
57}
58
59impl EncodeError for serde_json::Error {
60    fn message(&self) -> String {
61        self.to_string()
62    }
63
64    fn kind(&self) -> EncodeErrorKind {
65        match self.classify() {
66            serde_json::error::Category::Data | serde_json::error::Category::Syntax => {
67                EncodeErrorKind::Data
68            }
69            serde_json::error::Category::Eof => EncodeErrorKind::Eof,
70            serde_json::error::Category::Io => EncodeErrorKind::Io,
71        }
72    }
73}