1
0

feat: add arbitrarily_delete_subkey_all for regext

This commit is contained in:
2026-05-29 08:58:15 +08:00
parent 119a4d0341
commit f0bd2c0b73
6 changed files with 87 additions and 31 deletions

View File

@@ -62,7 +62,7 @@ impl AppPathsKey {
Ok(key.is_some()) Ok(key.is_some())
} }
/// Ensure this application key is presented in App Paths. /// Ensure this application key is presented in App Paths key.
/// ///
/// Return true if we newly create this key, /// Return true if we newly create this key,
/// otherwise false indicating there already is an existing key. /// otherwise false indicating there already is an existing key.
@@ -79,15 +79,16 @@ impl AppPathsKey {
} }
} }
/// Delete this application key from App Paths. /// Delete this application key from App Paths key.
/// ///
/// If there is no such key in App Paths, /// Return true if we successfully delete this key,
/// this function does nothing. /// otherwise false indicating there is no such key (already deleted).
pub fn delete(&mut self, scope: Scope) -> Result<()> { pub fn delete(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
key.parent_key Ok(regext::arbitrarily_delete_subkey_all(
.delete_subkey_all(regext::blank_path_guard(self.key_name.inner())?)?; &key.parent_key,
Ok(()) regext::blank_path_guard(self.key_name.inner())?,
)?)
} }
fn open_scope_for_getter(&self, scope: Scope) -> Result<RegKey> { fn open_scope_for_getter(&self, scope: Scope) -> Result<RegKey> {

View File

@@ -68,6 +68,10 @@ impl ApplicationsKey {
Ok(key.is_some()) Ok(key.is_some())
} }
/// Ensure this application key is presented in Applications key.
///
/// Return true if we newly create this key,
/// otherwise false indicating there already is an existing key.
pub fn ensure(&mut self, scope: Scope) -> Result<bool> { pub fn ensure(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
if let None = key.this_key { if let None = key.this_key {
@@ -81,11 +85,16 @@ impl ApplicationsKey {
} }
} }
pub fn delete(&mut self, scope: Scope) -> Result<()> { /// Delete this application key from Applications key.
///
/// Return true if we successfully delete this key,
/// otherwise false indicating there is no such key (already deleted).
pub fn delete(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
key.parent_key Ok(regext::arbitrarily_delete_subkey_all(
.delete_subkey_all(regext::blank_path_guard(self.key_name.inner())?)?; &key.parent_key,
Ok(()) regext::blank_path_guard(self.key_name.inner())?,
)?)
} }
// YYC MARK: // YYC MARK:
@@ -176,7 +185,7 @@ impl ApplicationsKey {
} }
None => { None => {
// Delete shell and its all subkey. // Delete shell and its all subkey.
key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?; regext::arbitrarily_delete_subkey_all(&key, Self::NAMEOF_SHELL_VERB_PART1)?;
} }
} }
@@ -217,7 +226,7 @@ impl ApplicationsKey {
} }
None => { None => {
// Delete shell and its all subkey. // Delete shell and its all subkey.
key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?; regext::arbitrarily_delete_subkey_all(&key, Self::NAMEOF_DEFAULT_ICON_PART1)?;
} }
} }
@@ -298,7 +307,7 @@ impl ApplicationsKey {
} }
None => { None => {
// Delete this subkey. // Delete this subkey.
key.delete_subkey_all(Self::NAMEOF_SUPPORTED_TYPES)?; regext::arbitrarily_delete_subkey_all(&key, Self::NAMEOF_SUPPORTED_TYPES)?;
} }
} }

View File

@@ -66,6 +66,10 @@ impl ExtKey {
Ok(key.is_some()) Ok(key.is_some())
} }
/// Ensure this file extension key is presented in Classes key.
///
/// Return true if we newly create this key,
/// otherwise false indicating there already is an existing key.
pub fn ensure(&mut self, scope: Scope) -> Result<bool> { pub fn ensure(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
if let None = key.this_key { if let None = key.this_key {
@@ -79,11 +83,16 @@ impl ExtKey {
} }
} }
pub fn delete(&mut self, scope: Scope) -> Result<()> { /// Delete this file extension key from Classes key.
///
/// Return true if we successfully delete this key,
/// otherwise false indicating there is no such key (already deleted).
pub fn delete(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
key.parent_key Ok(regext::arbitrarily_delete_subkey_all(
.delete_subkey_all(regext::blank_path_guard(self.ext.dotted_inner())?)?; &key.parent_key,
Ok(()) regext::blank_path_guard(self.ext.dotted_inner())?,
)?)
} }
// YYC MARK: // YYC MARK:

View File

@@ -66,6 +66,10 @@ impl ProgIdKey {
Ok(key.is_some()) Ok(key.is_some())
} }
/// Ensure this ProgId key is presented in Classes key.
///
/// Return true if we newly create this key,
/// otherwise false indicating there already is an existing key.
pub fn ensure(&mut self, scope: Scope) -> Result<bool> { pub fn ensure(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
if let None = key.this_key { if let None = key.this_key {
@@ -79,11 +83,16 @@ impl ProgIdKey {
} }
} }
pub fn delete(&mut self, scope: Scope) -> Result<()> { /// Delete this ProgId key from Classes key.
///
/// Return true if we successfully delete this key,
/// otherwise false indicating there is no such key (already deleted).
pub fn delete(&mut self, scope: Scope) -> Result<bool> {
let key = self.open_scope_for_write(scope)?; let key = self.open_scope_for_write(scope)?;
key.parent_key Ok(regext::arbitrarily_delete_subkey_all(
.delete_subkey_all(regext::blank_path_guard(self.progid.to_string())?)?; &key.parent_key,
Ok(()) regext::blank_path_guard(self.progid.to_string())?,
)?)
} }
// YYC MARK: // YYC MARK:
@@ -209,7 +218,7 @@ impl ProgIdKey {
} }
None => { None => {
// Delete shell and its all subkey. // Delete shell and its all subkey.
key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?; regext::arbitrarily_delete_subkey_all(&key, Self::NAMEOF_SHELL_VERB_PART1)?;
} }
} }
@@ -284,7 +293,7 @@ impl ProgIdKey {
} }
None => { None => {
// Delete shell and its all subkey. // Delete shell and its all subkey.
key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?; regext::arbitrarily_delete_subkey_all(&key, Self::NAMEOF_DEFAULT_ICON_PART1)?;
} }
} }

View File

@@ -61,6 +61,30 @@ pub fn try_get_value<T: FromRegValue, N: AsRef<OsStr>>(
} }
} }
/// Delete all tree of given path of given key anyway.
///
/// This function was invented to fix the shortcoming of [RegKey::delete_subkey_all].
/// This function always delete given path of given key no matter it is existing.
/// Oppositely, [RegKey::delete_subkey_all] will return error if there is no such path.
///
/// Return true if we successfully delete this key,
/// otherwise false indicating there is no such key (already deleted).
pub fn arbitrarily_delete_subkey_all<P: AsRef<OsStr>>(
regkey: &RegKey,
path: P,
) -> std::io::Result<bool> {
match regkey.delete_subkey_all(path) {
Ok(()) => Ok(true),
Err(e) => match e.raw_os_error() {
Some(errno) => match errno as u32 {
ERROR_FILE_NOT_FOUND => Ok(false),
_ => Err(e),
},
_ => Err(e),
},
}
}
/// Get the name of only subkey in given key. /// Get the name of only subkey in given key.
/// ///
/// If there is only one subkey in given key, the return value is its name. /// If there is only one subkey in given key, the return value is its name.
@@ -110,6 +134,9 @@ pub fn get_all_string_subkey_names(regkey: &RegKey) -> std::io::Result<Vec<Strin
/// This is very dangerous and may be used by accident. /// This is very dangerous and may be used by accident.
/// So I create this to explicitly indicate this behavior and avoid any mis-type in code. /// So I create this to explicitly indicate this behavior and avoid any mis-type in code.
pub fn clean_all_contents(regkey: &RegKey) -> std::io::Result<()> { pub fn clean_all_contents(regkey: &RegKey) -> std::io::Result<()> {
// There is no possibility that this key do not existing,
// because what we are cleaning is self content.
// So directly use delete_subkey_all is okey.
regkey.delete_subkey_all("") regkey.delete_subkey_all("")
} }

View File

@@ -31,6 +31,7 @@ fn test_app_paths_key() {
// delete and ensure // delete and ensure
let rv = key.delete(scope); let rv = key.delete(scope);
eprintln!("{rv:?}");
assert!(rv.is_ok()); assert!(rv.is_ok());
let rv = key.is_exist(scope); let rv = key.is_exist(scope);
assert!(rv.is_ok()); assert!(rv.is_ok());