1
0
Files
wfassoc/wfassoc_exec/src/main.rs

150 lines
3.9 KiB
Rust
Raw Normal View History

2025-10-05 18:04:20 +08:00
use clap::{Parser, Subcommand};
2025-10-09 15:32:22 +08:00
use comfy_table::Table;
2025-10-05 18:04:20 +08:00
use std::process;
2025-10-09 13:53:31 +08:00
use thiserror::Error as TeError;
2025-10-15 13:15:29 +08:00
use wfassoc::{Error as WfError, Ext, Scope, View};
2025-10-05 18:04:20 +08:00
2025-10-09 13:53:31 +08:00
// region: Basic Types
/// All errors occurs in this executable.
#[derive(TeError, Debug)]
enum Error {
/// Error from wfassoc core.
#[error("{0}")]
2025-10-09 15:32:22 +08:00
Core(#[from] WfError),
2025-10-09 13:53:31 +08:00
}
/// Result type used in this executable.
type Result<T> = std::result::Result<T, Error>;
// endregion
2025-10-05 18:04:20 +08:00
// region: Command Line Parser
/// Simple program to manage Windows file associations
#[derive(Parser)]
#[command(name = "Windows File Association Operator", version, about)]
struct Cli {
/// The toml file introducing the complete program
#[arg(
short = 'c',
long = "config",
value_name = "PROG_CONFIG",
required = true
)]
config_file: String,
/// The scope where wfassoc operate
#[arg(short = 'f', long = "for", value_name = "TARGET", value_enum, default_value_t = ForTarget::User)]
for_which: ForTarget,
#[command(subcommand)]
command: Commands,
}
#[derive(clap::ValueEnum, Clone)]
enum ForTarget {
#[value(name = "user")]
User,
#[value(name = "system")]
System,
}
2025-10-09 13:53:31 +08:00
// impl From<ForTarget> for RegisterKind {
// fn from(target: ForTarget) -> Self {
// match target {
// ForTarget::User => RegisterKind::User,
// ForTarget::System => RegisterKind::System,
// }
// }
// }
2025-10-05 18:04:20 +08:00
#[derive(Subcommand)]
enum Commands {
/// Register the program
#[command(name = "register")]
Register,
/// Unregister the program
#[command(name = "unregister")]
Unregister,
/// Query file associations
#[command(name = "query")]
Query,
}
// endregion
// region: Correponding Runner
fn run_register(cli: Cli) -> Result<()> {
2025-10-09 13:53:31 +08:00
// let program = Program::new();
// let kind: RegisterKind = cli.for_which.into();
// program.register(kind)?;
Ok(())
2025-10-05 18:04:20 +08:00
}
fn run_unregister(cli: Cli) -> Result<()> {
2025-10-09 13:53:31 +08:00
// let program = Program::new();
// program.unregister()?;
Ok(())
2025-10-05 18:04:20 +08:00
}
fn run_query(cli: Cli) -> Result<()> {
2025-10-09 15:32:22 +08:00
let exts = [
".jpg", ".jfif", ".gif", ".bmp", ".png", ".ico", ".jpeg", ".tif", ".tiff", ".webp", ".svg",
".kra", ".xcf", ".avif", ".qoi", ".apng", ".exr",
];
2025-10-15 13:15:29 +08:00
for ext in exts.iter().map(|e| Ext::new(e).unwrap()) {
2025-10-09 16:07:23 +08:00
if let Some(ext_assoc) = ext.query(View::Hybrid) {
println!("{:?}", ext_assoc)
2025-10-09 15:32:22 +08:00
}
}
2025-10-09 16:07:23 +08:00
// let mut table = Table::new();
// table.set_header(["Extension", "Default Open", "Open With"]);
// for ext in exts.iter().map(|e| FileExt::new(e).unwrap()) {
// if let Some(ext_assoc) = ext.query(View::Hybrid) {
// if ext_assoc.len_open_with_progid() == 0 {
// table.add_row([ext.to_string().as_str(), ext_assoc.get_default(), ""]);
// } else {
// for (i, open_with_entry) in ext_assoc.iter_open_with_progids().enumerate() {
// if i == 0 {
// table.add_row([
// ext.to_string().as_str(),
// ext_assoc.get_default(),
// open_with_entry,
// ]);
// } else {
// table.add_row(["", "", open_with_entry]);
// }
// }
// }
// } else {
// table.add_row([ext.to_string().as_str(), "", ""]);
// }
// }
// println!("{table}");
2025-10-09 15:32:22 +08:00
2025-10-09 13:53:31 +08:00
// let program = Program::new();
// program.query()?;
2025-10-09 15:32:22 +08:00
//println!("Has privilege: {}", wfassoc::has_privilege());
2025-10-09 13:53:31 +08:00
Ok(())
2025-10-05 18:04:20 +08:00
}
// endregion
fn main() {
2025-10-05 18:04:20 +08:00
let cli = Cli::parse();
let rv = match &cli.command {
Commands::Register => run_register(cli),
Commands::Unregister => run_unregister(cli),
Commands::Query => run_query(cli),
};
rv.unwrap_or_else(|e| {
eprintln!("Runtime error: {}.", e);
process::exit(1)
});
}