write shit
This commit is contained in:
@ -4,6 +4,9 @@
|
|||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
compile_error!("Crate wfassoc is only supported on Windows.");
|
compile_error!("Crate wfassoc is only supported on Windows.");
|
||||||
|
|
||||||
|
/// The expand of winreg crate according to our module requirements.
|
||||||
|
mod winregex;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -249,7 +252,14 @@ impl FileExt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_open_with(&self, view: View) -> Result<impl Iterator<Item = ProgId>, Error> {
|
pub fn iter_open_with(&self, view: View) -> Result<impl Iterator<Item = ProgId>, Error> {
|
||||||
todo!()
|
let viewer = match self.open_view(view)? {
|
||||||
|
Some(viewer) => viewer,
|
||||||
|
None => return Ok(std::iter::empty::<ProgId>()),
|
||||||
|
};
|
||||||
|
let it = winregex::iter_sz_keys(&viewer);
|
||||||
|
let it = winregex::exclude_default_key(it);
|
||||||
|
|
||||||
|
Ok(it.map(|s| ProgId::from(s.as_str())))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_open_with(&mut self, scope: Scope, prog_id: &ProgId) -> Result<(), Error> {
|
pub fn insert_open_with(&mut self, scope: Scope, prog_id: &ProgId) -> Result<(), Error> {
|
||||||
|
96
wfassoc/src/winregex.rs
Normal file
96
wfassoc/src/winregex.rs
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
use winreg::RegKey;
|
||||||
|
use winreg::EnumKeys;
|
||||||
|
use winreg::enums::RegType;
|
||||||
|
// use thiserror::Error as TeError;
|
||||||
|
|
||||||
|
// #[derive(Debug, TeError)]
|
||||||
|
// pub enum WinRegExError {
|
||||||
|
// #[error("{0}")]
|
||||||
|
// Io(#[from] std::io::Error)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub type WinRegExResult<T> = Result<T, WinRegExError>;
|
||||||
|
|
||||||
|
// region Iterate Keys without Error
|
||||||
|
|
||||||
|
pub struct IterKeys<'a> {
|
||||||
|
iter_keys: EnumKeys<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IterKeys<'a> {
|
||||||
|
fn new(regkey: &'a RegKey) -> Self {
|
||||||
|
Self { iter_keys: regkey.enum_keys() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for IterKeys<'a> {
|
||||||
|
type Item = String;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
loop {
|
||||||
|
match self.iter_keys.next() {
|
||||||
|
Some(key) => match key {
|
||||||
|
Ok(key) => return Some(key),
|
||||||
|
Err(_) => continue,
|
||||||
|
},
|
||||||
|
None => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter_keys<'a>(regkey: &'a RegKey) -> IterKeys<'a> {
|
||||||
|
IterKeys::new(regkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region Iterate REG_SZ Keys
|
||||||
|
|
||||||
|
pub struct IterSzKeys<'a> {
|
||||||
|
regkey: &'a RegKey,
|
||||||
|
iter_keys: IterKeys<'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IterSzKeys<'a> {
|
||||||
|
fn new(regkey: &'a RegKey) -> Self {
|
||||||
|
Self { regkey, iter_keys: IterKeys::new(regkey) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for IterSzKeys<'a> {
|
||||||
|
type Item = String;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
loop {
|
||||||
|
if let Some(key) = self.iter_keys.next() {
|
||||||
|
// Check whether given key is REG_SZ.
|
||||||
|
match self.regkey.get_raw_value(&key) {
|
||||||
|
Ok(raw_value) => {
|
||||||
|
if matches!(raw_value.vtype, RegType::REG_SZ) {
|
||||||
|
return Some(key);
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => continue,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Untitled
|
||||||
|
///
|
||||||
|
/// Given RegKey must has KEY_READ permission, otherwise the result may be inaccurate.
|
||||||
|
pub fn iter_sz_keys<'a>(regkey: &'a RegKey) -> IterSzKeys<'a> {
|
||||||
|
IterSzKeys::new(regkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
pub fn exclude_default_key(it: impl Iterator<Item = String>) -> impl Iterator<Item = String> {
|
||||||
|
it.filter(|x| !x.is_empty())
|
||||||
|
}
|
Reference in New Issue
Block a user