summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 5e33302ee3034762bc36e429fa556534b7ab1e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub mod git;
pub mod parser;

use parser::*;
use rustyline::error::ReadlineError;
use std::{io, ops::ControlFlow};
use thiserror::Error;

pub fn run_command(user_input: String) -> Result<ControlFlow<(), ()>, ShackleError> {
    match user_input.parse::<ShackleCommand>() {
        Err(parse_error) => {
            println!("{}", parse_error);
        }
        Ok(ShackleCommand::Whitespace) => {}
        Ok(ShackleCommand::Exit) => {
            return Ok(ControlFlow::Break(()));
        }
        Ok(ShackleCommand::GitInit(GitInitArgs { repo_name })) => {
            let init_result = git::init(&repo_name)?;
            println!("Successfully created \"{}\"", init_result.path.display());
        }
        Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => {
            git::upload_pack(&upload_pack_args)?;
        }
        Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => {
            git::receive_pack(&receive_pack_args)?;
        }
    }
    Ok(ControlFlow::Continue(()))
}

#[derive(Error, Debug)]
pub enum ShackleError {
    #[error(transparent)]
    IoError(#[from] io::Error),
    #[error(transparent)]
    GitError(#[from] git2::Error),
    #[error(transparent)]
    ReadlineError(#[from] ReadlineError),
    #[error("Could not get the current user name")]
    UserReadError,
    #[error("Path is not accessible")]
    InvalidDirectory,
    #[error("`{0}`")]
    CustomError(String),
}