From 72d0d5f1e2c75a1da53d28b94a73583098cb0e9c Mon Sep 17 00:00:00 2001 From: Antonin Ruan Date: Thu, 12 Mar 2026 20:31:37 +0100 Subject: [PATCH] refactor subcommand behavior --- src/main.rs | 7 ++----- src/subcommands/add.rs | 11 ++++++----- src/subcommands/hash_object.rs | 9 +++++---- src/subcommands/init.rs | 18 ++++++++++-------- src/subcommands/mod.rs | 13 ++++++++----- src/subcommands/rm.rs | 11 ++++++----- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 78c8a1a..1a3c1c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use clap::Parser; mod git_fs; mod subcommands; -use subcommands::{Subcommand, SubcommandType}; +use subcommands::{GitSubcommand, SubcommandType}; const GIT_DIR: &str = ".rgit"; @@ -17,8 +17,5 @@ struct CmdArgs { fn main() { let args = CmdArgs::parse(); - match args.cmd.run() { - Err(str) => println!("Some error occured: {str}"), - Ok(str) => println!("{str}"), - } + if let Err(str) = args.cmd.run() { eprintln!("Some error occured: {str}") } } diff --git a/src/subcommands/add.rs b/src/subcommands/add.rs index 6410011..7f9cb91 100644 --- a/src/subcommands/add.rs +++ b/src/subcommands/add.rs @@ -7,17 +7,18 @@ use crate::{ subcommands::hash_object::HashObjectSubcommand, }; -use super::Subcommand; +use super::GitSubcommand; #[derive(Parser, Debug)] pub struct AddSubcommand { pub paths: Vec, } -impl Subcommand for AddSubcommand { - fn run(&self) -> Result { +impl GitSubcommand for AddSubcommand { + fn run(&self) -> Result<()> { if self.paths.is_empty() { - return Ok(String::from("Nothing specified, nothing added")); + println!("Nothing specified, nothing added"); + return Ok(()); } let mut index = Index::load()?; @@ -38,6 +39,6 @@ impl Subcommand for AddSubcommand { index.save()?; - Ok(String::from("")) + Ok(()) } } diff --git a/src/subcommands/hash_object.rs b/src/subcommands/hash_object.rs index ade71ae..6dffa43 100644 --- a/src/subcommands/hash_object.rs +++ b/src/subcommands/hash_object.rs @@ -4,7 +4,7 @@ use hex::encode; use crate::{ git_fs::object::{Blob, GitObject}, - subcommands::Subcommand, + subcommands::GitSubcommand, }; #[derive(Parser, Debug)] @@ -30,9 +30,10 @@ impl HashObjectSubcommand { } } -impl Subcommand for HashObjectSubcommand { - fn run(&self) -> Result { +impl GitSubcommand for HashObjectSubcommand { + fn run(&self) -> Result<()> { let hash = self.run_raw()?; - Ok(encode(hash)) + println!("{}", encode(hash)); + Ok(()) } } diff --git a/src/subcommands/init.rs b/src/subcommands/init.rs index 1785b3b..81e2e19 100644 --- a/src/subcommands/init.rs +++ b/src/subcommands/init.rs @@ -5,15 +5,15 @@ use std::{fs, path::Path}; use crate::GIT_DIR; use crate::git_fs::head::Head; -use crate::subcommands::Subcommand; +use crate::subcommands::GitSubcommand; #[derive(Parser, Debug)] pub struct InitSubcommand { directory: Option, } -impl Subcommand for InitSubcommand { - fn run(&self) -> Result { +impl GitSubcommand for InitSubcommand { + fn run(&self) -> Result<()> { let path = match &self.directory { None => Path::new("."), Some(path) => Path::new(path), @@ -40,15 +40,17 @@ impl Subcommand for InitSubcommand { let canonical_path = path.canonicalize(); if new_repo { - Ok(format!( + println!( "Reinitialized exisiting Git repo in {}", canonical_path?.display() - )) + ); } else { - Ok(format!( + println!( "Initialized empty Git repo in {}", canonical_path?.display() - )) - } + ); + }; + + Ok(()) } } diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs index 1971aa8..4b5bf52 100644 --- a/src/subcommands/mod.rs +++ b/src/subcommands/mod.rs @@ -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; +pub trait GitSubcommand { + fn run(&self) -> Result<()>; } -impl Subcommand for SubcommandType { - fn run(&self) -> Result { +impl GitSubcommand for SubcommandType { + fn run(&self) -> Result<()> { match self { Self::Add(cmd) => cmd.run(), Self::Rm(cmd) => cmd.run(), diff --git a/src/subcommands/rm.rs b/src/subcommands/rm.rs index e7f91ef..2cebe6b 100644 --- a/src/subcommands/rm.rs +++ b/src/subcommands/rm.rs @@ -4,7 +4,7 @@ use std::path::Path; use crate::git_fs::{gitignore::expands_and_filter_path, index::Index}; -use super::Subcommand; +use super::GitSubcommand; #[derive(Parser, Debug)] pub struct RmSubcommand { @@ -15,10 +15,11 @@ pub struct RmSubcommand { pub paths: Vec, } -impl Subcommand for RmSubcommand { - fn run(&self) -> Result { +impl GitSubcommand for RmSubcommand { + fn run(&self) -> Result<()> { if self.paths.is_empty() { - return Ok(String::from("Nothing specified, nothing removed")); + println!("Nothing specified, nothing removed"); + return Ok(()); } let mut index = Index::load()?; @@ -32,6 +33,6 @@ impl Subcommand for RmSubcommand { index.save()?; - Ok(String::from("")) + Ok(()) } }