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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
use std::{
fmt::{Display, Formatter, Result as FmtResult},
process::Command,
};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
pub fn process_args(mut args: Vec<String>) -> Result<(), GiError> {
args.remove(0);
let original = args.join(" ");
let args = transform_args(args)?;
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Green)))
.map_err(|_| GiError::StdoutSet)?;
println!(
"Correcting `gi {}` to `git{}{}`...",
original,
if args.is_empty() { "" } else { " " },
args.join(" ")
);
stdout.reset().map_err(|_| GiError::StdoutReset)?;
println!();
Command::new("git")
.args(&args)
.status()
.map_err(|_| GiError::GitFail)?;
Ok(())
}
pub fn transform_args(mut args: Vec<String>) -> GiResult<Vec<String>> {
if args.is_empty() {
return Err(GiError::NoArgs);
}
let first = &mut args[0];
if !first.starts_with('t') {
return Err(GiError::BadPrefix);
}
first.remove(0);
if first.is_empty() {
args.remove(0);
}
Ok(args)
}
type GiResult<T> = Result<T, GiError>;
#[derive(Debug, Clone, PartialEq)]
pub enum GiError {
BadPrefix,
GitFail,
NoArgs,
StdoutReset,
StdoutSet,
}
impl GiError {
pub fn print(self) {
StandardStream::stderr(ColorChoice::Always)
.set_color(ColorSpec::new().set_fg(Some(Color::Red)))
.expect("Unable to set stderr color!");
println!("{}", self)
}
}
impl Display for GiError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(
f,
"{}",
match &self {
Self::BadPrefix => "Argument does not start with a `t`!",
Self::GitFail => "Failed to run git!",
Self::NoArgs => "No arguments provided!",
Self::StdoutReset => "Unable to reset stdout color!",
Self::StdoutSet => "Unable to set stdout color!,",
}
)
}
}
#[cfg(test)]
mod tests {
use crate::{transform_args, GiError};
#[test]
fn no_args() {
assert_eq!(transform_args(vec![]), Err(GiError::NoArgs));
}
#[test]
fn no_t() {
assert_eq!(transform_args(vec!["foo".into()]), Err(GiError::BadPrefix));
}
#[test]
fn only_t() {
assert_eq!(transform_args(vec!["t".into()]), Ok(vec![]));
}
#[test]
fn simple_valid() {
assert_eq!(
transform_args(vec!["tstatus".into()]),
Ok(vec!["status".into()])
);
}
#[test]
fn complex_valid() {
assert_eq!(
transform_args(vec![
"tconfig".into(),
"--global".into(),
"name".into(),
"Firstname Lastname".into()
]),
Ok(vec![
"config".into(),
"--global".into(),
"name".into(),
"Firstname Lastname".into()
])
);
}
}