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

162 lines
4.4 KiB
Rust
Raw Normal View History

2025-10-17 14:19:26 +08:00
pub(crate) mod cli;
pub(crate) mod manifest;
use clap::Parser;
use cli::{Cli, Commands, Target};
use comfy_table::Table;
use manifest::Manifest;
2025-10-18 09:55:08 +08:00
use std::collections::HashMap;
use std::process;
2025-10-09 13:53:31 +08:00
use thiserror::Error as TeError;
use wfassoc::{Program, Scope, Token, 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-17 14:19:26 +08:00
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 },
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
2025-10-17 14:19:26 +08:00
// region: Correponding Runner
2025-10-05 18:04:20 +08:00
struct Composition {
program: Program,
ext_tokens: Vec<Token>,
}
impl Composition {
pub fn new(cli: &Cli) -> Result<Composition> {
// Open file and read manifest TOML file
let mf = Manifest::from_file(&cli.config_file)?;
// Create instance
let mut program = Program::new(&mf.identifier, &cli.config_file)?;
// Setup manner
let mut manner_token_map: HashMap<&str, Token> = HashMap::new();
for (k, v) in mf.manners.iter() {
let token = program.add_manner(v.as_str())?;
manner_token_map.insert(k.as_str(), token);
}
// Setup extension
let mut ext_tokens = Vec::new();
for (k, v) in mf.exts.iter() {
let token = match manner_token_map.get(v.as_str()) {
Some(v) => v,
None => {
return Err(Error::InvalidMannerName {
manner: v.to_string(),
ext: k.clone(),
});
}
};
let token = program.add_ext(k.as_str(), *token)?;
ext_tokens.push(token);
}
// Okey
Ok(Self {
program,
ext_tokens,
})
}
pub fn get_program(&self) -> &Program {
&self.program
2025-10-17 14:19:26 +08:00
}
pub fn iter_ext_tokens(&self) -> impl Iterator<Item = Token> {
self.ext_tokens.iter().copied()
2025-10-17 14:19:26 +08:00
}
2025-10-05 18:04:20 +08:00
}
fn run_register(cli: &Cli, target: &Target) -> Result<()> {
let composition = Composition::new(cli)?;
let scope = target.clone().into();
// Register program
let program = composition.get_program();
program.register(scope)?;
// Link all file extensions
for token in composition.iter_ext_tokens() {
program.link_ext(token, scope)?;
}
println!("Register OK.");
2025-10-09 13:53:31 +08:00
Ok(())
2025-10-05 18:04:20 +08:00
}
fn run_unregister(cli: &Cli, target: &Target) -> Result<()> {
let composition = Composition::new(cli)?;
let scope = target.clone().into();
// Unlink all file extensions
let program = composition.get_program();
for token in composition.iter_ext_tokens() {
program.unlink_ext(token, scope)?;
}
// Unregister prorgam
program.unregister(scope)?;
println!("Unregister OK.");
2025-10-09 13:53:31 +08:00
Ok(())
2025-10-05 18:04:20 +08:00
}
2025-10-17 14:19:26 +08:00
fn run_query(cli: &Cli) -> Result<()> {
let composition = Composition::new(cli)?;
let mut table = Table::new();
table.set_header(["Extension", "Hybrid", "User", "System"]);
let program = composition.get_program();
for token in composition.iter_ext_tokens() {
let cell_ext = program.get_ext_str(token).unwrap();
let cell_hybrid = program
.query_ext(token, View::Hybrid)?
.map(|pi| pi.to_string())
.unwrap_or_default();
let cell_user = program
.query_ext(token, View::User)?
.map(|pi| pi.to_string())
.unwrap_or_default();
let cell_system = program
.query_ext(token, View::System)?
.map(|pi| pi.to_string())
.unwrap_or_default();
table.add_row([cell_ext, cell_hybrid, cell_user, cell_system]);
}
println!("{table}");
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 { target } => run_register(&cli, target),
Commands::Unregister { target } => run_unregister(&cli, target),
2025-10-17 14:19:26 +08:00
Commands::Query => run_query(&cli),
2025-10-05 18:04:20 +08:00
};
rv.unwrap_or_else(|e| {
eprintln!("Runtime error: {}.", e);
process::exit(1)
});
}