mopro-ffi 0.3.5

Mopro is a toolkit for ZK app development on mobile. Mopro makes client-side proving on mobile simple.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use color_eyre::eyre::ContextCompat;

pub const BUILD_MODE_ENV: &str = "CONFIGURATION";
pub const IOS_ARCHS_ENV: &str = "IOS_ARCHS";
pub const ANDROID_ARCHS_ENV: &str = "ANDROID_ARCHS";
pub const FLUTTER_ARCHS_ENV: &str = "FLUTTER_ARCHS";
pub const REACT_NATIVE_ARCHS_ENV: &str = "REACT_NATIVE_ARCHS";

pub const IOS_BINDINGS_DIR: &str = "MoproiOSBindings";
pub const IOS_SWIFT_FILE: &str = "mopro.swift";
pub const IOS_XCFRAMEWORKS_DIR: &str = "MoproBindings.xcframework";

pub const ANDROID_BINDINGS_DIR: &str = "MoproAndroidBindings";
pub const ANDROID_JNILIBS_DIR: &str = "jniLibs";
pub const ANDROID_UNIFFI_DIR: &str = "uniffi";
pub const ANDROID_PACKAGE_NAME: &str = "mopro";
pub const ANDROID_KT_FILE: &str = "mopro.kt";

pub const WEB_BINDINGS_DIR: &str = "MoproWasmBindings";

pub const ARCH_X86_64: &str = "x86_64";
pub const ARCH_ARM_64: &str = "aarch64";
pub const ARCH_I686: &str = "x86";
pub const ARCH_ARM_V7_ABI: &str = "armeabi-v7a";
pub const ARCH_ARM_64_V8: &str = "arm64-v8a";

pub const FLUTTER_BINDINGS_DIR: &str = "mopro_flutter_bindings";
pub const REACT_NATIVE_BINDINGS_DIR: &str = "MoproReactNativeBindings";

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Mode {
    Debug,
    Release,
}

struct ModeInfo {
    mode: Mode,
    str: &'static str,
}

const MODES: [ModeInfo; 2] = [
    ModeInfo {
        mode: Mode::Debug,
        str: "debug",
    },
    ModeInfo {
        mode: Mode::Release,
        str: "release",
    },
];

impl Mode {
    pub fn as_str(&self) -> &'static str {
        MODES
            .iter()
            .find(|info| info.mode == *self)
            .map(|info| info.str)
            .expect("Unsupported Mode, only support 'release' and 'debug'")
    }

    pub fn parse_from_str(s: &str) -> Self {
        MODES
            .iter()
            .find(|info| info.str.to_lowercase() == s.to_lowercase())
            .map(|info| info.mode)
            .expect("Unsupported Mode String, only support 'release' and 'debug'")
    }

    pub fn from_idx(idx: usize) -> Self {
        MODES[idx].mode
    }

    pub fn idx(s: &str) -> Option<usize> {
        MODES
            .iter()
            .enumerate()
            .find(|(_, m)| m.str == s)
            .map(|(i, _)| i)
    }

    pub fn all_strings() -> Vec<&'static str> {
        MODES.iter().map(|info| info.str).collect()
    }
}

//
// Architecture Section
//

pub trait Arch {
    fn platform() -> Box<dyn Platform>;
    fn as_str(&self) -> &'static str;
    fn parse_from_str<S: AsRef<str>>(s: S) -> Self;
    fn all_strings() -> Vec<&'static str>;
    fn all_display_strings() -> Vec<(String, String)>;
    fn env_var_name() -> &'static str;
}

// https://developer.apple.com/documentation/xcode/build-settings-reference#Architectures
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum IosArch {
    Aarch64Apple,
    Aarch64AppleSim,
    X8664Apple,
}

struct IosArchInfo {
    #[allow(dead_code)] // currently not used
    arch: IosArch,
    str: &'static str,
    description: &'static str,
}

const IOS_ARCHS: [IosArchInfo; 3] = [
    IosArchInfo {
        arch: IosArch::Aarch64Apple,
        str: "aarch64-apple-ios",
        description: "64-bit iOS devices (iPhone/iPad)",
    },
    IosArchInfo {
        arch: IosArch::Aarch64AppleSim,
        str: "aarch64-apple-ios-sim",
        description: "ARM64 iOS simulator on Apple Silicon Macs",
    },
    IosArchInfo {
        arch: IosArch::X8664Apple,
        str: "x86_64-apple-ios",
        description: "x86_64 iOS simulator on Intel Macs",
    },
];

impl Arch for IosArch {
    fn platform() -> Box<dyn Platform> {
        Box::new(IosPlatform)
    }

    fn as_str(&self) -> &'static str {
        IOS_ARCHS
            .iter()
            .find(|info| info.arch == *self)
            .map(|info| info.str)
            .expect("Unsupported iOS Arch")
    }

    fn parse_from_str<S: AsRef<str>>(s: S) -> Self {
        IOS_ARCHS
            .iter()
            .find(|info| info.str.to_lowercase() == s.as_ref().to_lowercase())
            .map(|info| info.arch)
            .context(format!("Unsupported iOS Arch '{}'", s.as_ref()))
            .unwrap()
    }

    fn all_strings() -> Vec<&'static str> {
        IOS_ARCHS.iter().map(|info| info.str).collect()
    }

    fn all_display_strings() -> Vec<(String, String)> {
        IOS_ARCHS
            .iter()
            .map(|info| (info.str.to_string(), info.description.to_string()))
            .collect()
    }

    fn env_var_name() -> &'static str {
        IOS_ARCHS_ENV
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AndroidArch {
    X8664Linux,
    I686Linux,
    Armv7LinuxAbi,
    Aarch64Linux,
}

struct AndroidArchInfo {
    arch: AndroidArch,
    str: &'static str,
    description: &'static str,
}

const ANDROID_ARCHS: [AndroidArchInfo; 4] = [
    AndroidArchInfo {
        arch: AndroidArch::X8664Linux,
        str: "x86_64-linux-android",
        description: "64-bit Android emulators (x86_64 architecture)",
    },
    AndroidArchInfo {
        arch: AndroidArch::I686Linux,
        str: "i686-linux-android",
        description: "32-bit Android emulators (x86 architecture, legacy)",
    },
    AndroidArchInfo {
        arch: AndroidArch::Armv7LinuxAbi,
        str: "armv7-linux-androideabi",
        description: "32-bit ARM devices (older Android smartphones/tablets)",
    },
    AndroidArchInfo {
        arch: AndroidArch::Aarch64Linux,
        str: "aarch64-linux-android",
        description: "64-bit ARM devices (modern Android smartphones/tablets)",
    },
];

impl Arch for AndroidArch {
    fn platform() -> Box<dyn Platform> {
        Box::new(AndroidPlatform)
    }

    fn as_str(&self) -> &'static str {
        ANDROID_ARCHS
            .iter()
            .find(|info| info.arch == *self)
            .map(|info| info.str)
            .expect("Unsupported Android Arch")
    }

    fn parse_from_str<S: AsRef<str>>(s: S) -> Self {
        ANDROID_ARCHS
            .iter()
            .find(|info| info.str.to_lowercase() == s.as_ref().to_lowercase())
            .map(|info| info.arch)
            .context(format!("Unsupported Android Arch '{}'", s.as_ref()))
            .unwrap()
    }

    fn all_strings() -> Vec<&'static str> {
        ANDROID_ARCHS.iter().map(|info| info.str).collect()
    }

    fn all_display_strings() -> Vec<(String, String)> {
        ANDROID_ARCHS
            .iter()
            .map(|info| (info.str.to_string(), info.description.to_string()))
            .collect()
    }

    fn env_var_name() -> &'static str {
        ANDROID_ARCHS_ENV
    }
}

pub struct WebArch;

impl Arch for WebArch {
    fn platform() -> Box<dyn Platform> {
        Box::new(WebPlatform)
    }

    fn as_str(&self) -> &'static str {
        "wasm32-unknown-unknown"
    }

    fn parse_from_str<S: AsRef<str>>(_s: S) -> Self {
        WebArch
    }

    fn all_strings() -> Vec<&'static str> {
        vec!["wasm32-unknown-unknown"]
    }

    fn all_display_strings() -> Vec<(String, String)> {
        vec![(
            "wasm32-unknown-unknown".to_string(),
            "WebAssembly".to_string(),
        )]
    }

    fn env_var_name() -> &'static str {
        "WEB_ARCHS"
    }
}

// TODO: reuse iOS, Android constants
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

pub enum FlutterArch {
    Aarch64Apple,
    Aarch64AppleSim,
    X8664Apple,
    X8664Linux,
    I686Linux,
    Armv7LinuxAbi,
    Aarch64Linux,
}
struct FlutterArchInfo {
    arch: FlutterArch,
    str: &'static str,
    description: &'static str,
}

const FLUTTER_ARCHS: [FlutterArchInfo; 7] = [
    FlutterArchInfo {
        arch: FlutterArch::Aarch64Apple,
        str: "aarch64-apple-ios",
        description: "64-bit iOS devices (iPhone/iPad)",
    },
    FlutterArchInfo {
        arch: FlutterArch::Aarch64AppleSim,
        str: "aarch64-apple-ios-sim",
        description: "ARM64 iOS simulator on Apple Silicon Macs",
    },
    FlutterArchInfo {
        arch: FlutterArch::X8664Apple,
        str: "x86_64-apple-ios",
        description: "x86_64 iOS simulator on Intel Macs",
    },
    FlutterArchInfo {
        arch: FlutterArch::X8664Linux,
        str: "x86_64-linux-android",
        description: "64-bit Android emulators (x86_64 architecture)",
    },
    FlutterArchInfo {
        arch: FlutterArch::I686Linux,
        str: "i686-linux-android",
        description: "32-bit Android emulators (x86 architecture, legacy)",
    },
    FlutterArchInfo {
        arch: FlutterArch::Armv7LinuxAbi,
        str: "armv7-linux-androideabi",
        description: "32-bit ARM devices (older Android smartphones/tablets)",
    },
    FlutterArchInfo {
        arch: FlutterArch::Aarch64Linux,
        str: "aarch64-linux-android",
        description: "64-bit ARM devices (modern Android smartphones/tablets)",
    },
];

impl Arch for FlutterArch {
    fn platform() -> Box<dyn Platform> {
        Box::new(FlutterPlatform)
    }

    fn as_str(&self) -> &'static str {
        FLUTTER_ARCHS
            .iter()
            .find(|info| info.arch == *self)
            .map(|info| info.str)
            .expect("Unsupported iOS Arch")
    }

    fn parse_from_str<S: AsRef<str>>(s: S) -> Self {
        FLUTTER_ARCHS
            .iter()
            .find(|info| info.str.to_lowercase() == s.as_ref().to_lowercase())
            .map(|info| info.arch)
            .context(format!("Unsupported iOS Arch '{}'", s.as_ref()))
            .unwrap()
    }

    fn all_strings() -> Vec<&'static str> {
        FLUTTER_ARCHS.iter().map(|info| info.str).collect()
    }

    fn all_display_strings() -> Vec<(String, String)> {
        FLUTTER_ARCHS
            .iter()
            .map(|info| (info.str.to_string(), info.description.to_string()))
            .collect()
    }

    fn env_var_name() -> &'static str {
        FLUTTER_ARCHS_ENV
    }
}

// TODO: reuse iOS, Android constants
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

pub enum ReactNativeArch {
    Aarch64Apple,
    Aarch64AppleSim,
    X8664Apple,
    X8664Linux,
    I686Linux,
    Armv7LinuxAbi,
    Aarch64Linux,
}
struct ReactNativeArchInfo {
    arch: ReactNativeArch,
    str: &'static str,
    description: &'static str,
}

const REACT_NATIVE_ARCHS: [ReactNativeArchInfo; 7] = [
    ReactNativeArchInfo {
        arch: ReactNativeArch::Aarch64Apple,
        str: "aarch64-apple-ios",
        description: "64-bit iOS devices (iPhone/iPad)",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::Aarch64AppleSim,
        str: "aarch64-apple-ios-sim",
        description: "ARM64 iOS simulator on Apple Silicon Macs",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::X8664Apple,
        str: "x86_64-apple-ios",
        description: "x86_64 iOS simulator on Intel Macs",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::X8664Linux,
        str: "x86_64-linux-android",
        description: "64-bit Android emulators (x86_64 architecture)",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::I686Linux,
        str: "i686-linux-android",
        description: "32-bit Android emulators (x86 architecture, legacy)",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::Armv7LinuxAbi,
        str: "armv7-linux-androideabi",
        description: "32-bit ARM devices (older Android smartphones/tablets)",
    },
    ReactNativeArchInfo {
        arch: ReactNativeArch::Aarch64Linux,
        str: "aarch64-linux-android",
        description: "64-bit ARM devices (modern Android smartphones/tablets)",
    },
];

impl Arch for ReactNativeArch {
    fn platform() -> Box<dyn Platform> {
        Box::new(ReactNativePlatform)
    }

    fn as_str(&self) -> &'static str {
        REACT_NATIVE_ARCHS
            .iter()
            .find(|info| info.arch == *self)
            .map(|info| info.str)
            .expect("Unsupported React Native Arch")
    }

    fn parse_from_str<S: AsRef<str>>(s: S) -> Self {
        REACT_NATIVE_ARCHS
            .iter()
            .find(|info| info.str.to_lowercase() == s.as_ref().to_lowercase())
            .map(|info| info.arch)
            .context(format!("Unsupported React Native Arch '{}'", s.as_ref()))
            .unwrap()
    }

    fn all_strings() -> Vec<&'static str> {
        REACT_NATIVE_ARCHS.iter().map(|info| info.str).collect()
    }

    fn all_display_strings() -> Vec<(String, String)> {
        REACT_NATIVE_ARCHS
            .iter()
            .map(|info| (info.str.to_string(), info.description.to_string()))
            .collect()
    }

    fn env_var_name() -> &'static str {
        REACT_NATIVE_ARCHS_ENV
    }
}

//
// Platform Section
//

pub trait Platform {
    fn identifier() -> &'static str
    where
        Self: Sized;
}

pub trait PlatformBuilder: Platform {
    type Arch: Arch;
    type Params: Default;

    fn build(
        mode: Mode,
        project_dir: &std::path::Path,
        target_arch: Vec<Self::Arch>,
        params: Self::Params,
    ) -> anyhow::Result<std::path::PathBuf>;
}

pub struct IosPlatform;

impl Platform for IosPlatform {
    fn identifier() -> &'static str {
        "iOS Bindings Builder"
    }
}

pub struct AndroidPlatform;

impl Platform for AndroidPlatform {
    fn identifier() -> &'static str {
        "Android Bindings Builder"
    }
}

pub struct WebPlatform;

impl Platform for WebPlatform {
    fn identifier() -> &'static str {
        "Web Bindings Builder"
    }
}

pub struct FlutterPlatform;

impl Platform for FlutterPlatform {
    fn identifier() -> &'static str {
        "Flutter Bindings Builder"
    }
}

pub struct ReactNativePlatform;

impl Platform for ReactNativePlatform {
    fn identifier() -> &'static str {
        "React Native Bindings Builder"
    }
}