Compare commits

..

1 Commits

Author SHA1 Message Date
Antonin Ruan 72d0d5f1e2 refactor subcommand behavior 2026-03-12 20:32:55 +01:00
6 changed files with 37 additions and 32 deletions
+2 -5
View File
@@ -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}") }
}
+6 -5
View File
@@ -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<String>,
}
impl Subcommand for AddSubcommand {
fn run(&self) -> Result<String> {
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(())
}
}
+5 -4
View File
@@ -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<String> {
impl GitSubcommand for HashObjectSubcommand {
fn run(&self) -> Result<()> {
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_fs::head::Head;
use crate::subcommands::Subcommand;
use crate::subcommands::GitSubcommand;
#[derive(Parser, Debug)]
pub struct InitSubcommand {
directory: Option<String>,
}
impl Subcommand for InitSubcommand {
fn run(&self) -> Result<String> {
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(())
}
}
+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(),
+6 -5
View File
@@ -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<String>,
}
impl Subcommand for RmSubcommand {
fn run(&self) -> Result<String> {
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(())
}
}