From a47ecb963f0ecd78105dcd12ce1ae3e59dfb1943 Mon Sep 17 00:00:00 2001 From: sftse Date: Thu, 15 Jan 2026 11:57:09 +0100 Subject: [PATCH] refactor: remove once-cell-regex from dependencies (#500) Co-authored-by: Fabian-Lars --- .changes/rm-oncecell-regex.md | 5 +++++ Cargo.lock | 13 ++----------- Cargo.toml | 3 ++- src/android/adb/device_list.rs | 2 +- src/android/adb/device_name.rs | 2 +- src/android/env.rs | 14 ++++++++------ src/android/ndk.rs | 2 +- src/android/source_props.rs | 2 +- src/android/target.rs | 2 +- src/apple/deps/mod.rs | 2 +- src/apple/deps/update.rs | 2 +- src/apple/project.rs | 3 +-- src/apple/system_profile.rs | 2 +- src/apple/target.rs | 2 +- src/apple/teams.rs | 2 +- src/config/app/mod.rs | 5 ++--- src/lib.rs | 1 + src/once_cell_regex.rs | 34 ++++++++++++++++++++++++++++++++++ src/os/linux/info.rs | 2 +- src/os/linux/xdg.rs | 6 +++--- src/os/macos/info.rs | 2 +- src/util/cli.rs | 2 +- src/util/git/mod.rs | 11 ++++------- src/util/git/submodule.rs | 14 ++++---------- src/util/mod.rs | 11 ++++++----- src/util/path.rs | 4 +--- 26 files changed, 86 insertions(+), 64 deletions(-) create mode 100644 .changes/rm-oncecell-regex.md create mode 100644 src/once_cell_regex.rs diff --git a/.changes/rm-oncecell-regex.md b/.changes/rm-oncecell-regex.md new file mode 100644 index 0000000..0e22975 --- /dev/null +++ b/.changes/rm-oncecell-regex.md @@ -0,0 +1,5 @@ +--- +"cargo-mobile2": patch +--- + +Remove unnecessary once-cell-regex dependency. diff --git a/Cargo.lock b/Cargo.lock index 5c0a8e3..da36025 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,11 +191,12 @@ dependencies = [ "java-properties", "libc", "log", - "once-cell-regex", + "once_cell", "os_info", "os_pipe", "path_abs", "plist", + "regex", "rstest", "serde", "serde_json", @@ -978,16 +979,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "once-cell-regex" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3de7e389a5043420c8f2b95ed03f3f104ad6f4c41f7d7e27298f033abc253e8" -dependencies = [ - "once_cell", - "regex", -] - [[package]] name = "once_cell" version = "1.21.3" diff --git a/Cargo.toml b/Cargo.toml index 4296f96..3da4b06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,8 @@ home = "0.5" ignore = "0.4" java-properties = "2" log = "0.4" -once-cell-regex = "0.2" +once_cell = "1.21" +regex = "1.11" path_abs = "0.5" serde = { version = "1.0", features = ["derive"] } structopt = { version = "0.3", optional = true } diff --git a/src/android/adb/device_list.rs b/src/android/adb/device_list.rs index f380115..91a5068 100644 --- a/src/android/adb/device_list.rs +++ b/src/android/adb/device_list.rs @@ -1,4 +1,5 @@ use super::{device_name, get_prop}; +use crate::regex_multi_line; use crate::{ android::{ device::{ConnectionStatus, Device}, @@ -9,7 +10,6 @@ use crate::{ target::TargetTrait, util::cli::{Report, Reportable}, }; -use once_cell_regex::regex_multi_line; use std::{collections::BTreeSet, process::Command}; use thiserror::Error; diff --git a/src/android/adb/device_name.rs b/src/android/adb/device_name.rs index a97795b..c43a505 100644 --- a/src/android/adb/device_name.rs +++ b/src/android/adb/device_name.rs @@ -5,11 +5,11 @@ use std::{ }; use super::adb; +use crate::regex; use crate::{ android::env::Env, util::cli::{Report, Reportable}, }; -use once_cell_regex::regex; use thiserror::Error; #[derive(Debug, Error)] diff --git a/src/android/env.rs b/src/android/env.rs index db594c4..c7a423e 100644 --- a/src/android/env.rs +++ b/src/android/env.rs @@ -7,7 +7,11 @@ use crate::{ os::Env as CoreEnv, util::cli::{Report, Reportable}, }; -use std::{collections::HashMap, ffi::OsString, path::PathBuf}; +use std::{ + collections::HashMap, + ffi::OsString, + path::{Path, PathBuf}, +}; use thiserror::Error; #[derive(Debug, Error)] @@ -63,8 +67,7 @@ impl Env { } }) .or_else(|err| { - if let Some(sdk_root) = std::env::var("ANDROID_SDK_ROOT") - .ok() + if let Some(sdk_root) = std::env::var_os("ANDROID_SDK_ROOT") .map(PathBuf::from) .filter(|sdk_root| sdk_root.is_dir()) { @@ -75,8 +78,7 @@ impl Env { } }) .or_else(|err| { - if let Some(sdk_root) = std::env::var("ANDROID_SDK_ROOT") - .ok() + if let Some(sdk_root) = std::env::var_os("ANDROID_SDK_ROOT") .map(PathBuf::from) .filter(|sdk_root| sdk_root.is_dir()) { @@ -102,7 +104,7 @@ impl Env { } pub fn platform_tools_path(&self) -> PathBuf { - PathBuf::from(&self.android_home).join("platform-tools") + Path::new(&self.android_home).join("platform-tools") } pub fn sdk_version(&self) -> Result { diff --git a/src/android/ndk.rs b/src/android/ndk.rs index a81974f..cfd2706 100644 --- a/src/android/ndk.rs +++ b/src/android/ndk.rs @@ -2,6 +2,7 @@ use super::{ source_props::{self, SourceProps}, target::Target, }; +use crate::regex_multi_line; use crate::{ os::consts, util::{ @@ -9,7 +10,6 @@ use crate::{ VersionDouble, }, }; -use once_cell_regex::regex_multi_line; use std::{ collections::HashSet, fmt::{self, Display}, diff --git a/src/android/source_props.rs b/src/android/source_props.rs index 805f1cd..0a4cbe7 100644 --- a/src/android/source_props.rs +++ b/src/android/source_props.rs @@ -1,5 +1,5 @@ +use crate::regex; use crate::util; -use once_cell_regex::regex; use std::{ collections::HashMap, fmt::Display, diff --git a/src/android/target.rs b/src/android/target.rs index 1e81031..9cb7a9e 100644 --- a/src/android/target.rs +++ b/src/android/target.rs @@ -13,7 +13,7 @@ use crate::{ CargoCommand, }, }; -use once_cell_regex::exports::once_cell::sync::OnceCell; +use once_cell::sync::OnceCell; use serde::Serialize; use std::{collections::BTreeMap, fmt, io, path::PathBuf, str}; use thiserror::Error; diff --git a/src/apple/deps/mod.rs b/src/apple/deps/mod.rs index 0d98630..95a1cea 100644 --- a/src/apple/deps/mod.rs +++ b/src/apple/deps/mod.rs @@ -6,6 +6,7 @@ use super::{ device_ctl_available, system_profile::{self, DeveloperTools}, }; +use crate::regex; use crate::{ util::{ self, @@ -14,7 +15,6 @@ use crate::{ }, DuctExpressionExt, }; -use once_cell_regex::regex; use std::collections::hash_set::HashSet; use thiserror::Error; diff --git a/src/apple/deps/update.rs b/src/apple/deps/update.rs index e966e8c..6b56507 100644 --- a/src/apple/deps/update.rs +++ b/src/apple/deps/update.rs @@ -2,7 +2,7 @@ use super::{ util::{self, CaptureGroupError}, GemCache, PACKAGES, }; -use once_cell_regex::regex; +use crate::regex; use serde::Deserialize; use thiserror::Error; diff --git a/src/apple/project.rs b/src/apple/project.rs index 4d9c844..0e121d1 100644 --- a/src/apple/project.rs +++ b/src/apple/project.rs @@ -1,10 +1,9 @@ -use once_cell_regex::regex; - use super::{ config::{Config, Metadata}, deps, rust_version_check, target::Target, }; +use crate::regex; use crate::{ bicycle, target::TargetTrait as _, diff --git a/src/apple/system_profile.rs b/src/apple/system_profile.rs index c174582..d5b6281 100644 --- a/src/apple/system_profile.rs +++ b/src/apple/system_profile.rs @@ -1,5 +1,5 @@ +use crate::regex; use crate::util; -use once_cell_regex::regex; use thiserror::Error; #[derive(Debug, Error)] diff --git a/src/apple/target.rs b/src/apple/target.rs index 9b1e3b2..ff6dda7 100644 --- a/src/apple/target.rs +++ b/src/apple/target.rs @@ -17,7 +17,7 @@ use crate::{ }, DuctExpressionExt, }; -use once_cell_regex::exports::once_cell::sync::OnceCell; +use once_cell::sync::OnceCell; use serde::Deserialize; use std::{ collections::{BTreeMap, HashMap}, diff --git a/src/apple/teams.rs b/src/apple/teams.rs index af7d6e0..0df232f 100644 --- a/src/apple/teams.rs +++ b/src/apple/teams.rs @@ -1,4 +1,4 @@ -use once_cell_regex::regex; +use crate::regex; use std::collections::BTreeSet; use thiserror::Error; diff --git a/src/config/app/mod.rs b/src/config/app/mod.rs index c5e37c9..82ad810 100644 --- a/src/config/app/mod.rs +++ b/src/config/app/mod.rs @@ -73,7 +73,7 @@ pub struct App { template_pack: Pack, #[serde(skip)] #[allow(clippy::type_complexity)] - target_dir_resolver: Option PathBuf>>>, + target_dir_resolver: Option PathBuf>>, } impl Debug for App { @@ -164,8 +164,7 @@ impl App { mut self, resolver: F, ) -> Self { - self.target_dir_resolver - .replace(Arc::new(Box::new(resolver))); + self.target_dir_resolver.replace(Arc::new(resolver)); self } diff --git a/src/lib.rs b/src/lib.rs index ef98e53..e6d1be4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub mod doctor; pub mod dot_cargo; pub mod env; pub mod init; +mod once_cell_regex; pub mod opts; pub mod os; mod project; diff --git a/src/once_cell_regex.rs b/src/once_cell_regex.rs new file mode 100644 index 0000000..2e6c2fa --- /dev/null +++ b/src/once_cell_regex.rs @@ -0,0 +1,34 @@ +#[macro_export] +macro_rules! regex { + ($re:expr) => {{ + use ::once_cell::sync::OnceCell; + use ::regex::Regex; + static RE: OnceCell = OnceCell::new(); + RE.get_or_init(|| Regex::new($re).unwrap()) + }}; +} + +#[macro_export] +macro_rules! regex_multi_line { + ($re:expr) => {{ + use ::once_cell::sync::OnceCell; + use ::regex::Regex; + static RE: OnceCell = OnceCell::new(); + RE.get_or_init(|| { + ::regex::RegexBuilder::new($re) + .multi_line(true) + .build() + .unwrap() + }) + }}; +} + +#[macro_export] +macro_rules! byte_regex { + ($re:expr) => {{ + use ::once_cell::sync::OnceCell; + use ::regex::bytes::Regex; + static RE: OnceCell = OnceCell::new(); + RE.get_or_init(|| Regex::new($re).unwrap()) + }}; +} diff --git a/src/os/linux/info.rs b/src/os/linux/info.rs index 41f84b6..474f120 100644 --- a/src/os/linux/info.rs +++ b/src/os/linux/info.rs @@ -1,5 +1,5 @@ use crate::os::Info; -use once_cell_regex::regex; +use crate::regex; use std::path::PathBuf; use thiserror::Error; diff --git a/src/os/linux/xdg.rs b/src/os/linux/xdg.rs index ce70a90..2ae3ec5 100644 --- a/src/os/linux/xdg.rs +++ b/src/os/linux/xdg.rs @@ -1,5 +1,6 @@ +use crate::byte_regex; use freedesktop_entry_parser::{parse_entry, Entry as FreeDesktopEntry}; -use once_cell_regex::{byte_regex, exports::regex::bytes::Regex}; +use regex::bytes::Regex; use std::{ env, ffi::{OsStr, OsString}, @@ -154,8 +155,7 @@ fn parse_unquoted_text( // We parse the arguments // We only have one file path (not an URL). Any instance of these ones // needs to be replaced by the file path in this particular case. - let arg_re = byte_regex!(r"%u|%U|%f|%F"); - let result = replace_on_pattern(text, argument, arg_re); + let result = replace_on_pattern(text, argument, byte_regex!(r"%u|%U|%f|%F")); // Then the other flags let icon_replace = icon.unwrap_or_else(|| "".as_ref()); diff --git a/src/os/macos/info.rs b/src/os/macos/info.rs index 50de7ec..98f6722 100644 --- a/src/os/macos/info.rs +++ b/src/os/macos/info.rs @@ -1,5 +1,5 @@ +use crate::regex; use crate::{os::Info, util}; -use once_cell_regex::regex; pub fn check() -> Result { util::run_and_search( diff --git a/src/util/cli.rs b/src/util/cli.rs index 22bd3cd..1ffaebc 100644 --- a/src/util/cli.rs +++ b/src/util/cli.rs @@ -141,7 +141,7 @@ mod interface { use std::fmt::Debug; use crate::{opts, util}; - use once_cell_regex::exports::once_cell::sync::Lazy; + use once_cell::sync::Lazy; use structopt::{ clap::{self, AppSettings}, StructOpt, diff --git a/src/util/git/mod.rs b/src/util/git/mod.rs index 654a6d0..28bcc41 100644 --- a/src/util/git/mod.rs +++ b/src/util/git/mod.rs @@ -14,21 +14,18 @@ impl<'a> Git<'a> { Self { root } } - pub fn root(&'a self) -> &'a Path { + pub fn root(&self) -> &Path { self.root } pub fn command(&self) -> duct::Expression { - duct::cmd( - "git", - ["-C", self.root.as_os_str().to_str().unwrap_or_default()], - ) + duct::cmd("git", ["-C".as_ref(), self.root.as_os_str()]) } pub fn command_parse(&self, arg_str: impl AsRef) -> duct::Expression { - let mut args = vec!["-C", self.root.as_os_str().to_str().unwrap_or_default()]; + let mut args = vec!["-C".as_ref(), self.root.as_os_str()]; for arg in arg_str.as_ref().split(' ') { - args.push(arg) + args.push(arg.as_ref()) } duct::cmd("git", args) } diff --git a/src/util/git/submodule.rs b/src/util/git/submodule.rs index dfe716f..3c3fb71 100644 --- a/src/util/git/submodule.rs +++ b/src/util/git/submodule.rs @@ -1,5 +1,5 @@ use super::{lfs, Git}; -use once_cell_regex::regex; +use crate::regex; use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Display}, @@ -142,20 +142,14 @@ impl Submodule { cause: Box::new(Cause::IndexCheckFailed(cause)), })?; let initialized = if !in_index { - let path_str = self - .path - .to_str() - .ok_or_else(|| Error { - submodule: self.clone(), - cause: Box::new(Cause::PathInvalidUtf8), - })? - .to_owned(); + let path = self.path.to_owned(); log::info!("adding submodule: {:#?}", self); let remote = self.remote.clone(); let name = name.to_owned(); git.command() .before_spawn(move |cmd| { - cmd.args(["submodule", "add", "--name", &name, &remote, &path_str]); + cmd.args(["submodule", "add", "--name", &name, &remote]) + .arg(&path); Ok(()) }) .run() diff --git a/src/util/mod.rs b/src/util/mod.rs index 15a8f9f..caa655c 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -8,13 +8,14 @@ pub mod prompt; pub use self::{cargo::*, git::*, path::*}; use self::cli::{Report, Reportable}; +use crate::regex; use crate::{ env::ExplicitEnv, os::{self, command_path}, DuctExpressionExt, }; -use once_cell_regex::{exports::regex::Captures, exports::regex::Regex, regex}; use path_abs::PathOps; +use regex::{Captures, Regex}; use serde::{ser::Serializer, Deserialize, Serialize}; use std::{ error::Error as StdError, @@ -28,10 +29,10 @@ use std::{ use thiserror::Error; pub fn list_display(list: &[impl Display]) -> String { - if list.len() == 1 { - list[0].to_string() - } else if list.len() == 2 { - format!("{} and {}", list[0], list[1]) + if let [x0] = list { + x0.to_string() + } else if let [x0, x1] = list { + format!("{x0} and {x1}") } else { let mut display = String::new(); for (idx, item) in list.iter().enumerate() { diff --git a/src/util/path.rs b/src/util/path.rs index bc219b6..72bdc21 100644 --- a/src/util/path.rs +++ b/src/util/path.rs @@ -103,9 +103,7 @@ pub fn prefix_path(root: impl AsRef, path: impl AsRef) -> PathBuf { } Component::CurDir => {} Component::ParentDir => { - if buf.last().is_some() { - buf.pop(); - } + buf.pop(); } _ => buf.push(component), };