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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::fmt::{Display, Formatter};

use crate::errors::ParseError;

/// Describes an rebase action.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)]
pub enum Action {
	/// A break action.
	Break,
	/// A drop action.
	Drop,
	/// An edit action.
	Edit,
	/// An exec action.
	Exec,
	/// A fixup action.
	Fixup,
	/// A noop action.
	Noop,
	/// A pick action.
	Pick,
	/// A reword action.
	Reword,
	/// A squash action.
	Squash,
	/// A label for a merge block.
	Label,
	/// A reset for a merge block.
	Reset,
	/// A merge action.
	Merge,
	/// Update a reference
	UpdateRef,
}

impl Action {
	/// Get the abbreviated version of the action.
	#[must_use]
	#[inline]
	pub fn to_abbreviation(self) -> String {
		String::from(match self {
			Self::Break => "b",
			Self::Drop => "d",
			Self::Edit => "e",
			Self::Exec => "x",
			Self::Fixup => "f",
			Self::Label => "l",
			Self::Merge => "m",
			Self::Noop => "n",
			Self::Pick => "p",
			Self::Reset => "t",
			Self::Reword => "r",
			Self::Squash => "s",
			Self::UpdateRef => "u",
		})
	}

	/// Can the action be changed.
	#[must_use]
	#[inline]
	pub const fn is_static(self) -> bool {
		match self {
			Self::Break | Self::Exec | Self::Noop | Self::Reset | Self::Label | Self::Merge | Self::UpdateRef => true,
			Self::Drop | Self::Edit | Self::Fixup | Self::Pick | Self::Reword | Self::Squash => false,
		}
	}
}

impl Display for Action {
	#[inline]
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", match *self {
			Self::Break => "break",
			Self::Drop => "drop",
			Self::Edit => "edit",
			Self::Exec => "exec",
			Self::Fixup => "fixup",
			Self::Label => "label",
			Self::Merge => "merge",
			Self::Noop => "noop",
			Self::Pick => "pick",
			Self::Reset => "reset",
			Self::Reword => "reword",
			Self::Squash => "squash",
			Self::UpdateRef => "update-ref",
		})
	}
}

impl TryFrom<&str> for Action {
	type Error = ParseError;

	#[inline]
	fn try_from(s: &str) -> Result<Self, Self::Error> {
		match s {
			"break" | "b" => Ok(Self::Break),
			"drop" | "d" => Ok(Self::Drop),
			"edit" | "e" => Ok(Self::Edit),
			"exec" | "x" => Ok(Self::Exec),
			"fixup" | "f" => Ok(Self::Fixup),
			"noop" | "n" => Ok(Self::Noop),
			"pick" | "p" => Ok(Self::Pick),
			"reword" | "r" => Ok(Self::Reword),
			"squash" | "s" => Ok(Self::Squash),
			"label" | "l" => Ok(Self::Label),
			"reset" | "t" => Ok(Self::Reset),
			"merge" | "m" => Ok(Self::Merge),
			"update-ref" | "u" => Ok(Self::UpdateRef),
			_ => Err(ParseError::InvalidAction(String::from(s))),
		}
	}
}

#[cfg(test)]
mod tests {
	use claim::assert_ok_eq;
	use rstest::rstest;
	use testutils::assert_err_eq;

	use super::*;

	#[rstest]
	#[case::break_str(Action::Break, "break")]
	#[case::drop(Action::Drop, "drop")]
	#[case::edit(Action::Edit, "edit")]
	#[case::exec(Action::Exec, "exec")]
	#[case::fixup(Action::Fixup, "fixup")]
	#[case::noop(Action::Noop, "noop")]
	#[case::pick(Action::Pick, "pick")]
	#[case::reword(Action::Reword, "reword")]
	#[case::squash(Action::Squash, "squash")]
	#[case::label(Action::Label, "label")]
	#[case::reset(Action::Reset, "reset")]
	#[case::merge(Action::Merge, "merge")]
	#[case::update_ref(Action::UpdateRef, "update-ref")]
	fn to_string(#[case] action: Action, #[case] expected: &str) {
		assert_eq!(format!("{action}"), expected);
	}

	#[rstest]
	#[case::b("b", Action::Break)]
	#[case::break_str("break", Action::Break)]
	#[case::d("d", Action::Drop)]
	#[case::drop("drop", Action::Drop)]
	#[case::e("e", Action::Edit)]
	#[case::edit("edit", Action::Edit)]
	#[case::x("x", Action::Exec)]
	#[case::exec("exec", Action::Exec)]
	#[case::f("f", Action::Fixup)]
	#[case::fixup("fixup", Action::Fixup)]
	#[case::n("n", Action::Noop)]
	#[case::noop("noop", Action::Noop)]
	#[case::p("p", Action::Pick)]
	#[case::pick("pick", Action::Pick)]
	#[case::r("r", Action::Reword)]
	#[case::reword("reword", Action::Reword)]
	#[case::s("s", Action::Squash)]
	#[case::squash("squash", Action::Squash)]
	#[case::l("l", Action::Label)]
	#[case::label("label", Action::Label)]
	#[case::t("t", Action::Reset)]
	#[case::reset("reset", Action::Reset)]
	#[case::m("m", Action::Merge)]
	#[case::merge("merge", Action::Merge)]
	#[case::u("u", Action::UpdateRef)]
	#[case::update_ref("update-ref", Action::UpdateRef)]
	fn try_from(#[case] action_str: &str, #[case] expected: Action) {
		assert_ok_eq!(Action::try_from(action_str), expected);
	}

	#[test]
	fn action_try_from_invalid() {
		let invalid = String::from("invalid");
		assert_err_eq!(Action::try_from(invalid.as_str()), ParseError::InvalidAction(invalid));
	}

	#[rstest]
	#[case::b(Action::Break, "b")]
	#[case::d(Action::Drop, "d")]
	#[case::e(Action::Edit, "e")]
	#[case::x(Action::Exec, "x")]
	#[case::f(Action::Fixup, "f")]
	#[case::n(Action::Noop, "n")]
	#[case::p(Action::Pick, "p")]
	#[case::r(Action::Reword, "r")]
	#[case::s(Action::Squash, "s")]
	#[case::l(Action::Label, "l")]
	#[case::t(Action::Reset, "t")]
	#[case::m(Action::Merge, "m")]
	#[case::u(Action::UpdateRef, "u")]
	fn to_abbreviation(#[case] action: Action, #[case] expected: &str) {
		assert_eq!(action.to_abbreviation(), expected);
	}

	#[rstest]
	#[case::break_action(Action::Break, true)]
	#[case::drop(Action::Drop, false)]
	#[case::edit(Action::Edit, false)]
	#[case::exec(Action::Exec, true)]
	#[case::fixup(Action::Fixup, false)]
	#[case::noop(Action::Noop, true)]
	#[case::pick(Action::Pick, false)]
	#[case::reword(Action::Reword, false)]
	#[case::squash(Action::Squash, false)]
	#[case::label(Action::Label, true)]
	#[case::reset(Action::Reset, true)]
	#[case::merge(Action::Merge, true)]
	#[case::update_ref(Action::UpdateRef, true)]
	fn module_lifecycle(#[case] action: Action, #[case] expected: bool) {
		assert_eq!(action.is_static(), expected);
	}
}