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
+2 -5
View File
@@ -3,7 +3,7 @@ use clap::Parser;
mod git_fs; mod git_fs;
mod subcommands; mod subcommands;
use subcommands::{Subcommand, SubcommandType}; use subcommands::{GitSubcommand, SubcommandType};
const GIT_DIR: &str = ".rgit"; const GIT_DIR: &str = ".rgit";
@@ -17,8 +17,5 @@ struct CmdArgs {
fn main() { fn main() {
let args = CmdArgs::parse(); let args = CmdArgs::parse();
match args.cmd.run() { if let Err(str) = args.cmd.run() { eprintln!("Some error occured: {str}") }
Err(str) => println!("Some error occured: {str}"),
Ok(str) => println!("{str}"),
}
} }
+6 -5
View File
@@ -7,17 +7,18 @@ use crate::{
subcommands::hash_object::HashObjectSubcommand, subcommands::hash_object::HashObjectSubcommand,
}; };
use super::Subcommand; use super::GitSubcommand;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct AddSubcommand { pub struct AddSubcommand {
pub paths: Vec<String>, pub paths: Vec<String>,
} }
impl Subcommand for AddSubcommand { impl GitSubcommand for AddSubcommand {
fn run(&self) -> Result<String> { fn run(&self) -> Result<()> {
if self.paths.is_empty() { 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()?; let mut index = Index::load()?;
@@ -38,6 +39,6 @@ impl Subcommand for AddSubcommand {
index.save()?; index.save()?;
Ok(String::from("")) Ok(())
} }
} }
+5 -4
View File
@@ -4,7 +4,7 @@ use hex::encode;
use crate::{ use crate::{
git_fs::object::{Blob, GitObject}, git_fs::object::{Blob, GitObject},
subcommands::Subcommand, subcommands::GitSubcommand,
}; };
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@@ -30,9 +30,10 @@ impl HashObjectSubcommand {
} }
} }
impl Subcommand for HashObjectSubcommand { impl GitSubcommand for HashObjectSubcommand {
fn run(&self) -> Result<String> { fn run(&self) -> Result<()> {
let hash = self.run_raw()?; let hash = self.run_raw()?;
Ok(encode(hash)) println!("{}", encode(hash));
Ok(())
} }
} }
+10 -8
View File
@@ -5,15 +5,15 @@ use std::{fs, path::Path};
use crate::GIT_DIR; use crate::GIT_DIR;
use crate::git_fs::head::Head; use crate::git_fs::head::Head;
use crate::subcommands::Subcommand; use crate::subcommands::GitSubcommand;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct InitSubcommand { pub struct InitSubcommand {
directory: Option<String>, directory: Option<String>,
} }
impl Subcommand for InitSubcommand { impl GitSubcommand for InitSubcommand {
fn run(&self) -> Result<String> { fn run(&self) -> Result<()> {
let path = match &self.directory { let path = match &self.directory {
None => Path::new("."), None => Path::new("."),
Some(path) => Path::new(path), Some(path) => Path::new(path),
@@ -40,15 +40,17 @@ impl Subcommand for InitSubcommand {
let canonical_path = path.canonicalize(); let canonical_path = path.canonicalize();
if new_repo { if new_repo {
Ok(format!( println!(
"Reinitialized exisiting Git repo in {}", "Reinitialized exisiting Git repo in {}",
canonical_path?.display() canonical_path?.display()
)) );
} else { } else {
Ok(format!( println!(
"Initialized empty Git repo in {}", "Initialized empty Git repo in {}",
canonical_path?.display() canonical_path?.display()
)) );
} };
Ok(())
} }
} }
+8 -5
View File
@@ -3,6 +3,7 @@ use crate::subcommands::{
test::TestSubcommand, test::TestSubcommand,
}; };
use anyhow::Result; use anyhow::Result;
use clap::Subcommand;
mod add; mod add;
mod hash_object; mod hash_object;
@@ -10,7 +11,7 @@ mod init;
mod rm; mod rm;
mod test; mod test;
#[derive(clap::Parser, Debug)] #[derive(Debug, Subcommand)]
pub enum SubcommandType { pub enum SubcommandType {
/// Add file(s) to index /// Add file(s) to index
Add(AddSubcommand), Add(AddSubcommand),
@@ -18,16 +19,18 @@ pub enum SubcommandType {
Rm(RmSubcommand), Rm(RmSubcommand),
/// Init a Git repository /// Init a Git repository
Init(InitSubcommand), Init(InitSubcommand),
#[clap(hide = true)]
HashObject(HashObjectSubcommand), HashObject(HashObjectSubcommand),
#[clap(hide = true)]
Test(TestSubcommand), Test(TestSubcommand),
} }
pub trait Subcommand { pub trait GitSubcommand {
fn run(&self) -> Result<String>; fn run(&self) -> Result<()>;
} }
impl Subcommand for SubcommandType { impl GitSubcommand for SubcommandType {
fn run(&self) -> Result<String> { fn run(&self) -> Result<()> {
match self { match self {
Self::Add(cmd) => cmd.run(), Self::Add(cmd) => cmd.run(),
Self::Rm(cmd) => cmd.run(), Self::Rm(cmd) => cmd.run(),
+6 -5
View File
@@ -4,7 +4,7 @@ use std::path::Path;
use crate::git_fs::{gitignore::expands_and_filter_path, index::Index}; use crate::git_fs::{gitignore::expands_and_filter_path, index::Index};
use super::Subcommand; use super::GitSubcommand;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct RmSubcommand { pub struct RmSubcommand {
@@ -15,10 +15,11 @@ pub struct RmSubcommand {
pub paths: Vec<String>, pub paths: Vec<String>,
} }
impl Subcommand for RmSubcommand { impl GitSubcommand for RmSubcommand {
fn run(&self) -> Result<String> { fn run(&self) -> Result<()> {
if self.paths.is_empty() { 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()?; let mut index = Index::load()?;
@@ -32,6 +33,6 @@ impl Subcommand for RmSubcommand {
index.save()?; index.save()?;
Ok(String::from("")) Ok(())
} }
} }