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
mod alias_map;
mod builder;
mod emitter;
mod field_map;
mod parser;
mod type_meta;
mod util;

use alias_map::AliasMap;
pub use builder::Builder;

use builder::BindgenOptions;
use emitter::*;
use field_map::FieldMap;
use parser::*;
use std::{collections::HashSet, error::Error};
use type_meta::{ExternMethod, RustEnum, RustStruct, RustType};

enum GenerateKind {
    InputBindgen,
    InputExtern,
}

pub(crate) fn generate(
    generate_kind: GenerateKind,
    options: &BindgenOptions,
) -> Result<(Option<String>, String), Box<dyn Error>> {
    let (paths, generate_rust) = match generate_kind {
        GenerateKind::InputBindgen => (options.input_bindgen_files.as_slice(), true),
        GenerateKind::InputExtern => (options.input_extern_files.as_slice(), false),
    };

    let mut methods: Vec<ExternMethod> = vec![];
    let mut aliases = AliasMap::new();
    let mut structs: Vec<RustStruct> = vec![];
    let mut enums: Vec<RustEnum> = vec![];

    for path in paths {
        let file_content = std::fs::read_to_string(path)
            .expect(&format!("input file not found, path: {}", path.display()));
        let file_ast = syn::parse_file(file_content.as_str())?;

        match generate_kind {
            GenerateKind::InputBindgen => collect_foreign_method(&file_ast, options, &mut methods),
            GenerateKind::InputExtern => collect_extern_method(&file_ast, options, &mut methods),
        };
        collect_type_alias(&file_ast, &mut aliases);
        collect_struct(&file_ast, &mut structs);
        collect_enum(&file_ast, &mut enums);
    }

    // collect using_types
    let mut using_types = HashSet::new();
    for method in &methods {
        // add to using_types with normalize
        if let Some(x) = &method.return_type {
            collect_using_types(&mut using_types, &aliases, x);
        }
        for p in &method.parameters {
            collect_using_types(&mut using_types, &aliases, &p.rust_type);
        }
    }

    let mut field_map = FieldMap::new();
    for struct_type in &structs {
        for field in &struct_type.fields {
            let (struct_type_normalized, _) = aliases.normalize(&struct_type.struct_name);
            collect_field_types(
                &mut field_map,
                &aliases,
                &struct_type_normalized,
                &field.rust_type,
            );
        }
    }

    let structs = reduce_struct(&structs, &field_map, &using_types);
    let enums = reduce_enum(&enums, &field_map, &using_types);

    let rust = if generate_rust {
        Some(emit_rust_method(&methods, options))
    } else {
        None
    };
    let csharp = emit_csharp(&methods, &aliases, &structs, &enums, options);

    Ok((rust, csharp))
}

fn collect_using_types(
    using_types: &mut HashSet<String>,
    aliases: &AliasMap,
    rust_type: &RustType,
) {
    if let type_meta::TypeKind::Option(o) = &rust_type.type_kind {
        collect_using_types(using_types, aliases, o);
    } else if let type_meta::TypeKind::Function(parameters, return_type) = &rust_type.type_kind {
        if let Some(x) = &return_type {
            collect_using_types(using_types, aliases, x);
        }
        for p in parameters {
            collect_using_types(using_types, aliases, &p.rust_type);
        }
    } else {
        let (normalized, normalized_rust_type) = aliases.normalize(&rust_type.type_name);
        if let Some(x) = normalized_rust_type {
            collect_using_types(using_types, aliases, &x);
        } else {
            using_types.insert(normalized.clone());
        }
    }
}

fn collect_field_types(
    field_map: &mut FieldMap,
    aliases: &AliasMap,
    struct_type_normalized: &String,
    rust_type: &RustType,
) {
    if let type_meta::TypeKind::Option(o) = &rust_type.type_kind {
        collect_field_types(field_map, aliases, struct_type_normalized, o);
    } else if let type_meta::TypeKind::Function(parameters, return_type) = &rust_type.type_kind {
        if let Some(x) = &return_type {
            collect_field_types(field_map, aliases, struct_type_normalized, x);
        }
        for p in parameters {
            collect_field_types(field_map, aliases, struct_type_normalized, &p.rust_type);
        }
    } else {
        let (normalized, normalized_rust_type) = aliases.normalize(&rust_type.type_name);
        if let Some(x) = normalized_rust_type {
            collect_field_types(field_map, aliases, struct_type_normalized, &x);
        } else {
            field_map.insert(struct_type_normalized, &normalized);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        env,
        fs::{self},
        io::Write,
    };

    use regex::Regex;

    use super::*;

    #[test]
    fn test() {
        let path = std::env::current_dir().unwrap();
        println!("starting dir: {}", path.display()); // csbindgen/csbindgen

        Builder::new()
            .input_bindgen_file("csbindgen-tests/src/liblz4.rs")
            .csharp_class_name("LibLz4")
            .csharp_dll_name("csbindgen_tests")
            .generate_to_file(
                "csbindgen-tests/src/lz4_ffi.rs",
                "dotnet-sandbox/lz4_bindgen.cs",
            )
            .unwrap();
    }

    // cargo test update_package_version -- 1.0.0 --nocapture
    #[test]
    fn update_package_version() {
        let args: Vec<String> = env::args().collect();
        // 0: exe path
        // 1: update_package_version
        // 2: 1.0.0
        // 3: --nocapture

        if args[1] != "update_package_version" {
            return;
        }

        println!("version: {}", args[2]);
        let mut path = std::env::current_dir().unwrap();
        println!("current_dir: {}", path.display());

        path.push("Cargo.toml");
        let toml = fs::read_to_string(path.clone()).unwrap();

        // replace only first-match
        let regex = Regex::new("version = \".+\"").unwrap();

        let new_toml = regex.replace(toml.as_str(), format!("version = \"{}\"", args[2]));

        let mut file = fs::File::create(path.clone()).unwrap();
        file.write_all(new_toml.as_bytes()).unwrap();
        file.flush().unwrap();
    }
}