aboutsummaryrefslogtreecommitdiff
path: root/src/export.rs
blob: 6d95b65e4a28c9927740d6f01cdd2f28b19de88f (plain) (blame)
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
// This file is part of senpy-cli <https://github.com/senpy-club/cli>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use std::{
  fs::File,
  io::{BufWriter, Write},
};

use senpy::Random;

pub trait Exporter {
  fn random_to_file(_: &str, _: Random);
  fn vec_to_file(_: &str, _: Vec<String>);
}

pub struct JsonExporter;
pub struct YamlExporter;
pub struct DhallExporter;

impl Exporter for JsonExporter {
  fn random_to_file(filename: &str, random: Random) {
    let writer = BufWriter::new(File::create(filename).unwrap());

    serde_json::to_writer_pretty(
      writer,
      &serde_json::json!({
        "language": random.language,
        "image": random.image,
      }),
    )
    .unwrap();
  }

  fn vec_to_file(filename: &str, languages: Vec<String>) {
    let writer = BufWriter::new(File::create(filename).unwrap());

    serde_json::to_writer_pretty(writer, &languages).unwrap();
  }
}

impl Exporter for YamlExporter {
  fn random_to_file(filename: &str, random: Random) {
    let writer = BufWriter::new(File::create(filename).unwrap());

    serde_yaml::to_writer(writer, &random).unwrap()
  }

  fn vec_to_file(filename: &str, languages: Vec<String>) {
    let writer = BufWriter::new(File::create(filename).unwrap());

    serde_yaml::to_writer(writer, &languages).unwrap();
  }
}

impl Exporter for DhallExporter {
  fn random_to_file(filename: &str, random: Random) {
    let mut writer = BufWriter::new(File::create(filename).unwrap());

    writer
      .write_all(
        serde_dhall::serialize(&random)
          .to_string()
          .unwrap()
          .as_bytes(),
      )
      .unwrap();
  }

  fn vec_to_file(filename: &str, languages: Vec<String>) {
    let mut writer = BufWriter::new(File::create(filename).unwrap());

    writer
      .write_all(
        serde_dhall::serialize(&languages)
          .to_string()
          .unwrap()
          .as_bytes(),
      )
      .unwrap();
  }
}