forked from mozilla/uniffi-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
134 lines (121 loc) · 4.06 KB
/
mod.rs
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Generate foreign language bindings for a uniffi component.
//!
//! This module contains all the code for generating foreign language bindings,
//! along with some helpers for executing foreign language scripts or tests.
use anyhow::{bail, Result};
use camino::Utf8Path;
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::interface::ComponentInterface;
pub mod kotlin;
pub mod python;
pub mod ruby;
pub mod swift;
pub mod dart;
/// Enumeration of all foreign language targets currently supported by this crate.
///
/// The functions in this module will delegate to a language-specific backend based
/// on the provided `TargetLanguage`. For convenience of calling code we also provide
/// a few `TryFrom` implementations to help guess the correct target language from
/// e.g. a file extension of command-line argument.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum TargetLanguage {
Kotlin,
Swift,
Python,
Ruby,
Dart,
}
impl fmt::Display for TargetLanguage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Kotlin => write!(f, "kotlin"),
Self::Swift => write!(f, "swift"),
Self::Python => write!(f, "python"),
Self::Ruby => write!(f, "ruby"),
Self::Dart => write!(f, "dart"),
}
}
}
/// Mode for the `run_script` function defined for each language
#[derive(Clone, Debug)]
pub struct RunScriptOptions {
pub show_compiler_messages: bool,
}
impl Default for RunScriptOptions {
fn default() -> Self {
Self {
show_compiler_messages: true,
}
}
}
impl TryFrom<&str> for TargetLanguage {
type Error = anyhow::Error;
fn try_from(value: &str) -> Result<Self> {
Ok(match value.to_ascii_lowercase().as_str() {
"kotlin" | "kt" | "kts" => TargetLanguage::Kotlin,
"swift" => TargetLanguage::Swift,
"python" | "py" => TargetLanguage::Python,
"ruby" | "rb" => TargetLanguage::Ruby,
"dart" => TargetLanguage::Dart,
_ => bail!("Unknown or unsupported target language: \"{value}\""),
})
}
}
impl TryFrom<&std::ffi::OsStr> for TargetLanguage {
type Error = anyhow::Error;
fn try_from(value: &std::ffi::OsStr) -> Result<Self> {
match value.to_str() {
None => bail!("Unreadable target language"),
Some(s) => s.try_into(),
}
}
}
impl TryFrom<String> for TargetLanguage {
type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self> {
TryFrom::try_from(value.as_str())
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub(crate) kotlin: kotlin::Config,
#[serde(default)]
pub(crate) swift: swift::Config,
#[serde(default)]
pub(crate) python: python::Config,
#[serde(default)]
pub(crate) ruby: ruby::Config,
#[serde(default)]
pub(crate) dart: dart::Config,
}
/// Generate foreign language bindings from a compiled `uniffi` library.
pub fn write_bindings(
config: &Config,
ci: &ComponentInterface,
out_dir: &Utf8Path,
language: TargetLanguage,
try_format_code: bool,
) -> Result<()> {
match language {
TargetLanguage::Kotlin => {
kotlin::write_bindings(&config.kotlin, ci, out_dir, try_format_code)?
}
TargetLanguage::Swift => {
swift::write_bindings(&config.swift, ci, out_dir, try_format_code)?
}
TargetLanguage::Python => {
python::write_bindings(&config.python, ci, out_dir, try_format_code)?
}
TargetLanguage::Ruby => ruby::write_bindings(&config.ruby, ci, out_dir, try_format_code)?,
TargetLanguage::Dart => {
dart::write_bindings(&config.dart, ci, out_dir, try_format_code)?
}
}
Ok(())
}