aws_multipart_upload/codec/
mod.rs1use crate::client::part::PartBody;
7
8use bytes::BufMut;
9
10#[cfg(feature = "csv")]
11#[cfg_attr(docsrs, doc(cfg(feature = "csv")))]
12mod csv_writer;
13#[cfg(feature = "csv")]
14#[cfg_attr(docsrs, doc(cfg(feature = "csv")))]
15pub use csv_writer::CsvEncoder;
16
17mod error;
18pub use error::{EncodeError, EncodeErrorKind};
19
20mod json_writer;
21pub use json_writer::JsonLinesEncoder;
22
23mod lines_writer;
24pub use lines_writer::LinesEncoder;
25
26pub trait PartEncoder<Item> {
28 type Error: EncodeError;
30
31 fn restore(&self) -> Result<Self, Self::Error>
33 where
34 Self: Sized;
35
36 fn encode(&mut self, item: Item) -> Result<usize, Self::Error>;
38
39 fn flush(&mut self) -> Result<(), Self::Error>;
41
42 fn into_body(self) -> Result<PartBody, Self::Error>;
44
45 fn clear(&self) -> Result<Self, Self::Error>
51 where
52 Self: Sized,
53 {
54 self.restore()
55 }
56}
57
58impl<T: AsRef<[u8]>> PartEncoder<T> for PartBody {
59 type Error = std::convert::Infallible;
60
61 fn restore(&self) -> Result<Self, Self::Error> {
62 let capacity = self.capacity();
63 Ok(Self::with_capacity(capacity))
64 }
65
66 fn encode(&mut self, item: T) -> Result<usize, Self::Error> {
67 let buf = item.as_ref();
68 let bytes = buf.len();
69 self.reserve(bytes);
70 self.put(buf);
71 Ok(bytes)
72 }
73
74 fn flush(&mut self) -> Result<(), Self::Error> {
75 Ok(())
76 }
77
78 fn into_body(self) -> Result<PartBody, Self::Error> {
79 Ok(self)
80 }
81}