aws_multipart_upload/encoder/
mod.rs1use 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
31pub trait PartEncoder<Item> {
33 type Error: EncodeError;
35
36 fn encode(
41 &mut self,
42 part: &mut PartBody,
43 part_number: PartNumber,
44 item: Item,
45 ) -> Result<(), Self::Error>;
46}
47
48pub trait EncodeError: std::error::Error {
50 fn message(&self) -> String;
52
53 fn kind(&self) -> EncodeErrorKind;
55}
56
57#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
59pub enum EncodeErrorKind {
60 Io,
62 Data,
64 Eof,
66 #[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}