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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use crate::converter::file_type::FileType;
use crate::faker::fake_options::FakeOption;
use crate::faker::Faker;
use rand::Rng;
use std::io;

/// one record
pub fn to_record<W: io::Write, R: Rng>(
    w: &mut W,
    faker: &mut Faker<R>,
    file_type: FileType,
    header_options: &[(String, FakeOption)],
) -> io::Result<()> {
    match file_type {
        FileType::CSV => {
            let converter = CsvConverter::new(header_options);
            converter.to_record(w, &faker.gen_record(converter.options()))
        }
        FileType::TSV => {
            let converter = TsvConverter::new(header_options);
            converter.to_record(w, &faker.gen_record(converter.options()))
        }
        FileType::JSON => {
            let converter = JsonConverter::new(header_options);
            converter.to_record(w, &faker.gen_record(converter.options()))
        }
    }
}

/// one record with header
pub fn to_record_with_header<W: io::Write, R: Rng>(
    w: &mut W,
    faker: &mut Faker<R>,
    file_type: FileType,
    header_options: &[(String, FakeOption)],
) -> io::Result<()> {
    match file_type {
        FileType::CSV => {
            let converter = CsvConverter::new(header_options);
            converter.to_record_with_header(w, &faker.gen_record(converter.options()))
        }
        FileType::TSV => {
            let converter = TsvConverter::new(header_options);
            converter.to_record_with_header(w, &faker.gen_record(converter.options()))
        }
        FileType::JSON => {
            let converter = JsonConverter::new(header_options);
            converter.to_record_with_header(w, &faker.gen_record(converter.options()))
        }
    }
}

/// many record
pub fn to_data_set<W: io::Write, R: Rng>(
    w: &mut W,
    faker: &mut Faker<R>,
    file_type: FileType,
    header_options: &[(String, FakeOption)],
    count: usize,
) -> io::Result<()> {
    match file_type {
        FileType::CSV => {
            let converter = CsvConverter::new(header_options);
            converter.to_data_set(w, &faker.gen_data_set(count, converter.options()))
        }
        FileType::TSV => {
            let converter = TsvConverter::new(header_options);
            converter.to_data_set(w, &faker.gen_data_set(count, converter.options()))
        }
        FileType::JSON => {
            let converter = JsonConverter::new(header_options);
            converter.to_data_set(w, &faker.gen_data_set(count, converter.options()))
        }
    }
}

/// full formed many record
pub fn to_full_form<W: io::Write, R: Rng>(
    w: &mut W,
    faker: &mut Faker<R>,
    file_type: FileType,
    header_options: &[(String, FakeOption)],
    count: usize,
) -> io::Result<()> {
    match file_type {
        FileType::CSV => {
            let converter = CsvConverter::new(header_options);
            converter.to_full_form(w, &faker.gen_data_set(count, converter.options()))
        }
        FileType::TSV => {
            let converter = TsvConverter::new(header_options);
            converter.to_full_form(w, &faker.gen_data_set(count, converter.options()))
        }
        FileType::JSON => {
            let converter = JsonConverter::new(header_options);
            converter.to_full_form(w, &faker.gen_data_set(count, converter.options()))
        }
    }
}

fn split_options(header_options: &[(String, FakeOption)]) -> (Vec<String>, Vec<FakeOption>) {
    let mut header: Vec<String> = Vec::new();
    let mut options: Vec<FakeOption> = Vec::new();

    for (h, opt) in header_options {
        header.push(h.clone());
        options.push(opt.clone());
    }

    return (header, options);
}

/// Converter
trait Converter {
    // create
    fn new(header_options: &[(String, FakeOption)]) -> Self;

    fn header(&self) -> &Vec<String>;

    fn options(&self) -> &Vec<FakeOption>;

    // formatter
    fn formatted_header(&self) -> Vec<String> {
        self.header().iter().map(|h| format!("\"{}\"", h)).collect()
    }

    fn formatted_record(&self, record: &[String]) -> Vec<String> {
        record
            .iter()
            .zip(self.options())
            .map(|(rec, opt)| opt.with_format(rec))
            .collect()
    }

    // do not assertion
    /// write a record with flush
    fn to_header<W: io::Write>(&self, w: &mut W) -> io::Result<()>;

    /// write a record with flush
    fn to_record<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()>;

    fn to_record_with_header<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()>;

    fn to_data_set<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()>;

    fn to_full_form<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()>;
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct CsvConverter {
    header: Vec<String>,
    options: Vec<FakeOption>,
}

impl Converter for CsvConverter {
    fn new(header_options: &[(String, FakeOption)]) -> Self {
        let (header, options): (Vec<String>, Vec<FakeOption>) = split_options(header_options);
        CsvConverter { header, options }
    }

    fn header(&self) -> &Vec<String> {
        &self.header
    }

    fn options(&self) -> &Vec<FakeOption> {
        &self.options
    }

    /// write a record with flush
    fn to_header<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
        write!(w, "{}", self.formatted_header().join(","))?;
        w.flush()
    }

    /// write a record with flush
    fn to_record<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        write!(w, "{}", self.formatted_record(&record).join(","))?;
        w.flush()
    }

    fn to_record_with_header<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        self.to_header(w)?;
        write!(w, "\n")?;
        self.to_record(w, record)?;
        Ok(())
    }

    fn to_data_set<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        if data_set.is_empty() {
            return Ok(());
        }
        if let Some((fst, snd)) = data_set.split_first() {
            self.to_record(w, fst)?;
            for record in snd {
                write!(w, "\n")?;
                self.to_record(w, record)?;
            }
        }
        Ok(())
    }

    fn to_full_form<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        self.to_header(w)?;
        for record in data_set {
            write!(w, "\n")?;
            self.to_record(w, record)?;
        }
        Ok(())
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct TsvConverter {
    header: Vec<String>,
    options: Vec<FakeOption>,
}

impl Converter for TsvConverter {
    fn new(header_options: &[(String, FakeOption)]) -> Self {
        let (header, options): (Vec<String>, Vec<FakeOption>) = split_options(header_options);
        TsvConverter { header, options }
    }

    fn header(&self) -> &Vec<String> {
        &self.header
    }

    fn options(&self) -> &Vec<FakeOption> {
        &self.options
    }

    /// write a record with flush
    fn to_header<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
        write!(w, "{}", self.formatted_header().join("\t"))?;
        w.flush()
    }

    /// write a record with flush
    fn to_record<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        write!(w, "{}", self.formatted_record(&record).join("\t"))?;
        w.flush()
    }

    fn to_record_with_header<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        self.to_header(w)?;
        write!(w, "\n")?;
        self.to_record(w, record)?;
        Ok(())
    }

    fn to_data_set<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        if data_set.is_empty() {
            return Ok(());
        }
        if let Some((fst, snd)) = data_set.split_first() {
            self.to_record(w, fst)?;
            for record in snd {
                write!(w, "\n")?;
                self.to_record(w, record)?;
            }
        }
        Ok(())
    }

    fn to_full_form<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        self.to_header(w)?;
        for record in data_set {
            write!(w, "\n")?;
            self.to_record(w, record)?;
        }
        Ok(())
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct JsonConverter {
    indent_count: usize,
    one_indent: &'static str,
    header: Vec<String>,
    options: Vec<FakeOption>,
}

impl JsonConverter {
    fn add_indent(&self, num: usize) -> JsonConverter {
        JsonConverter {
            indent_count: self.indent_count + num,
            one_indent: self.one_indent,
            header: self.header().clone(),
            options: self.options().clone(),
        }
    }

    fn get_indent(&self) -> String {
        self.one_indent.repeat(self.indent_count)
    }
}

impl Converter for JsonConverter {
    fn new(header_options: &[(String, FakeOption)]) -> Self {
        let (header, options) = split_options(header_options);
        JsonConverter {
            indent_count: 0,
            one_indent: "  ",
            header,
            options,
        }
    }

    fn header(&self) -> &Vec<String> {
        &self.header
    }

    fn options(&self) -> &Vec<FakeOption> {
        &self.options
    }

    fn formatted_header(&self) -> Vec<String> {
        unreachable!()
    }

    #[allow(unused_variables)]
    fn to_header<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
        unreachable!()
    }

    /// write a record with flush
    fn to_record<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        let indent: String = self.get_indent();
        write!(w, "{}{{", indent)?;
        let record_items: Vec<String> = self
            .header()
            .iter()
            .zip(self.formatted_record(&record))
            .map(|(h, r)| format!("{}{}\"{}\": {}", self.one_indent, indent, h, r))
            .collect();
        if let Some((head, tails)) = record_items.split_first() {
            write!(w, "\n{}", head)?;
            for tail in tails {
                write!(w, ",\n{}", tail)?;
            }
        }
        write!(w, "\n{}}}", indent)?;
        w.flush()
    }

    fn to_record_with_header<W: io::Write>(&self, w: &mut W, record: &[String]) -> io::Result<()> {
        self.to_record(w, record)
    }

    /// array for json value
    fn to_data_set<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        let indent: String = self.get_indent();

        write!(w, "{}[", indent)?;
        let indented_converter: JsonConverter = self.add_indent(1);
        if let Some((head, tails)) = data_set.split_first() {
            write!(w, "\n")?;
            indented_converter.to_record(w, head)?;
            for tail in tails {
                write!(w, ",\n")?;
                indented_converter.to_record(w, tail)?;
            }
            write!(w, "\n{}", indent)?;
        }
        write!(w, "]")?;
        w.flush()
    }

    fn to_full_form<W: io::Write>(&self, w: &mut W, data_set: &[Vec<String>]) -> io::Result<()> {
        let indent: String = self.get_indent();

        write!(w, "{}{{", indent)?;
        write!(w, "\n{}{}\"dummy\": [", self.one_indent, indent,)?;
        let indented_converter: JsonConverter = self.add_indent(2);
        if let Some((head, tails)) = data_set.split_first() {
            write!(w, "\n")?;
            indented_converter.to_record(w, head)?;
            for tail in tails {
                write!(w, ",\n")?;
                indented_converter.to_record(w, tail)?;
            }
            write!(w, "\n{}{}", self.one_indent, indent)?;
        }
        write!(w, "]")?;
        write!(w, "\n{}}}", indent)?;
        w.flush()
    }
}