feat: finish exec runner
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use crate::cli;
|
||||
use crate::manifest;
|
||||
use comfy_table::Table;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::Path;
|
||||
use thiserror::Error as TeError;
|
||||
use toml;
|
||||
|
||||
// region: Error Handling
|
||||
|
||||
@@ -20,6 +23,19 @@ pub enum Error {
|
||||
/// Error when operating Program.
|
||||
#[error("{0}")]
|
||||
Program(#[from] wfassoc::highlevel::ProgramError),
|
||||
/// Error when serializing TOML
|
||||
#[error("{0}")]
|
||||
SerializeToml(#[from] toml::ser::Error),
|
||||
|
||||
/// Find duplicated name when converting extension name to index
|
||||
#[error("given extension name {0} has been specified more than one time")]
|
||||
DupExtName(String),
|
||||
/// Find invalid name when converting extension name to index
|
||||
#[error("given extension name {0} is not presented in application")]
|
||||
BadExtName(String),
|
||||
/// Find star (*) extension name with other extension names when converting extension name to index
|
||||
#[error("given extension name {0} is not presented in application")]
|
||||
ExclusiveStarExtName(String),
|
||||
}
|
||||
|
||||
/// Result type used in this module.
|
||||
@@ -27,13 +43,38 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// endregion
|
||||
|
||||
// region: Utilities Functions
|
||||
// region: Utilities
|
||||
|
||||
fn stringified_exts_to_indices(
|
||||
program: &wfassoc::Program,
|
||||
exts: Vec<String>,
|
||||
) -> Result<Vec<usize>> {
|
||||
todo!()
|
||||
// Check for duplicate extension names
|
||||
let mut seen = HashSet::new();
|
||||
for ext in &exts {
|
||||
if !seen.insert(ext.as_str()) {
|
||||
return Err(Error::DupExtName(ext.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
// Check for star (*) with other extensions
|
||||
let has_star = exts.iter().any(|ext| ext == "*");
|
||||
if has_star && exts.len() > 1 {
|
||||
return Err(Error::ExclusiveStarExtName("*".to_string()));
|
||||
}
|
||||
|
||||
// If star is present alone, return fixed list from zero to the maximum ext index.
|
||||
if has_star {
|
||||
return Ok((0..program.get_ext_count()).collect());
|
||||
}
|
||||
|
||||
// Convert each extension name to index using program.find_ext()
|
||||
let indices = exts
|
||||
.into_iter()
|
||||
.map(|ext| program.find_ext(&ext).ok_or(Error::BadExtName(ext)))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
Ok(indices)
|
||||
}
|
||||
|
||||
// endregion
|
||||
@@ -87,7 +128,31 @@ fn run_ext_list(
|
||||
view: wfassoc::View,
|
||||
style: cli::ExtListStyle,
|
||||
) -> Result<()> {
|
||||
todo!()
|
||||
// Fetch info
|
||||
let mut ext_list: HashMap<String, Option<String>> = HashMap::new();
|
||||
for index in 0..program.get_ext_count() {
|
||||
let ext = program.get_ext(index)?;
|
||||
let status = program.query_ext(view, index)?;
|
||||
ext_list.insert(ext.dotted_inner(), status.map(|s| s.get_name().to_string()));
|
||||
}
|
||||
|
||||
// Output by styles
|
||||
use cli::ExtListStyle;
|
||||
match style {
|
||||
ExtListStyle::Human => {
|
||||
let mut table = Table::new();
|
||||
table.set_header(["Extension", "Association"]);
|
||||
for (k, v) in ext_list {
|
||||
table.add_row([k, v.unwrap_or("".to_string())]);
|
||||
}
|
||||
println!("{table}");
|
||||
}
|
||||
ExtListStyle::Machine => {
|
||||
let stoml = toml::to_string(&ext_list)?;
|
||||
println!("{stoml}")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user