Skip to main content

aws_multipart_upload/encoder/
mod.rs

1//! Encoding parts in an upload.
2//!
3//! This module defines [`PartEncoder`] and a few select implementations. A
4//! `PartEncoder` describes how an item should be written as bytes to a part
5//! upload request body.
6use std::convert::Infallible;
7use std::fmt::{self, Display, Formatter};
8
9use crate::client::part::{PartBody, PartNumber};
10
11#[cfg(feature = "csv")]
12#[cfg_attr(docsrs, doc(cfg(feature = "csv")))]
13mod csv_encoder;
14#[cfg(feature = "csv")]
15#[cfg_attr(docsrs, doc(cfg(feature = "csv")))]
16pub use csv_encoder::CsvEncoder;
17
18#[cfg(feature = "tokio-util")]
19#[cfg_attr(docsrs, doc(cfg(feature = "tokio-util")))]
20mod framed_encoder;
21#[cfg(feature = "tokio-util")]
22#[cfg_attr(docsrs, doc(cfg(feature = "tokio-util")))]
23pub use framed_encoder::FramedEncoder;
24
25mod json_encoder;
26pub use json_encoder::JsonLinesEncoder;
27
28mod lines_encoder;
29pub use lines_encoder::LinesEncoder;
30
31/// Encodes items in the body of a part upload request.
32pub trait PartEncoder<Item> {
33    /// The type of value returned when encoding fails.
34    type Error: EncodeError;
35
36    /// Encodes an item in the provided part.
37    ///
38    /// The part is in the context of an active upload, and its part number is
39    /// provided in case the encoding behavior depends on it.
40    fn encode(
41        &mut self,
42        part: &mut PartBody,
43        part_number: PartNumber,
44        item: Item,
45    ) -> Result<(), Self::Error>;
46}
47
48/// A trait that implements errors coming from encoding an item in a part.
49pub trait EncodeError: std::error::Error {
50    /// Human-readable error message.
51    fn message(&self) -> String;
52
53    /// Category of error.
54    fn kind(&self) -> EncodeErrorKind;
55}
56
57/// Categorizes the cause of an encoding error.
58#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
59pub enum EncodeErrorKind {
60    /// An I/O error.
61    Io,
62    /// Error in a representation of the data.
63    Data,
64    /// Received fewer bytes than expected.
65    Eof,
66    /// The origin of the error is not known.
67    #[default]
68    Unknown,
69}
70
71impl Display for EncodeErrorKind {
72    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
73        let x = match self {
74            Self::Io => "io",
75            Self::Data => "data",
76            Self::Eof => "eof",
77            Self::Unknown => "unknown",
78        };
79        write!(f, "{x}")
80    }
81}
82
83impl EncodeError for std::io::Error {
84    fn message(&self) -> String {
85        self.to_string()
86    }
87
88    fn kind(&self) -> EncodeErrorKind {
89        EncodeErrorKind::Io
90    }
91}
92
93impl EncodeError for Infallible {
94    fn message(&self) -> String {
95        "unbelievable".into()
96    }
97
98    fn kind(&self) -> EncodeErrorKind {
99        EncodeErrorKind::Unknown
100    }
101}
102
103impl EncodeError for serde_json::Error {
104    fn message(&self) -> String {
105        self.to_string()
106    }
107
108    fn kind(&self) -> EncodeErrorKind {
109        match self.classify() {
110            serde_json::error::Category::Data
111            | serde_json::error::Category::Syntax => EncodeErrorKind::Data,
112            serde_json::error::Category::Eof => EncodeErrorKind::Eof,
113            serde_json::error::Category::Io => EncodeErrorKind::Io,
114        }
115    }
116}