aws_multipart_upload/codec/
lines_writer.rs1use crate::AWS_MIN_PART_SIZE;
2use crate::client::part::PartBody;
3use crate::codec::PartEncoder;
4
5use bytes::BufMut as _;
6use std::convert::Infallible;
7use std::ops::DerefMut;
8
9#[derive(Debug, Clone)]
12pub struct LinesEncoder {
13 writer: PartBody,
14 header: Option<String>,
15}
16
17impl LinesEncoder {
18 pub fn with_header<T: Into<String>>(self, header: T) -> Self {
20 Self {
21 header: Some(header.into()),
22 ..self
23 }
24 }
25}
26
27impl Default for LinesEncoder {
28 fn default() -> Self {
29 Self {
30 writer: PartBody::with_capacity(AWS_MIN_PART_SIZE.as_u64() as usize),
31 header: None,
32 }
33 }
34}
35
36impl<Item: AsRef<str>> PartEncoder<Item> for LinesEncoder {
37 type Error = Infallible;
38
39 fn restore(&self) -> Result<Self, Self::Error> {
40 let capacity = self.writer.capacity();
41 let mut writer = PartBody::with_capacity(capacity);
42 if let Some(h) = self.header.as_deref() {
43 writer.put(h.as_bytes());
44 }
45 Ok(Self {
46 writer,
47 header: self.header.clone(),
48 })
49 }
50
51 fn encode(&mut self, item: Item) -> Result<usize, Self::Error> {
52 let item = item.as_ref();
53 let bytes = item.len();
54 self.writer.deref_mut().reserve(bytes + 1);
55 self.writer.deref_mut().put(item.as_bytes());
56 self.writer.deref_mut().put_u8(b'\n');
57 Ok(bytes + 1)
58 }
59
60 fn flush(&mut self) -> Result<(), Self::Error> {
61 Ok(())
62 }
63
64 fn into_body(self) -> Result<PartBody, Self::Error> {
65 Ok(self.writer)
66 }
67
68 fn clear(&self) -> Result<Self, Self::Error> {
69 let capacity = self.writer.capacity();
70 Ok(Self {
71 writer: PartBody::with_capacity(capacity),
72 header: self.header.clone(),
73 })
74 }
75}