Skip to content

Commit a2abe2e

Browse files
sftseLegend-Master
andauthored
refactor(cli): simplify features: Option<Vec<String>> to Vec<String> (#14607)
* refactor: use empty vector for features instead of None * refactor: reorder * add change file * comment: highlight places where serialization is used * refactor: simplify serialization * Update .changes/empty-vec-instead-of-none.md * Update crates/tauri-cli/src/mobile/ios/mod.rs --------- Co-authored-by: Tony <68118705+Legend-Master@users.noreply.github.com>
1 parent 51f0fcb commit a2abe2e

File tree

19 files changed

+57
-70
lines changed

19 files changed

+57
-70
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@tauri-apps/cli": patch:enhance
3+
"tauri-cli": patch:enhance
4+
---
5+
6+
Simplified internal representation of `features: Option<Vec<String>>` with `Vec<String>`, no user facing changes

crates/tauri-cli/src/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct Options {
4040
pub target: Option<String>,
4141
/// Space or comma separated list of features to activate
4242
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
43-
pub features: Option<Vec<String>>,
43+
pub features: Vec<String>,
4444
/// Space or comma separated list of bundles to package.
4545
#[clap(short, long, action = ArgAction::Append, num_args(0..), value_delimiter = ',')]
4646
pub bundles: Option<Vec<BundleFormat>>,
@@ -252,8 +252,7 @@ pub fn setup(
252252

253253
options
254254
.features
255-
.get_or_insert(Vec::new())
256-
.extend(config.build.features.clone().unwrap_or_default());
255+
.extend_from_slice(config.build.features.as_deref().unwrap_or_default());
257256
interface.build_options(&mut options.args, &mut options.features, mobile);
258257

259258
Ok(())

crates/tauri-cli/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct Options {
7171
pub config: Vec<ConfigValue>,
7272
/// Space or comma separated list of features, should be the same features passed to `tauri build` if any.
7373
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
74-
pub features: Option<Vec<String>>,
74+
pub features: Vec<String>,
7575
/// Target triple to build against.
7676
///
7777
/// It must be one of the values outputted by `$rustc --print target-list` or `universal-apple-darwin` for an universal macOS application.

crates/tauri-cli/src/dev.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct Options {
5757
pub target: Option<String>,
5858
/// List of cargo features to activate
5959
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
60-
pub features: Option<Vec<String>>,
60+
pub features: Vec<String>,
6161
/// Exit on panic
6262
#[clap(short, long)]
6363
pub exit_on_panic: bool,
@@ -254,9 +254,7 @@ pub fn setup(interface: &AppInterface, options: &mut Options, config: ConfigHand
254254
.features
255255
.clone()
256256
.unwrap_or_default();
257-
if let Some(features) = &options.features {
258-
cargo_features.extend(features.clone());
259-
}
257+
cargo_features.extend(options.features.clone());
260258

261259
let mut dev_url = config
262260
.lock()

crates/tauri-cli/src/interface/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub trait AppSettings {
4646
package_types: Vec<PackageType>,
4747
) -> crate::Result<Settings> {
4848
let no_default_features = options.args.contains(&"--no-default-features".into());
49-
let mut enabled_features = options.features.clone().unwrap_or_default();
49+
let mut enabled_features = options.features.clone();
5050
if !no_default_features {
5151
enabled_features.push("default".into());
5252
}

crates/tauri-cli/src/interface/rust.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct Options {
5151
pub runner: Option<RunnerConfig>,
5252
pub debug: bool,
5353
pub target: Option<String>,
54-
pub features: Option<Vec<String>>,
54+
pub features: Vec<String>,
5555
pub args: Vec<String>,
5656
pub config: Vec<ConfigValue>,
5757
pub no_watch: bool,
@@ -108,7 +108,7 @@ impl From<crate::dev::Options> for Options {
108108
#[derive(Debug, Clone)]
109109
pub struct MobileOptions {
110110
pub debug: bool,
111-
pub features: Option<Vec<String>>,
111+
pub features: Vec<String>,
112112
pub args: Vec<String>,
113113
pub config: Vec<ConfigValue>,
114114
pub no_watch: bool,
@@ -393,7 +393,7 @@ fn dev_options(
393393
mobile: bool,
394394
args: &mut Vec<String>,
395395
run_args: &mut Vec<String>,
396-
features: &mut Option<Vec<String>>,
396+
features: &mut Vec<String>,
397397
app_settings: &RustAppSettings,
398398
) {
399399
let mut dev_args = Vec::new();
@@ -429,9 +429,7 @@ fn dev_options(
429429
})
430430
.collect();
431431
args.push("--no-default-features".into());
432-
if !enable_features.is_empty() {
433-
features.get_or_insert(Vec::new()).extend(enable_features);
434-
}
432+
features.extend(enable_features);
435433
}
436434
}
437435

@@ -498,15 +496,8 @@ fn get_watch_folders(additional_watch_folders: &[PathBuf]) -> crate::Result<Vec<
498496
}
499497

500498
impl Rust {
501-
pub fn build_options(
502-
&self,
503-
args: &mut Vec<String>,
504-
features: &mut Option<Vec<String>>,
505-
mobile: bool,
506-
) {
507-
features
508-
.get_or_insert(Vec::new())
509-
.push("tauri/custom-protocol".into());
499+
pub fn build_options(&self, args: &mut Vec<String>, features: &mut Vec<String>, mobile: bool) {
500+
features.push("tauri/custom-protocol".into());
510501
if mobile {
511502
args.push("--lib".into());
512503
} else {
@@ -957,11 +948,12 @@ impl AppSettings for RustAppSettings {
957948
.clone()
958949
.unwrap_or_default();
959950
for bin in bins {
960-
if let (Some(req_features), Some(opt_features)) =
961-
(&bin.required_features, &options.features)
962-
{
951+
if let Some(req_features) = &bin.required_features {
963952
// Check if all required features are enabled.
964-
if !req_features.iter().all(|feat| opt_features.contains(feat)) {
953+
if !req_features
954+
.iter()
955+
.all(|feat| options.features.contains(feat))
956+
{
965957
continue;
966958
}
967959
}

crates/tauri-cli/src/interface/rust/desktop.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ fn cargo_command(
262262
build_cmd.args(&options.args);
263263

264264
let mut features = config_features;
265-
if let Some(f) = options.features {
266-
features.extend(f);
267-
}
265+
features.extend(options.features);
268266
if !features.is_empty() {
269267
build_cmd.arg("--features");
270268
build_cmd.arg(features.join(","));

crates/tauri-cli/src/mobile/android/android_studio_script.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn command(options: Options) -> Result<()> {
8080
&AppInterface::new(tauri_config_, None)?,
8181
),
8282
tauri_config_,
83-
None,
83+
&[],
8484
&cli_options,
8585
);
8686
(config, metadata)

crates/tauri-cli/src/mobile/android/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct Options {
4848
pub targets: Option<Vec<String>>,
4949
/// List of cargo features to activate
5050
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
51-
pub features: Option<Vec<String>>,
51+
pub features: Vec<String>,
5252
/// JSON strings or paths to JSON, JSON5 or TOML files to merge with the default configuration file
5353
///
5454
/// Configurations are merged in the order they are provided, which means a particular value overwrites previous values when a config key-value pair conflicts.
@@ -152,7 +152,7 @@ pub fn command(options: Options, noise_level: NoiseLevel) -> Result<BuiltApplica
152152
let (config, metadata) = get_config(
153153
&app,
154154
tauri_config_,
155-
build_options.features.as_ref(),
155+
&build_options.features,
156156
&Default::default(),
157157
);
158158
(interface, config, metadata)

crates/tauri-cli/src/mobile/android/dev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use std::{env::set_current_dir, net::Ipv4Addr, path::PathBuf};
4545
pub struct Options {
4646
/// List of cargo features to activate
4747
#[clap(short, long, action = ArgAction::Append, num_args(0..))]
48-
pub features: Option<Vec<String>>,
48+
pub features: Vec<String>,
4949
/// Exit on panic
5050
#[clap(short, long)]
5151
exit_on_panic: bool,

0 commit comments

Comments
 (0)