2025-10-05 18:04:20 +08:00
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
use std::process;
|
|
|
|
|
use wfassoc::{Program, RegisterKind, WfError};
|
|
|
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, WfError>;
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<ForTarget> for RegisterKind {
|
|
|
|
|
fn from(target: ForTarget) -> Self {
|
|
|
|
|
match target {
|
|
|
|
|
ForTarget::User => RegisterKind::User,
|
|
|
|
|
ForTarget::System => RegisterKind::System,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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<()> {
|
|
|
|
|
let program = Program::new();
|
|
|
|
|
let kind: RegisterKind = cli.for_which.into();
|
|
|
|
|
program.register(kind)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_unregister(cli: Cli) -> Result<()> {
|
|
|
|
|
let program = Program::new();
|
|
|
|
|
program.unregister()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_query(cli: Cli) -> Result<()> {
|
|
|
|
|
let program = Program::new();
|
|
|
|
|
program.query()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
|
|
2025-10-04 22:04:30 +08:00
|
|
|
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)
|
|
|
|
|
});
|
2025-10-04 22:04:30 +08:00
|
|
|
}
|