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
use crate::{
  core::suiter::Suite,
  exports::json::JSON,
};


use std::{
  fs,
  io::Write,
  path::Path,
};


pub trait Exporter {
  fn new (data: Suite) -> Self;
  fn parse (self) -> Self;
  fn path (self, pathname: String) -> Self;
}


#[derive(Clone, Copy, Debug)]
pub enum ExportMode {
  JSON,
}


#[derive(Debug)]
pub struct ExportConfig {
  pub data: Suite,
  pub filename: &'static str,
  pub pathname: &'static str,
}


impl Clone for ExportConfig {
  fn clone (&self) -> Self {
    ExportConfig {
      data: self.data.clone(),
      filename: self.filename.clone(),
      pathname: self.pathname.clone(),
    }
  }
}


#[derive(Debug)]
pub struct Export {
  pub config: ExportConfig,
  pub exports: Vec<String>,
  pub mode: ExportMode,
}


impl Export {
  pub fn new (config: ExportConfig) -> Self {
    Export {
      config,
      exports: vec![],
      mode: ExportMode::JSON,
    }
  }

  pub fn mode (mut self, mode: ExportMode) -> Self {
    self.mode = mode;
    self
  }

  // TODO: refacto!
  pub fn to_json (mut self) {
    println!("\ncreating json...");

    let config = self.config.clone();
    let path = format!("{}/{}", config.pathname, config.filename);
    let json = JSON::new(config.data).path(path).parse();

    self.exports.push(json.output.clone());

    if let Some(parent) = Path::new(&json.pathname).parent() {
      if !parent.exists() {
        fs::create_dir_all(parent)
        .unwrap_or_else(|_| panic!("Could not create {:?}", json.pathname));
      }
    }

    let mut file = fs::File::create(&json.pathname)
    .expect("Could not create output file");

    file.write_all(self.exports[ExportMode::JSON as usize].as_bytes())
    .expect("Could not output to file");

    println!("created json in {}", &json.pathname);
  }
}