refactor subcommand behavior

This commit is contained in:
Antonin Ruan
2026-03-12 20:31:37 +01:00
parent c118829c11
commit 72d0d5f1e2
6 changed files with 37 additions and 32 deletions
+8 -5
View File
@@ -3,6 +3,7 @@ use crate::subcommands::{
test::TestSubcommand,
};
use anyhow::Result;
use clap::Subcommand;
mod add;
mod hash_object;
@@ -10,7 +11,7 @@ mod init;
mod rm;
mod test;
#[derive(clap::Parser, Debug)]
#[derive(Debug, Subcommand)]
pub enum SubcommandType {
/// Add file(s) to index
Add(AddSubcommand),
@@ -18,16 +19,18 @@ pub enum SubcommandType {
Rm(RmSubcommand),
/// Init a Git repository
Init(InitSubcommand),
#[clap(hide = true)]
HashObject(HashObjectSubcommand),
#[clap(hide = true)]
Test(TestSubcommand),
}
pub trait Subcommand {
fn run(&self) -> Result<String>;
pub trait GitSubcommand {
fn run(&self) -> Result<()>;
}
impl Subcommand for SubcommandType {
fn run(&self) -> Result<String> {
impl GitSubcommand for SubcommandType {
fn run(&self) -> Result<()> {
match self {
Self::Add(cmd) => cmd.run(),
Self::Rm(cmd) => cmd.run(),