Add binary to remove the need for uniffi-bindgen cli tool

This commit is contained in:
thunderbiscuit 2022-03-24 12:54:26 -04:00 committed by Steve Myers
parent 5512b31969
commit ce848725b4
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
2 changed files with 70 additions and 2 deletions

View File

@ -19,14 +19,15 @@ uniffi_macros = { version = "0.16.0", features = ["builtin-bindgen"] }
uniffi = { version = "0.16.0", features = ["builtin-bindgen"] }
thiserror = "1.0"
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
uniffi_bindgen = "0.16.0"
uniffi_bindgen = { version = "0.16.0", optional = true }
#uniffi_bindgen = { version = "0.16.0", optional = true }
[build-dependencies]
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
[features]
generate-python = ["uniffi_bindgen"]
#generate-python = ["uniffi_bindgen"]
[[bin]]
name = "generate"

View File

@ -0,0 +1,67 @@
use uniffi_bindgen;
pub const BDK_UDL: &str = "src/bdk.udl";
#[derive(Debug)]
#[derive(PartialEq)]
pub enum Language {
KOTLIN,
// PYTHON,
// SWIFT,
UNSUPPORTED,
}
impl Language {
fn to_string(self) -> &'static str {
if self == Language::KOTLIN { "kotlin" }
// else if self == Language::PYTHON { "python" }
// else if self == Language::SWIFT { "swift" }
else { panic!("Not a supported language") }
}
}
fn parse_language(language_argument: &str) -> Language {
match language_argument {
"kotlin" => Language::KOTLIN,
// "python" => Language::PYTHON,
// "swift" => Language::SWIFT,
_ => panic!("Unsupported language")
}
}
fn generate_bindings() -> Result<(), Box<dyn std::error::Error>> {
use std::env;
let args: Vec<String> = env::args().collect();
let language: Language;
let output_directory: &String;
if &args[1] != "--language" {
panic!("Please provide the --language option");
} else {
language = parse_language(&args[2]);
}
if &args[3] != "--out-dir" {
panic!("Please provide the --out-dir option");
} else {
output_directory = &args[4]
}
println!("Chosen language is {:?}", language);
println!("Output directory is {:?}", output_directory);
uniffi_bindgen::generate_bindings(
&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL),
None,
vec![language.to_string()],
Some(&output_directory),
false,
)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
generate_bindings()?;
Ok(())
}