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
use io_trait::Io;
use std::io::{self, Error};

use crate::{
    common::default::default,
    mem::global::GLOBAL,
    parser::{parse, Context, DataType},
    serializer::{to_djs::to_djs, to_json::to_json},
};

pub fn run(io: &impl Io) -> io::Result<()> {
    let mut a = io.args();
    a.next().unwrap();
    let input = a.next().unwrap();
    let output = a.next().unwrap();

    let mc = &mut default();
    let mut context = Context::new(GLOBAL, io, input, mc);
    let output_data_type = file_to_data_type(&output);
    match output_data_type {
        Ok(data_type) => match parse(&mut context) {
            Ok(parse_result) => match data_type {
                DataType::Json => {
                    let to_json_result = to_json(parse_result.any);
                    match to_json_result {
                        Ok(s) => io.write(&output, s.as_bytes()),
                        Err(e) => Err(Error::other(e)),
                    }
                }
                DataType::Cjs => {
                    let to_json_result = to_djs(parse_result.any, true);
                    match to_json_result {
                        Ok(s) => io.write(&output, s.as_bytes()),
                        Err(e) => Err(Error::other(e)),
                    }
                }
                DataType::Mjs => {
                    let to_json_result = to_djs(parse_result.any, false);
                    match to_json_result {
                        Ok(s) => io.write(&output, s.as_bytes()),
                        Err(e) => Err(Error::other(e)),
                    }
                }
                _ => unreachable!(),
            },
            Err(parse_error) => Err(Error::other(parse_error.to_string())),
        },
        Err(e) => Err(e),
    }
}

fn file_to_data_type(s: &str) -> Result<DataType, Error> {
    if s.ends_with(".json") {
        return Ok(DataType::Json);
    }
    if s.ends_with(".d.cjs") {
        return Ok(DataType::Cjs);
    }
    if s.ends_with(".d.mjs") {
        return Ok(DataType::Mjs);
    }
    Err(Error::other("invalid output extension"))
}

#[cfg(test)]
mod test {
    use io_test::VirtualIo;
    use io_trait::Io;
    use wasm_bindgen_test::wasm_bindgen_test;

    use super::run;

    #[test]
    #[wasm_bindgen_test]
    fn test_json() {
        let io: VirtualIo = VirtualIo::new(&["test_json.json", "output.json"]);

        let main = include_str!("../../test/test-json.json");
        let main_path = "test_json.json";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.json").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"{"key":[true,false,null]}"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_output_data_type() {
        let io: VirtualIo = VirtualIo::new(&["test_json.json", "output.d.cjs"]);

        let main = include_str!("../../test/test-json.json");
        let main_path = "test_json.json";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.cjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"module.exports={"key":[true,false,null]}"#);

        let io: VirtualIo = VirtualIo::new(&["test_json.json", "output.d.mjs"]);

        let main = include_str!("../../test/test-json.json");
        let main_path = "test_json.json";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.mjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"export default {"key":[true,false,null]}"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_cjs() {
        let io: VirtualIo = VirtualIo::new(&["test_djs.d.cjs", "output.d.cjs"]);

        let main = include_str!("../../test/test-djs.d.cjs");
        let main_path = "test_djs.d.cjs";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.cjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"module.exports={"id":null}"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_mjs() {
        let io: VirtualIo = VirtualIo::new(&["test_djs.d.mjs", "output.d.mjs"]);

        let main = include_str!("../../test/test-djs.d.mjs");
        let main_path = "test_djs.d.mjs";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.mjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"export default {"id":null}"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_cjs_import() {
        let io: VirtualIo = VirtualIo::new(&["test_import_main.d.cjs", "output.d.cjs"]);

        let main = include_str!("../../test/test_import_main.d.cjs");
        let main_path = "test_import_main.d.cjs";
        io.write(main_path, main.as_bytes()).unwrap();

        let module = include_str!("../../test/test_import_module.d.cjs");
        let module_path = "test_import_main.d.cjs";
        io.write(module_path, module.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.cjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"module.exports=3"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_mjs_import() {
        let io: VirtualIo = VirtualIo::new(&["test_import_main.d.mjs", "output.d.mjs"]);

        let main = include_str!("../../test/test_import_main.d.mjs");
        let main_path = "test_import_main.d.mjs";
        io.write(main_path, main.as_bytes()).unwrap();

        let module = include_str!("../../test/test_import_module.d.mjs");
        let module_path = "test_import_main.d.mjs";
        io.write(module_path, module.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_ok());
        let ouput_vec = io.read("output.d.mjs").unwrap();
        let vec = String::from_utf8(ouput_vec).unwrap();
        assert_eq!(vec, r#"export default 4"#);
    }

    #[test]
    #[wasm_bindgen_test]
    fn test_invalid_ouput_extension() {
        let io: VirtualIo = VirtualIo::new(&["test_json.json", "outputd.cjs"]);

        let main = include_str!("../../test/test-json.json");
        let main_path = "test_json.json";
        io.write(main_path, main.as_bytes()).unwrap();

        let result = run(&io);
        assert!(result.is_err());
    }
}