rsbind 0.6.0

Provide tools to bind rust trait with other language and export library artifact directly. Invoke rust functions just like you write it in native language.
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#![recursion_limit = "128"]
extern crate cbindgen;
extern crate core;
#[macro_use]
extern crate error_chain;
extern crate fs_extra;
extern crate heck;
extern crate ndk_build;
extern crate ndk_tool;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate rstgen;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate syn;
extern crate toml;
extern crate zip;

use std::fs;
use std::path::PathBuf;

use crate::android::config::Android;
use crate::android::process::AndroidProcess;
use crate::ast::AstResult;
use crate::base::process::*;
use crate::errors::*;
use crate::ios::config::Ios;
use crate::ios::process::IosProcess;
use crate::jar::config::Jar;
use crate::jar::process::JarProcess;
use crate::mac::config::Mac;
use crate::mac::process::MacProcess;

mod android;
mod ast;
mod base;
mod bridge;
mod cargo;
mod config;
mod errors;
mod ios;
mod jar;
mod java;
mod mac;
mod swift;
mod test;
mod unzip;
#[macro_use]
mod common;

const GEN_DIR_NAME: &str = "_gen";
const HEADER_NAME: &str = "header";
const AST_DIR: &str = "ast";
const IOS_PROJ: &str = "ios_artifact";
const IOS_BRIDGE_PROJ: &str = "ios_bridge";
const MAC_PROJ: &str = "mac_artifact";
const MAC_BRIDGE_PROJ: &str = "mac_bridge";
const ANDROID_BRIDGE_PROJ: &str = "android_bridge";
const ANDROID_PROJ: &str = "android_artifact";
const JAR_BRIDGE_PROJ: &str = "jar_bridge";
const JAR_PROJ: &str = "jar_artifact";

pub struct Bind {
    prj_path: PathBuf,
    ios_artifact_path: PathBuf,
    ios_bridge_path: PathBuf,
    mac_artifact_path: PathBuf,
    mac_bridge_path: PathBuf,
    android_bridge_path: PathBuf,
    android_artifact_path: PathBuf,
    jar_bridge_path: PathBuf,
    jar_artifact_path: PathBuf,
    header_path: PathBuf,
    ast_path: PathBuf,
    target: Target,
    action: Action,
}

pub enum Target {
    Android,
    Ios,
    Mac,
    Jar,
    All,
}

pub enum Action {
    /// Parse src/contract and src/imp, generate simplified ast json file to _gen/ast.
    GenAst,
    /// Generate rust bridge code to _gen/ios_bridge or _gen/android_bridge, for iOS it's c ffi functions, for android it's jni ffi functions.
    GenBridge,
    /// Generate code in artifact(iOS framework or android aar)
    GenArtifactCode,
    /// Generate c header file
    GenCHeader,
    /// Build and generate artifact.
    BuildArtifact,
    /// Do all the process and generate artifacts.
    All,
}

impl Bind {
    ///
    /// crate the object for binding.
    /// * prject_path: the rust project we need to bind
    /// * target: which target we want to generate, android or iOS
    ///
    pub fn from(prj_path: String, target: Target, action: Action) -> Bind {
        let root = PathBuf::from(&prj_path);

        // ./_gen/ast
        let ast_path = root.join(GEN_DIR_NAME).join(AST_DIR);

        // ./_gen/header/
        let header_path = root.join(GEN_DIR_NAME).join(HEADER_NAME);

        // ./_gen/ios_artifact/
        let ios_artifact_path = root.join(GEN_DIR_NAME).join(IOS_PROJ);

        // ./_gen/ios_bridge
        let ios_bridge_path = root.join(GEN_DIR_NAME).join(IOS_BRIDGE_PROJ);

        // ./_gen/mac_artifact/
        let mac_artifact_path = root.join(GEN_DIR_NAME).join(MAC_PROJ);

        // ./_gen/mac_bridge
        let mac_bridge_path = root.join(GEN_DIR_NAME).join(MAC_BRIDGE_PROJ);

        // ./_gen/android_bridge
        let android_bridge_path = root.join(GEN_DIR_NAME).join(ANDROID_BRIDGE_PROJ);

        let android_artifact_path = root.join(GEN_DIR_NAME).join(ANDROID_PROJ);

        // ./_gen/jar_bridge
        let jar_bridge_path = root.join(GEN_DIR_NAME).join(JAR_BRIDGE_PROJ);

        let jar_artifact_path = root.join(GEN_DIR_NAME).join(JAR_PROJ);

        Bind {
            prj_path: root,
            ios_artifact_path,
            ios_bridge_path,
            mac_artifact_path,
            mac_bridge_path,
            android_bridge_path,
            android_artifact_path,
            jar_bridge_path,
            jar_artifact_path,
            header_path,
            ast_path,
            target,
            action,
        }
    }

    ///
    /// generate the ios framework and android aar as per the target config
    ///
    pub fn gen_all(&self) -> Result<()> {
        let config = config::parse(&self.prj_path);
        println!("rsbind config in {:?} is {:?}", &self.prj_path, config);

        let crate_name = self.parse_crate_name()?;

        if let Action::GenAst = self.action {
            self.parse_ast(crate_name)?;
            return Ok(());
        }

        let ast = &self.get_ast_if_need(crate_name.clone())?;
        match self.target {
            Target::Ios => {
                self.gen_for_ios(&crate_name, ast, config)?;
            }
            Target::Android => {
                self.gen_for_android(&crate_name, ast, config)?;
            }
            Target::Mac => {
                self.gen_for_mac(&crate_name, ast, config)?;
            }
            Target::Jar => {
                self.gen_for_jar(&crate_name, ast, config)?;
            }
            Target::All => {
                self.gen_for_ios(&crate_name, ast, config.clone())?;
                self.gen_for_android(&crate_name, ast, config.clone())?;
                self.gen_for_mac(&crate_name, ast, config.clone())?;
                self.gen_for_jar(&crate_name, ast, config)?;
            }
        };
        Ok(())
    }

    fn get_ast_if_need(&self, crate_name: String) -> Result<AstResult> {
        match self.action {
            Action::GenBridge | Action::GenArtifactCode | Action::All => self.parse_ast(crate_name),
            _ => {
                use std::collections::HashMap;
                let ast_result = AstResult {
                    traits: HashMap::new(),
                    structs: HashMap::new(),
                    imps: vec![],
                };
                Ok(ast_result)
            }
        }
    }

    fn parse_ast(&self, crate_name: String) -> Result<AstResult> {
        let prj_path = PathBuf::from(&self.prj_path);
        if self.ast_path.exists() {
            fs::remove_dir_all(&self.ast_path)?;
        }
        fs::create_dir_all(&self.ast_path)?;
        ast::AstHandler::new(crate_name)
            .parse(&prj_path)?
            .flush(&self.ast_path)
    }

    ///
    /// generate the jar framework
    fn gen_for_jar(
        &self,
        crate_name: &str,
        ast_result: &AstResult,
        config: Option<config::Config>,
    ) -> Result<()> {
        let jar = match config {
            Some(ref config) => config.jar.clone(),
            None => Some(Jar::default()),
        };

        let jar_process = JarProcess::new(
            &self.prj_path,
            &self.jar_artifact_path,
            &self.jar_bridge_path,
            crate_name,
            ast_result,
            jar,
        );

        match self.action {
            Action::GenAst => (),
            Action::GenBridge => jar_process.gen_bridge_src()?,
            Action::GenArtifactCode => jar_process.gen_artifact_code()?,
            Action::GenCHeader => {}
            Action::BuildArtifact => {
                jar_process.build_bridge_prj()?;
                jar_process.copy_bridge_outputs()?;
                jar_process.build_artifact_prj()?;
            }
            Action::All => {
                jar_process.gen_bridge_src()?;
                jar_process.gen_artifact_code()?;
                jar_process.build_bridge_prj()?;
                jar_process.copy_bridge_outputs()?;
                jar_process.build_artifact_prj()?;
            }
        }

        Ok(())
    }

    ///
    /// generate the mac framework
    fn gen_for_mac(
        &self,
        crate_name: &str,
        ast_result: &AstResult,
        config: Option<config::Config>,
    ) -> Result<()> {
        let mac = match config {
            Some(ref config) => config.mac.clone(),
            None => Some(Mac::default()),
        };

        let mac_process = MacProcess::new(
            &self.prj_path,
            &self.mac_artifact_path,
            &self.mac_bridge_path,
            &self.header_path,
            crate_name,
            ast_result,
            mac,
        );

        match self.action {
            Action::GenAst => (),
            Action::GenBridge => mac_process.gen_bridge_src()?,
            Action::GenArtifactCode => mac_process.gen_artifact_code()?,
            Action::GenCHeader => mac_process.gen_c_header()?,
            Action::BuildArtifact => {
                mac_process.build_bridge_prj()?;
                mac_process.copy_bridge_outputs()?;
                // we don't generate artifact now. TODO generate cocoapods lib
                // ios_process.build_artifact_prj()?;
            }
            Action::All => {
                mac_process.gen_bridge_src()?;
                mac_process.gen_artifact_code()?;
                mac_process.build_bridge_prj()?;
                mac_process.copy_bridge_outputs()?;
                // we don't generate artifact now. TODO generate cocoapods lib
                // ios_process.build_artifact_prj()?;
            }
        }

        Ok(())
    }

    ///
    /// generate the ios framework
    fn gen_for_ios(
        &self,
        crate_name: &str,
        ast_result: &AstResult,
        config: Option<config::Config>,
    ) -> Result<()> {
        let ios = match config {
            Some(ref config) => config.ios.clone(),
            None => Some(Ios::default()),
        };

        let ios_process = IosProcess::new(
            &self.prj_path,
            &self.ios_artifact_path,
            &self.ios_bridge_path,
            &self.header_path,
            crate_name,
            ast_result,
            ios,
        );

        match self.action {
            Action::GenAst => (),
            Action::GenBridge => ios_process.gen_bridge_src()?,
            Action::GenArtifactCode => ios_process.gen_artifact_code()?,
            Action::GenCHeader => ios_process.gen_c_header()?,
            Action::BuildArtifact => {
                ios_process.build_bridge_prj()?;
                ios_process.copy_bridge_outputs()?;
                // we don't generate artifact now. TODO generate cocoapods lib
                // ios_process.build_artifact_prj()?;
            }
            Action::All => {
                ios_process.gen_bridge_src()?;
                ios_process.gen_artifact_code()?;
                ios_process.build_bridge_prj()?;
                ios_process.copy_bridge_outputs()?;
                // we don't generate artifact now. TODO generate cocoapods lib
                // ios_process.build_artifact_prj()?;
            }
        }

        Ok(())
    }

    ///
    /// generate the android aar
    ///
    fn gen_for_android(
        &self,
        crate_name: &str,
        ast_result: &AstResult,
        config: Option<config::Config>,
    ) -> Result<()> {
        let android = match config {
            Some(ref config) => config.android.clone(),
            None => Some(Android::default()),
        };

        let android_process = AndroidProcess::new(
            &self.prj_path,
            &self.android_artifact_path,
            &self.android_bridge_path,
            crate_name,
            ast_result,
            android,
        );

        match self.action {
            Action::GenAst => (),
            Action::GenBridge => android_process.gen_bridge_src()?,
            Action::GenArtifactCode => android_process.gen_artifact_code()?,
            Action::GenCHeader => (),
            Action::BuildArtifact => {
                android_process.build_bridge_prj()?;
                android_process.copy_bridge_outputs()?;
                android_process.build_artifact_prj()?;
            }
            Action::All => {
                android_process.gen_bridge_src()?;
                android_process.gen_artifact_code()?;
                android_process.build_bridge_prj()?;
                android_process.copy_bridge_outputs()?;
                android_process.build_artifact_prj()?;
            }
        };

        Ok(())
    }

    ///
    /// parse the crate name of origin project from Cargo.toml
    ///
    fn parse_crate_name(&self) -> Result<String> {
        let toml_path = PathBuf::from(&self.prj_path).join("Cargo.toml");
        let manifest = cargo::manifest(toml_path.as_path())?;
        println!("parse project name = {}", manifest.package.name);
        Ok(manifest.package.name)
    }
}