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
|
/* 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 crate::interface::ComponentInterface;
use crate::MergeWith;
pub mod kotlin;
pub mod python;
pub mod ruby;
pub mod swift;
/// 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)]
pub enum TargetLanguage {
Kotlin,
Swift,
Python,
Ruby,
}
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,
_ => 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)]
kotlin: kotlin::Config,
#[serde(default)]
swift: swift::Config,
#[serde(default)]
python: python::Config,
#[serde(default)]
ruby: ruby::Config,
}
impl From<&ComponentInterface> for Config {
fn from(ci: &ComponentInterface) -> Self {
Config {
kotlin: ci.into(),
swift: ci.into(),
python: ci.into(),
ruby: ci.into(),
}
}
}
impl MergeWith for Config {
fn merge_with(&self, other: &Self) -> Self {
Config {
kotlin: self.kotlin.merge_with(&other.kotlin),
swift: self.swift.merge_with(&other.swift),
python: self.python.merge_with(&other.python),
ruby: self.ruby.merge_with(&other.ruby),
}
}
}
/// 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)?,
}
Ok(())
}
|