1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use serde::Serialize;
use serde_json::{ser::CompactFormatter, Serializer};

use crate::{format_name::FormatName, input::Input};

pub struct JsonCompactEachRowInput<T> {
    rows: Vec<Vec<T>>,
}
impl<T> JsonCompactEachRowInput<T> {
    pub fn new(rows: Vec<Vec<T>>) -> Self {
        Self { rows }
    }
}

impl<T> Input for JsonCompactEachRowInput<T>
where
    T: Serialize,
{
    type Error = serde_json::Error;

    fn format_name() -> FormatName {
        FormatName::JsonCompactEachRow
    }

    fn serialize(&self) -> Result<Vec<u8>, Self::Error> {
        let mut buf = vec![];
        let mut ser_buf = Vec::with_capacity(128);

        for row in &self.rows {
            buf.push(b'[');
            for (i, item) in row.iter().enumerate() {
                ser_buf.clear();
                let mut ser = Serializer::with_formatter(&mut ser_buf, CompactFormatter);
                item.serialize(&mut ser)?;

                buf.extend_from_slice(&ser.into_inner());

                if i < (row.len() - 1) {
                    buf.extend_from_slice(b", ");
                }
            }
            buf.push(b']');

            buf.push(b'\n');
        }

        Ok(buf)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::{error, fs, path::PathBuf};

    use crate::test_helpers::{TEST_ROW_1, TEST_ROW_2};

    use serde_json::{Map, Value};

    #[test]
    fn simple() -> Result<(), Box<dyn error::Error>> {
        let file_path = PathBuf::new().join("tests/files/JSONCompactEachRow.txt");
        let content = fs::read_to_string(&file_path)?;

        assert_eq!(
            JsonCompactEachRowInput::<()>::format_name(),
            file_path
                .file_stem()
                .unwrap()
                .to_string_lossy()
                .parse()
                .unwrap()
        );

        let mut rows: Vec<Vec<Value>> = vec![];
        rows.push(vec![
            TEST_ROW_1.array1.to_owned().into(),
            TEST_ROW_1.array2.to_owned().into(),
            vec![
                Value::Number(TEST_ROW_1.tuple1.to_owned().0.into()),
                Value::String(TEST_ROW_1.tuple1.to_owned().1),
            ]
            .into(),
            vec![
                Value::Number(TEST_ROW_1.tuple2.to_owned().0.into()),
                Value::Null,
            ]
            .into(),
            Value::Object(TEST_ROW_1.map1.to_owned().into_iter().fold(
                Map::new(),
                |mut m, (k, v)| {
                    m.insert(k, Value::String(v));
                    m
                },
            )),
        ]);
        rows.push(vec![
            TEST_ROW_2.array1.to_owned().into(),
            TEST_ROW_2.array2.to_owned().into(),
            vec![
                Value::Number(TEST_ROW_2.tuple1.to_owned().0.into()),
                Value::String(TEST_ROW_2.tuple1.to_owned().1),
            ]
            .into(),
            vec![
                Value::Number(TEST_ROW_2.tuple2.to_owned().0.into()),
                Value::String(TEST_ROW_2.tuple2.to_owned().1.unwrap()),
            ]
            .into(),
            Value::Object(TEST_ROW_2.map1.to_owned().into_iter().fold(
                Map::new(),
                |mut m, (k, v)| {
                    m.insert(k, Value::String(v));
                    m
                },
            )),
        ]);

        let bytes = JsonCompactEachRowInput::new(rows).serialize()?;
        assert_eq!(bytes, content.as_bytes());

        Ok(())
    }
}