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
#![allow(unused_doc_comments)]

mod to_typescript;
mod typescript;
pub mod utils;
pub(crate) mod casing;
pub(crate) mod holochain_imports;
mod parser;


use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use crate::parser::*;


const MAGIC_FIRST_LINE: &str = "/* This file is generated by zits. Do not edit manually */";


pub struct GenConfig {
    can_debug: bool,
    //can_proxy: bool,
    can_hc_imports: bool,
    uses_typeinterface: bool,
}


///
pub fn generate_typescript_bindings(
    input: Vec<PathBuf>,
    external_imports: Vec<String>,
    output: PathBuf,
    can_debug: bool,
    can_hc_imports: bool,
    can_proxy: bool,
    maybe_default_zome_name: Option<String>,
) {
    let uses_typeinterface = output
       .as_os_str()
       .to_str()
       .map(|x| x.ends_with(".d.ts"))
       .unwrap_or(true);


    let mut state: ParseState = ParseState::new(
        GenConfig {
            can_debug,
            //can_proxy,
            can_hc_imports,
            uses_typeinterface,
        });

    state.set_external_import_header(external_imports);

    let file_name = output.file_stem().unwrap().to_str().unwrap();
    let zome_name: &str = file_name.split(".").collect::<Vec<&str>>()[0];
    let default_zome_name: String = if let Some(dzn) = maybe_default_zome_name {
        dzn
    } else {
        zome_name.to_string()
    };

    if !can_debug {
        state.write_type_defs_header();
        state.write_zome_fn_names_header(&zome_name);
        if can_proxy { state.write_zome_proxy_header(&zome_name, &default_zome_name); }
    }


    /// Parse input files
    for input_path in input {
        if !input_path.exists() {
            if can_debug {
                println!("Path `{:#?}` does not exist", input_path);
            }

            state.unprocessed_files.push(input_path);
            continue;
        }

        if input_path.is_dir() {
            for entry in WalkDir::new(input_path.clone()).sort_by_file_name() {
                match entry {
                    Ok(dir_entry) => {
                        let path = dir_entry.into_path();

                        // skip dir files because they're going to be recursively crawled by WalkDir
                        if !path.is_dir() {
                            // make sure it is a rust file
                            let extension = path.extension();
                            if extension.is_some() && extension.unwrap().eq_ignore_ascii_case("rs")
                            {
                                state.parse_rust_file(path);
                            } else if can_debug {
                                println!("Encountered non-rust file `{:#?}`", path);
                            }
                        } else if can_debug {
                            println!("Encountered directory `{:#?}`", path);
                        }
                    }
                    Err(_) => {
                        println!(
                            "An error occurred whilst walking directory `{:#?}`...",
                            input_path.clone()
                        );
                        continue;
                    }
                }
            }
        } else {
            state.parse_rust_file(input_path);
        }
    }



    if can_proxy {
        /// ZomeProxy file footer
        state.zome_proxy_output.push_str(&format!("}}\n"));
        /// Append type imports to ZomeProxy
        state.write_type_defs_import(&zome_name);
        /// ZomeFnNames file footer
        state.write_zome_fn_names_footer(&zome_name, &default_zome_name);
        //state.zome_fn_names_output.push_str(&format!("]\n"));
    }

    /** */
    if can_debug {
        println!("\n");
        println!("======================================");
        println!("Debug mode try run output");
        println!("======================================");
        //println!("======================================");
        println!("TYPE DEFS FILE for \"{}\"", zome_name);
        println!("======================================");
        println!("{}", state.type_defs_output);
        println!("======================================");
        if can_proxy {
            println!("ZomeProxy FILE for \"{}\"", zome_name);
            println!("======================================");
            println!("{}", state.zome_proxy_output);
            println!("======================================");
        }
        println!("Function Names for \"{}\"", zome_name);
        println!("======================================");
        println!("{}", state.zome_fn_names_output);
        println!("======================================");
    } else {
        println!("======================================");
        println!("Bindings generated for \"{}\"", zome_name);
        println!("======================================");

        let count_const = state.converted_items["const"].len();
        let count_type = state.converted_items["type"].len();
        let count_struct = state.converted_items["struct"].len();
        let count_enum = state.converted_items["enum"].len();
        let count_fn = state.converted_items["fn"].len();

        println!("Total Items found: {}", count_const + count_type + count_struct + count_enum + count_fn);
        if count_const > 0 {println!("  -  const: {}", count_const)}
        if count_type > 0 {println!("  -   type: {}", count_type)}
        if count_struct > 0 {println!("  - struct: {}", count_struct)}
        if count_enum > 0 {println!("  -   enum: {}", count_enum)}
        if count_fn > 0 {println!("  -     fn: {}", count_fn)}

        // Verify that the output file either doesn't exists or has been generated by zits.
        let original_file_path = Path::new(&output);
        if original_file_path.exists() {
            if !original_file_path.is_file() {
                panic!("Specified output path is a directory but must be a file.")
            }
            let original_file = File::open(original_file_path).expect("Couldn't open output file");
            let mut buffer = BufReader::new(original_file);

            let mut first_line = String::new();

            buffer
                .read_line(&mut first_line)
                .expect("Unable to read line");

            if first_line.trim() != MAGIC_FIRST_LINE {
                panic!("Aborting: specified output file exists but doesn't seem to be a zits output file: {}", first_line)
            }
        }

        if count_const + count_type + count_struct + count_enum > 0 {
            let mut types_output: PathBuf = output.clone();
            types_output.set_file_name(format!("{}.types.ts", zome_name));
            let mut types_file: File = File::create(&types_output).expect("Unable to write to file");
            match types_file.write_all(state.type_defs_output.as_bytes()) {
                Ok(_) => println!("Successfully generated types: {:#?}", types_output),
                Err(_) => println!("Failed to generate types, an error occurred."),
            }
        } else {
            println!("Types file not generated as no types have been found.");
        }

        if can_proxy {
            /// Proxy file
            let mut proxy_output: PathBuf = output.clone();
            proxy_output.set_file_name(format!("{}.proxy.ts", zome_name));
            //println!("ProxyFile: {:?}", proxy_output);
            let mut proxy_file: File = File::create(&proxy_output).expect("Unable to write to file");
            match proxy_file.write_all(state.zome_proxy_output.as_bytes()) {
                Ok(_) => println!("Successfully generated ZomeProxy: {:#?}", proxy_output),
                Err(_) => println!("Failed to generate ZomeProxy, an error occurred."),
            }
        }

        /// FnNames file
        if count_fn > 0 {
            let mut fn_output: PathBuf = output.clone();
            fn_output.set_file_name(format!("{}.fn.ts", zome_name));
            //println!("ProxyFile: {:?}", proxy_output);
            let mut fn_file: File = File::create(&fn_output).expect("Unable to write to file");
            match fn_file.write_all(state.zome_fn_names_output.as_bytes()) {
                Ok(_) => println!("Successfully generated FnNames: {:#?}", fn_output),
                Err(_) => println!("Failed to generate FnNames, an error occurred."),
            }
        } else {
            println!("FnNames file not generated as no functions have been found");
        }
    }

    if state.unprocessed_files.len() > 0 {
        println!("[zits][info] Could not parse the following files:");
        for unprocessed_file in state.unprocessed_files {
            println!("• {:#?}", unprocessed_file);
        }
    }
    println!("======================================");
}