1
0

write shit

This commit is contained in:
2025-10-17 14:19:26 +08:00
parent dab91f1581
commit bdb6b4ceee
6 changed files with 251 additions and 115 deletions

View File

@@ -4,8 +4,8 @@
#[cfg(not(target_os = "windows"))]
compile_error!("Crate wfassoc is only supported on Windows.");
pub(crate) mod assoc;
pub(crate) mod utilities;
pub mod assoc;
pub mod utilities;
use indexmap::{IndexMap, IndexSet};
use std::ffi::OsStr;
@@ -26,6 +26,8 @@ pub enum Error {
BadRegOper(#[from] std::io::Error),
#[error("{0}")]
CastOsStr(#[from] utilities::CastOsStrError),
#[error("{0}")]
ParseExt(#[from] assoc::ParseExtError),
#[error("no administrative privilege")]
NoPrivilege,
@@ -85,7 +87,7 @@ impl TryFrom<View> for Scope {
impl Scope {
/// Check whether we have enough privilege when operating in current scope.
/// If we have, return true, otherwise false.
fn has_privilege(&self) -> bool {
pub fn has_privilege(&self) -> bool {
// If we operate on System, and we do not has privilege,
// we think we do not have privilege, otherwise,
// there is no privilege required.
@@ -127,7 +129,7 @@ pub struct Manner {
}
impl Manner {
pub fn new(argv: &str) -> Self {
fn new(argv: &str) -> Self {
Self {
argv: argv.to_string(),
}
@@ -167,23 +169,25 @@ impl Program {
///
/// - `My App`
/// - `3DViewer`
/// - `我的Qt最时尚`
/// - `我的Qt程序` (means "My Qt App" in English)
///
/// More preciously, `identifier` will be used as the vendor part of ProgId.
///
/// `full_path` is the fully qualified path to the application.
pub fn new(identifier: &str, full_path: &Path) -> Self {
pub fn new(identifier: &str, full_path: &Path) -> Result<Self> {
// TODO: Add checker for identifier
Self {
Ok(Self {
identifier: identifier.to_string(),
full_path: full_path.to_path_buf(),
manners: IndexSet::new(),
exts: IndexMap::new(),
}
})
}
/// Add manner provided by this program.
pub fn add_manner(&mut self, manner: Manner) -> Result<Token> {
pub fn add_manner(&mut self, manner: &str) -> Result<Token> {
// Create manner from string
let manner = Manner::new(manner);
// Backup a stringfied manner for error output.
let manner_str = manner.to_string();
// Insert manner.
@@ -201,11 +205,14 @@ impl Program {
}
/// Add file extension supported by this program and its associated manner.
pub fn add_ext(&mut self, ext: assoc::Ext, token: Token) -> Result<Token> {
pub fn add_ext(&mut self, ext: &str, token: Token) -> Result<Token> {
// Check manner token
if let None = self.get_manner(token) {
return Err(Error::InvalidAssocManner);
}
// Create extension from string
let ext = assoc::Ext::new(ext)?;
// Backup a stringfied extension for error output.
let ext_str = ext.to_string();
// Insert file extension
@@ -353,4 +360,19 @@ impl Program {
}
}
impl Program {
/// Set the default "open with" of given extension to this program.
pub fn link_ext(&self, ext: Token) -> Result<()> {
todo!()
}
/// Remove this program from the default "open with" of given extension.
///
/// If the default "open with" of given extension is not our program,
/// this function do nothing.
pub fn unlink_ext(&self, ext: Token) -> Result<()> {
todo!()
}
}
// endregion