feat: fix LazyLock issue and add something in cdylib
This commit is contained in:
1 parent
f0e610f8c8
commit
76ddddb6cd
4 files changed
+134
-69
No files matched your search
+16
-16
@@ -6,7 +6,7 @@ use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use std::sync::LazyLock;
|
||||
use thiserror::Error as TeError;
|
||||
|
||||
@@ -234,16 +234,16 @@ pub struct Program {
|
||||
applications_key: lowlevel::ApplicationsKey,
|
||||
app_path: String,
|
||||
app_dir_path: String,
|
||||
name: Option<Rc<ProgramStr>>,
|
||||
icon: Option<Rc<ProgramIcon>>,
|
||||
behavior: Option<Rc<ProgramBehavior>>,
|
||||
name: Option<Arc<ProgramStr>>,
|
||||
icon: Option<Arc<ProgramIcon>>,
|
||||
behavior: Option<Arc<ProgramBehavior>>,
|
||||
|
||||
#[allow(dead_code)]
|
||||
strs: Vec<Rc<ProgramStr>>,
|
||||
strs: Vec<Arc<ProgramStr>>,
|
||||
#[allow(dead_code)]
|
||||
icons: Vec<Rc<ProgramIcon>>,
|
||||
icons: Vec<Arc<ProgramIcon>>,
|
||||
#[allow(dead_code)]
|
||||
behaviors: Vec<Rc<ProgramBehavior>>,
|
||||
behaviors: Vec<Arc<ProgramBehavior>>,
|
||||
|
||||
ext_keys: Vec<ProgramProgIdExtKey>,
|
||||
ext_keys_map: HashMap<String, usize>,
|
||||
@@ -293,9 +293,9 @@ impl Program {
|
||||
|
||||
fn resolve_index<T>(
|
||||
key: &String,
|
||||
vector: &Vec<Rc<T>>,
|
||||
vector: &Vec<Arc<T>>,
|
||||
index_map: &HashMap<String, usize>,
|
||||
) -> Result<Rc<T>, ParseProgramError> {
|
||||
) -> Result<Arc<T>, ParseProgramError> {
|
||||
match index_map.get(key) {
|
||||
Some(index) => Ok(vector
|
||||
.get(*index)
|
||||
@@ -355,22 +355,22 @@ impl Program {
|
||||
let program_str = ProgramStr {
|
||||
inner: str_res_variant,
|
||||
};
|
||||
Ok(Rc::new(program_str))
|
||||
Ok(Arc::new(program_str))
|
||||
})?;
|
||||
let (icons, icons_index_map) = Self::flat_hashmap(&schema.icons, |entry| {
|
||||
let icon_res_variant: lowlevel::IconResVariant = entry.as_str().into();
|
||||
let program_icon = ProgramIcon {
|
||||
inner: icon_res_variant,
|
||||
};
|
||||
Ok(Rc::new(program_icon))
|
||||
Ok(Arc::new(program_icon))
|
||||
})?;
|
||||
let (behaviors, behaviors_index_map) = Self::flat_hashmap(&schema.behaviors, |entry| {
|
||||
// We simply always use "Open" verb.
|
||||
let cmdline: concept::CmdLine = entry.as_str().parse()?;
|
||||
let verb = concept::Verb::OPEN.clone();
|
||||
let verb = concept::Verb::OPEN();
|
||||
let shell_verb = lowlevel::ShellVerb::new(verb, cmdline);
|
||||
let program_behavior = ProgramBehavior { inner: shell_verb };
|
||||
Ok(Rc::new(program_behavior))
|
||||
Ok(Arc::new(program_behavior))
|
||||
})?;
|
||||
|
||||
// Setup default name, icon and behavior
|
||||
@@ -764,9 +764,9 @@ struct ProgramProgIdExtKey {
|
||||
ext_key: lowlevel::ExtKey,
|
||||
|
||||
progid_key: lowlevel::ProgIdKey,
|
||||
name: Rc<ProgramStr>,
|
||||
icon: Rc<ProgramIcon>,
|
||||
behavior: Rc<ProgramBehavior>,
|
||||
name: Arc<ProgramStr>,
|
||||
icon: Arc<ProgramIcon>,
|
||||
behavior: Arc<ProgramBehavior>,
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
@@ -699,13 +699,10 @@ pub struct ExpandString {
|
||||
inner: String,
|
||||
}
|
||||
|
||||
impl ExpandString {
|
||||
/// Internal shared compiled regex pattern matching Variable,
|
||||
/// the `%` braced string like `%SystemRoot%`.
|
||||
const VAR_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"%[a-zA-Z0-9_]+%").expect("unexpected bad regex pattern string")
|
||||
});
|
||||
}
|
||||
/// Internal shared compiled regex pattern matching Variable,
|
||||
/// the `%` braced string like `%SystemRoot%`.
|
||||
static WINVAR_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"%[a-zA-Z0-9_]+%").expect("unexpected bad regex pattern string"));
|
||||
|
||||
impl ExpandString {
|
||||
/// Create a new expand string
|
||||
@@ -743,7 +740,7 @@ impl ExpandString {
|
||||
|
||||
// If the final string still has environment variable,
|
||||
// we think we fail to expand it.
|
||||
if Self::VAR_RE.is_match(rv.as_str()) {
|
||||
if WINVAR_RE.is_match(rv.as_str()) {
|
||||
Err(ExpandEnvVarError::NoEnvVar)
|
||||
} else {
|
||||
Ok(rv)
|
||||
@@ -761,7 +758,7 @@ impl FromStr for ExpandString {
|
||||
type Err = ParseExpandStrError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if Self::VAR_RE.is_match(s) {
|
||||
if WINVAR_RE.is_match(s) {
|
||||
Ok(Self {
|
||||
inner: s.to_string(),
|
||||
})
|
||||
@@ -858,9 +855,20 @@ pub struct Verb {
|
||||
}
|
||||
|
||||
impl Verb {
|
||||
pub const OPEN: LazyLock<Verb> = LazyLock::new(|| Verb::new("open").expect("unexpected bad verb"));
|
||||
pub const EDIT: LazyLock<Verb> = LazyLock::new(|| Verb::new("edit").expect("unexpected bad verb"));
|
||||
pub const PLAY: LazyLock<Verb> = LazyLock::new(|| Verb::new("play").expect("unexpected bad verb"));
|
||||
#[allow(non_snake_case)]
|
||||
pub fn OPEN() -> Self {
|
||||
Verb::new("open").expect("unexpected bad verb")
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn EDIT() -> Self {
|
||||
Verb::new("edit").expect("unexpected bad verb")
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn PLAY() -> Self {
|
||||
Verb::new("play").expect("unexpected bad verb")
|
||||
}
|
||||
}
|
||||
|
||||
impl Verb {
|
||||
@@ -927,9 +935,9 @@ pub struct BadCmdLineError {
|
||||
}
|
||||
|
||||
/// The struct representing a Windows command line.
|
||||
///
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
///
|
||||
/// This struct currently does nothing for validation for given command line.
|
||||
/// Because there is no standard for validating this.
|
||||
/// So I just write this struct as a placeholder for future extension.
|
||||
@@ -941,7 +949,9 @@ pub struct CmdLine {
|
||||
impl CmdLine {
|
||||
/// Create a new command line.
|
||||
pub fn new<S: AsRef<str>>(args: &[S]) -> Result<Self, BadCmdLineError> {
|
||||
Ok(Self { inner: args.iter().map(|s| s.as_ref().to_string()).collect() })
|
||||
Ok(Self {
|
||||
inner: args.iter().map(|s| s.as_ref().to_string()).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the full command line.
|
||||
@@ -981,7 +991,8 @@ impl FromStr for CmdLine {
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
// We simply split it by space for rough result.
|
||||
Ok(Self { inner: s.split(' ').map(|s| s.to_string()).collect() })
|
||||
Ok(Self {
|
||||
inner: s.split(' ').map(|s| s.to_string()).collect(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user