From 6e4170d4dab751027aad662837aa394de8ff99ba Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Fri, 2 Jun 2023 11:06:24 +0200 Subject: [PATCH] Upgrade to Clap 4 --- fixtures/benchmarks/Cargo.toml | 4 ++-- .../swift-bridging-header-compile/tests/clang.rs | 3 ++- uniffi/Cargo.toml | 4 ++-- uniffi/src/cli.rs | 7 ++++--- uniffi/src/lib.rs | 4 +++- uniffi_bindgen/Cargo.toml | 1 + uniffi_bindgen/src/bindings/kotlin/test.rs | 13 +++++++++++-- uniffi_bindgen/src/bindings/mod.rs | 1 + uniffi_bindgen/src/bindings/python/test.rs | 13 +++++++++++-- uniffi_bindgen/src/bindings/ruby/test.rs | 3 ++- uniffi_bindgen/src/bindings/swift/test.rs | 8 ++++++-- uniffi_bindgen/src/lib.rs | 5 +++-- uniffi_bindgen/src/library_mode.rs | 12 +++++++----- 13 files changed, 55 insertions(+), 23 deletions(-) diff --git a/fixtures/benchmarks/Cargo.toml b/fixtures/benchmarks/Cargo.toml index ad2329ae81..77914717b9 100644 --- a/fixtures/benchmarks/Cargo.toml +++ b/fixtures/benchmarks/Cargo.toml @@ -13,8 +13,8 @@ bench = false [dependencies] uniffi = {path = "../../uniffi"} -clap = { version = "3.1", features = ["cargo", "std", "derive"] } -criterion = "0.4.0" +clap = { version = "4", features = ["cargo", "std", "derive"] } +criterion = "0.5.1" [build-dependencies] uniffi = {path = "../../uniffi", features = ["build"] } diff --git a/fixtures/swift-bridging-header-compile/tests/clang.rs b/fixtures/swift-bridging-header-compile/tests/clang.rs index e97a390aeb..8f67a585f6 100644 --- a/fixtures/swift-bridging-header-compile/tests/clang.rs +++ b/fixtures/swift-bridging-header-compile/tests/clang.rs @@ -1,5 +1,6 @@ use camino::Utf8PathBuf; use std::process::Command; +use uniffi::TargetLanguage; use uniffi_testing::UniFFITestHelper; #[test] @@ -13,7 +14,7 @@ fn clang() -> Result<(), anyhow::Error> { uniffi::generate_bindings( &Utf8PathBuf::from("src/swift-bridging-header-compile.udl"), None, - vec!["swift"], + vec![TargetLanguage::Swift], Some(&out_dir), None, false, diff --git a/uniffi/Cargo.toml b/uniffi/Cargo.toml index 538ab12bfa..5b7209ad1f 100644 --- a/uniffi/Cargo.toml +++ b/uniffi/Cargo.toml @@ -20,7 +20,7 @@ uniffi_core = { path = "../uniffi_core", version = "=0.23.0" } uniffi_macros = { path = "../uniffi_macros", version = "=0.23.0" } anyhow = "1" camino = { version = "1.0.8", optional = true } -clap = { version = "3.1", features = ["cargo", "std", "derive"], optional = true } +clap = { version = "4", features = ["cargo", "std", "derive"], optional = true } [dev-dependencies] trybuild = "1" @@ -35,7 +35,7 @@ build = [ "dep:uniffi_build" ] bindgen = ["dep:uniffi_bindgen"] # Support for `uniffi_bindgen_main()`. Enable this feature for your # `uniffi-bindgen` binaries. -cli = [ "bindgen", "dep:clap", "dep:camino" ] +cli = [ "bindgen", "uniffi_bindgen?/clap", "dep:clap", "dep:camino" ] # Support for running example/fixture tests for `uniffi-bindgen`. You probably # don't need to enable this. bindgen-tests = [ "dep:uniffi_bindgen" ] diff --git a/uniffi/src/cli.rs b/uniffi/src/cli.rs index 0e980200f6..a20297c88e 100644 --- a/uniffi/src/cli.rs +++ b/uniffi/src/cli.rs @@ -4,6 +4,7 @@ use camino::Utf8PathBuf; use clap::{Parser, Subcommand}; +use uniffi_bindgen::bindings::TargetLanguage; // Structs to help our cmdline parsing. Note that docstrings below form part // of the "help" output. @@ -23,8 +24,8 @@ enum Commands { /// Generate foreign language bindings Generate { /// Foreign language(s) for which to build bindings. - #[clap(long, short, possible_values = &["kotlin", "python", "swift", "ruby"])] - language: Vec, + #[clap(long, short, value_enum)] + language: Vec, /// Directory in which to write generated files. Default is same folder as .udl file. #[clap(long, short)] @@ -109,7 +110,7 @@ pub fn run_main() -> anyhow::Result<()> { uniffi_bindgen::generate_bindings( &source, config.as_deref(), - language.iter().map(String::as_str).collect(), + language, out_dir.as_deref(), lib_file.as_deref(), !no_format, diff --git a/uniffi/src/lib.rs b/uniffi/src/lib.rs index 8e1bc95cea..d04e290948 100644 --- a/uniffi/src/lib.rs +++ b/uniffi/src/lib.rs @@ -16,7 +16,9 @@ pub use uniffi_bindgen::bindings::ruby::run_test as ruby_run_test; #[cfg(feature = "bindgen-tests")] pub use uniffi_bindgen::bindings::swift::run_test as swift_run_test; #[cfg(feature = "bindgen")] -pub use uniffi_bindgen::{generate_bindings, generate_component_scaffolding, print_json}; +pub use uniffi_bindgen::{ + bindings::TargetLanguage, generate_bindings, generate_component_scaffolding, print_json, +}; #[cfg(feature = "build")] pub use uniffi_build::generate_scaffolding; #[cfg(feature = "bindgen-tests")] diff --git a/uniffi_bindgen/Cargo.toml b/uniffi_bindgen/Cargo.toml index d4b45295d9..7c47bff6db 100644 --- a/uniffi_bindgen/Cargo.toml +++ b/uniffi_bindgen/Cargo.toml @@ -27,3 +27,4 @@ toml = "0.5" weedle2 = { version = "4.0.0", path = "../weedle2" } uniffi_meta = { path = "../uniffi_meta", version = "=0.23.0" } uniffi_testing = { path = "../uniffi_testing", version = "=0.23.0" } +clap = { version = "4", default-features = false, features = ["std", "derive"], optional = true } diff --git a/uniffi_bindgen/src/bindings/kotlin/test.rs b/uniffi_bindgen/src/bindings/kotlin/test.rs index 95b2e45e8a..7b78540741 100644 --- a/uniffi_bindgen/src/bindings/kotlin/test.rs +++ b/uniffi_bindgen/src/bindings/kotlin/test.rs @@ -2,7 +2,10 @@ 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/. */ -use crate::{bindings::RunScriptOptions, library_mode::generate_bindings}; +use crate::{ + bindings::{RunScriptOptions, TargetLanguage}, + library_mode::generate_bindings, +}; use anyhow::{bail, Context, Result}; use camino::{Utf8Path, Utf8PathBuf}; use std::env; @@ -34,7 +37,13 @@ pub fn run_script( let test_helper = UniFFITestHelper::new(crate_name)?; let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?; let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?; - generate_bindings(&cdylib_path, None, &["kotlin".into()], &out_dir, false)?; + generate_bindings( + &cdylib_path, + None, + &[TargetLanguage::Kotlin], + &out_dir, + false, + )?; let jar_file = build_jar(crate_name, &out_dir, options)?; let mut command = kotlinc_command(options); diff --git a/uniffi_bindgen/src/bindings/mod.rs b/uniffi_bindgen/src/bindings/mod.rs index 155a92c63c..d39202bcf2 100644 --- a/uniffi_bindgen/src/bindings/mod.rs +++ b/uniffi_bindgen/src/bindings/mod.rs @@ -26,6 +26,7 @@ pub mod swift; /// 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, diff --git a/uniffi_bindgen/src/bindings/python/test.rs b/uniffi_bindgen/src/bindings/python/test.rs index 43645ae1ca..0fcf09996f 100644 --- a/uniffi_bindgen/src/bindings/python/test.rs +++ b/uniffi_bindgen/src/bindings/python/test.rs @@ -2,7 +2,10 @@ 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/. */ -use crate::{bindings::RunScriptOptions, library_mode::generate_bindings}; +use crate::{ + bindings::{RunScriptOptions, TargetLanguage}, + library_mode::generate_bindings, +}; use anyhow::{Context, Result}; use camino::Utf8Path; use std::env; @@ -35,7 +38,13 @@ pub fn run_script( let test_helper = UniFFITestHelper::new(crate_name)?; let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?; let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?; - generate_bindings(&cdylib_path, None, &["python".into()], &out_dir, false)?; + generate_bindings( + &cdylib_path, + None, + &[TargetLanguage::Python], + &out_dir, + false, + )?; let pythonpath = env::var_os("PYTHONPATH").unwrap_or_else(|| OsString::from("")); let pythonpath = env::join_paths( diff --git a/uniffi_bindgen/src/bindings/ruby/test.rs b/uniffi_bindgen/src/bindings/ruby/test.rs index 75b070b807..03da37d567 100644 --- a/uniffi_bindgen/src/bindings/ruby/test.rs +++ b/uniffi_bindgen/src/bindings/ruby/test.rs @@ -2,6 +2,7 @@ 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/. */ +use crate::bindings::TargetLanguage; use crate::library_mode::generate_bindings; use anyhow::{bail, Context, Result}; use camino::Utf8Path; @@ -33,7 +34,7 @@ pub fn test_script_command( let test_helper = UniFFITestHelper::new(fixture_name)?; let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?; let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?; - generate_bindings(&cdylib_path, None, &["ruby".into()], &out_dir, false)?; + generate_bindings(&cdylib_path, None, &[TargetLanguage::Ruby], &out_dir, false)?; let rubypath = env::var_os("RUBYLIB").unwrap_or_else(|| OsString::from("")); let rubypath = env::join_paths( diff --git a/uniffi_bindgen/src/bindings/swift/test.rs b/uniffi_bindgen/src/bindings/swift/test.rs index a53c7390d8..c3b2f15277 100644 --- a/uniffi_bindgen/src/bindings/swift/test.rs +++ b/uniffi_bindgen/src/bindings/swift/test.rs @@ -2,7 +2,10 @@ 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/. */ -use crate::{bindings::RunScriptOptions, library_mode::generate_bindings}; +use crate::{ + bindings::{RunScriptOptions, TargetLanguage}, + library_mode::generate_bindings, +}; use anyhow::{bail, Context, Result}; use camino::{Utf8Path, Utf8PathBuf}; use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; @@ -123,7 +126,8 @@ struct GeneratedSources { impl GeneratedSources { fn new(crate_name: &str, cdylib_path: &Utf8Path, out_dir: &Utf8Path) -> Result { - let sources = generate_bindings(cdylib_path, None, &["swift".into()], out_dir, false)?; + let sources = + generate_bindings(cdylib_path, None, &[TargetLanguage::Swift], out_dir, false)?; let main_source = sources .iter() .find(|s| s.package.name == crate_name) diff --git a/uniffi_bindgen/src/lib.rs b/uniffi_bindgen/src/lib.rs index c0effbed84..033d572210 100644 --- a/uniffi_bindgen/src/lib.rs +++ b/uniffi_bindgen/src/lib.rs @@ -109,6 +109,7 @@ pub mod library_mode; pub mod macro_metadata; pub mod scaffolding; +use bindings::TargetLanguage; pub use interface::ComponentInterface; use scaffolding::RustScaffolding; @@ -268,7 +269,7 @@ pub fn generate_component_scaffolding( pub fn generate_bindings( udl_file: &Utf8Path, config_file_override: Option<&Utf8Path>, - target_languages: Vec<&str>, + target_languages: Vec, out_dir_override: Option<&Utf8Path>, library_file: Option<&Utf8Path>, try_format_code: bool, @@ -287,7 +288,7 @@ pub fn generate_bindings( &config.bindings, &component, &out_dir, - language.try_into()?, + language, try_format_code, )?; } diff --git a/uniffi_bindgen/src/library_mode.rs b/uniffi_bindgen/src/library_mode.rs index ac823b7ac0..916d334469 100644 --- a/uniffi_bindgen/src/library_mode.rs +++ b/uniffi_bindgen/src/library_mode.rs @@ -15,7 +15,10 @@ /// all of them at once. /// - UniFFI can figure out the package/module names for each crate, eliminating the external /// package maps. -use crate::{bindings, macro_metadata, parse_udl, ComponentInterface, Config, Result}; +use crate::{ + bindings::{self, TargetLanguage}, + macro_metadata, parse_udl, ComponentInterface, Config, Result, +}; use anyhow::{bail, Context}; use camino::Utf8Path; use cargo_metadata::{MetadataCommand, Package}; @@ -31,7 +34,7 @@ use uniffi_meta::group_metadata; pub fn generate_bindings( library_path: &Utf8Path, crate_name: Option, - target_languages: &[String], + target_languages: &[TargetLanguage], out_dir: &Utf8Path, try_format_code: bool, ) -> Result> { @@ -74,9 +77,8 @@ pub fn generate_bindings( } for source in sources.iter() { - for language in target_languages { - let language: bindings::TargetLanguage = language.as_str().try_into()?; - if cdylib_name.is_none() && language != bindings::TargetLanguage::Swift { + for &language in target_languages { + if cdylib_name.is_none() && language != TargetLanguage::Swift { bail!("Generate bindings for {language} requires a cdylib, but {library_path} was given"); } bindings::write_bindings(