1
0

feat: add two registry utility functions

This commit is contained in:
2026-05-16 22:35:36 +08:00
parent 92ec83b8d7
commit c164bb73bc
2 changed files with 56 additions and 0 deletions

View File

@@ -16,10 +16,14 @@ pub enum Error {
NoPrivilege, NoPrivilege,
#[error("given registry key is inexistant")] #[error("given registry key is inexistant")]
InexistantKey, InexistantKey,
#[error("registry operation error: {0}")] #[error("registry operation error: {0}")]
BadRegOp(#[from] std::io::Error), BadRegOp(#[from] std::io::Error),
#[error("{0}")] #[error("{0}")]
BadSoleSubKey(#[from] regext::OnlySubKeyError),
#[error("{0}")]
UnexpectedBlankKey(#[from] regext::BlankPathError), UnexpectedBlankKey(#[from] regext::BlankPathError),
#[error("{0}")] #[error("{0}")]
LoadIconRc(#[from] concept::LoadIconRcError), LoadIconRc(#[from] concept::LoadIconRcError),
#[error("{0}")] #[error("{0}")]

View File

@@ -7,6 +7,7 @@ use thiserror::Error as TeError;
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND; use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;
use windows_sys::Win32::System::Registry::REG_SAM_FLAGS; use windows_sys::Win32::System::Registry::REG_SAM_FLAGS;
use winreg::RegKey; use winreg::RegKey;
use winreg::enums::RegType;
use winreg::types::FromRegValue; use winreg::types::FromRegValue;
// region: Extra Operations // region: Extra Operations
@@ -60,6 +61,57 @@ pub fn try_get_value<T: FromRegValue, N: AsRef<OsStr>>(
} }
} }
/// The error occurs when fetching the name of only subkey.
#[derive(Debug, TeError)]
pub enum OnlySubKeyError {
#[error("registry operation error: {0}")]
Io(#[from] std::io::Error),
#[error("there is no any subkey in given RegKey")]
NoSubKey,
#[error("there is more than one subkey in given RegKey")]
TooManySubKey,
}
/// Get the name of only subkey in given key.
///
/// This is usually used for ShellVerb fetching.
pub fn get_sole_subkey_name(regkey: &RegKey) -> Result<String, OnlySubKeyError> {
let mut subkey_enumerator = regkey.enum_keys();
// Get first one.
let rv = match subkey_enumerator.next() {
Some(key) => key?,
None => return Err(OnlySubKeyError::NoSubKey),
};
// Check whether there is second one.
match subkey_enumerator.next() {
Some(_) => Err(OnlySubKeyError::TooManySubKey),
None => Ok(rv),
}
}
/// Get the name list of all "string" subkeys in given key.
///
/// This is usually used for "OpenWithProgIds" subkey.
pub fn get_all_string_subkey_names(regkey: &RegKey) -> std::io::Result<Vec<String>> {
regkey
.enum_values()
.filter_map(|item| match item {
Ok((key, value)) => {
// Check subkey type.
if value.vtype == RegType::REG_SZ {
Some(Ok(key))
} else {
None
}
}
Err(err) => Some(Err(err)),
})
.collect::<Result<Vec<_>, _>>()
}
// endregion // endregion
// region: Blank Path Guard // region: Blank Path Guard