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
use std::io;

use csv::{Reader, Writer};
use serde::{Deserialize, Serialize};

use crate::porter::ExporterError;

#[derive(Deserialize, Serialize)]
struct Translation {
    pub local: String,
    pub foreign: String,
    pub guesses_local_total: i32,
    pub guesses_local_correct: i32,
    pub guesses_foreign_total: i32,
    pub guesses_foreign_correct: i32,
}

impl From<crate::Translation> for Translation {
    fn from(t: crate::Translation) -> Self {
        Translation {
            local: t.local,
            foreign: t.foreign,
            guesses_local_total: t.guesses_local_total,
            guesses_local_correct: t.guesses_local_correct,
            guesses_foreign_total: t.guesses_foreign_total,
            guesses_foreign_correct: t.guesses_foreign_correct,
        }
    }
}

impl From<Translation> for crate::Translation {
    fn from(t: Translation) -> Self {
        crate::Translation {
            local: t.local,
            foreign: t.foreign,
            guesses_local_total: t.guesses_local_total,
            guesses_local_correct: t.guesses_local_correct,
            guesses_foreign_total: t.guesses_foreign_total,
            guesses_foreign_correct: t.guesses_foreign_correct,
        }
    }
}

pub struct CsvReader<R: io::Read> {
    reader: Reader<R>,
}

impl<R: io::Read> CsvReader<R> {
    pub fn new(source: R) -> CsvReader<R> {
        CsvReader {
            reader: Reader::from_reader(source),
        }
    }
}

impl<R: io::Read> Iterator for CsvReader<R> {
    type Item = Result<crate::Translation, ExporterError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.reader
            .deserialize::<Translation>()
            .next()
            .map(|res| res.map(|rec| rec.into()).map_err(|e| e.into()))
    }
}

pub struct CsvWriter<W: io::Write> {
    writer: Writer<W>,
}

impl<W: io::Write> CsvWriter<W> {
    pub fn new(destination: W) -> CsvWriter<W> {
        CsvWriter {
            writer: Writer::from_writer(destination),
        }
    }

    pub fn write(&mut self, translation: crate::Translation) -> Result<(), ExporterError> {
        let csv_translation: Translation = translation.into();
        self.writer.serialize(csv_translation)?;
        self.writer.flush()?;
        Ok(())
    }
}