Files
pile/crates/pile/src/command/mod.rs
2026-03-05 21:43:59 -08:00

75 lines
1.6 KiB
Rust

use anyhow::Result;
use clap::Subcommand;
use pile_toolbox::cancelabletask::{CancelFlag, CancelableTask, CancelableTaskError};
mod check;
mod index;
mod init;
mod lookup;
mod probe;
use crate::GlobalContext;
#[derive(Debug, Subcommand)]
pub enum SubCommand {
/// Create an empty dataset
Init {
#[command(flatten)]
cmd: init::InitCommand,
},
/// Check dataset config
Check {
#[command(flatten)]
cmd: check::CheckCommand,
},
/// Rebuild all indices
Index {
#[command(flatten)]
cmd: index::IndexCommand,
},
/// Search all sources
Lookup {
#[command(flatten)]
cmd: lookup::LookupCommand,
},
/// Print all metadata from an item
Probe {
#[command(flatten)]
cmd: probe::ProbeCommand,
},
}
impl CliCmdDispatch for SubCommand {
fn start(self, ctx: GlobalContext) -> Result<CancelableTask<Result<i32>>> {
match self {
Self::Init { cmd } => cmd.start(ctx),
Self::Check { cmd } => cmd.start(ctx),
Self::Index { cmd } => cmd.start(ctx),
Self::Lookup { cmd } => cmd.start(ctx),
Self::Probe { cmd } => cmd.start(ctx),
}
}
}
pub(super) trait CliCmdDispatch: Sized + Send + 'static {
fn start(self, ctx: GlobalContext) -> Result<CancelableTask<Result<i32>>>;
}
impl<T: CliCmd> CliCmdDispatch for T {
fn start(self, ctx: GlobalContext) -> Result<CancelableTask<Result<i32>>> {
Ok(CancelableTask::spawn(|flag| self.run(ctx, flag)))
}
}
pub(super) trait CliCmd: Sized + Send + 'static {
fn run(
self,
ctx: GlobalContext,
flag: CancelFlag,
) -> impl std::future::Future<Output = Result<i32, CancelableTaskError<anyhow::Error>>> + Send;
}