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

@ -1,8 +1,12 @@
use clap::{Parser, Subcommand};
use comfy_table::Table;
use std::process;
pub(crate) mod cli;
pub(crate) mod manifest;
use clap::Parser;
use std::{collections::HashMap, path::Path, process};
use thiserror::Error as TeError;
use wfassoc::{Error as WfError, Ext, Scope, View};
use wfassoc::{Program, Token};
use cli::{Cli, Commands};
use manifest::Manifest;
// region: Basic Types
@ -11,7 +15,17 @@ use wfassoc::{Error as WfError, Ext, Scope, View};
enum Error {
/// Error from wfassoc core.
#[error("{0}")]
Core(#[from] WfError),
Core(#[from] wfassoc::Error),
/// Error when parsing manifest TOML file.
#[error("invalid manifest file: {0}")]
Manifest(#[from] manifest::Error),
/// Error when specifying invalid manner name for extension.
#[error("extension {ext} associated manner {manner} is invalid in manifest file")]
InvalidMannerName {
manner: String,
ext: String
},
}
/// Result type used in this executable.
@ -19,116 +33,46 @@ type Result<T> = std::result::Result<T, Error>;
// endregion
// 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<()> {
fn build_program(cli: &Cli) -> Result<Program> {
// Open file and read manifest TOML file
let mf = Manifest::from_file(&cli.config_file)?;
// Create instance
let rv = Program::new(&mf.identifier, &Path::from(mf.path.as_str()))?;
// Setup manner
let manners: HashMap<&str, Token> = HashMap::new();
for (k, v) in mf.manners.iter() {
let token = rv.add_manner(v.as_str())?;
manners.insert(k.as_str(), token);
}
// Setup extension
for (k, v) in mf.exts.iter() {
let token = match manners.get(v.as_str()) {
Some(v) => v,
None => return Err(Error::InvalidMannerName { manner: v.to_string(), ext: k.clone() }),
};
rv.add_ext(k.as_str(), *token)?;
}
// Okey
Ok(rv)
}
fn run_register(cli: &Cli) -> Result<()> {
// let program = Program::new();
// let kind: RegisterKind = cli.for_which.into();
// program.register(kind)?;
Ok(())
}
fn run_unregister(cli: Cli) -> Result<()> {
fn run_unregister(cli: &Cli) -> Result<()> {
// let program = Program::new();
// program.unregister()?;
Ok(())
}
fn run_query(cli: Cli) -> Result<()> {
let exts = [
".jpg", ".jfif", ".gif", ".bmp", ".png", ".ico", ".jpeg", ".tif", ".tiff", ".webp", ".svg",
".kra", ".xcf", ".avif", ".qoi", ".apng", ".exr",
];
for ext in exts.iter().map(|e| Ext::new(e).unwrap()) {
if let Some(ext_assoc) = ext.query(View::Hybrid) {
println!("{:?}", ext_assoc)
}
}
// 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}");
// let program = Program::new();
// program.query()?;
//println!("Has privilege: {}", wfassoc::has_privilege());
fn run_query(cli: &Cli) -> Result<()> {
Ok(())
}
@ -138,9 +82,9 @@ fn main() {
let cli = Cli::parse();
let rv = match &cli.command {
Commands::Register => run_register(cli),
Commands::Unregister => run_unregister(cli),
Commands::Query => run_query(cli),
Commands::Register => run_register(&cli),
Commands::Unregister => run_unregister(&cli),
Commands::Query => run_query(&cli),
};
rv.unwrap_or_else(|e| {
eprintln!("Runtime error: {}.", e);