aws_multipart_upload/codec/
jsonlines.rs1use bytes::{BufMut, BytesMut};
2use serde::Serialize;
3use tokio_util::codec::Encoder;
4
5use crate::AwsError;
6
7#[derive(Debug, thiserror::Error)]
8pub enum JsonlinesCodecError {
9 #[error("serde_json encoding error {0}")]
10 Serde(#[from] serde_json::Error),
11 #[error("io error {0}")]
12 Io(#[from] std::io::Error),
13}
14
15impl From<JsonlinesCodecError> for AwsError {
16 fn from(value: JsonlinesCodecError) -> Self {
17 Self::Codec(value.to_string())
18 }
19}
20
21#[derive(Debug, Clone, Default)]
23pub struct JsonlinesCodec {
24 _private: (),
25}
26
27impl JsonlinesCodec {
28 pub fn new() -> Self {
29 Self::default()
30 }
31}
32
33impl<Item> Encoder<Item> for JsonlinesCodec
34where
35 Item: Serialize,
36{
37 type Error = JsonlinesCodecError;
38
39 fn encode(&mut self, item: Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
40 let bytevec = serde_json::to_vec(&item)?;
41 dst.reserve(&bytevec.len() + 1);
42 dst.put(bytevec.as_slice());
43 dst.put_u8(b'\n');
44 Ok(())
45 }
46}