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
use super::action::Action;

/// Describes a edit context for modifying a line.
#[derive(Debug)]
pub struct EditContext {
	action: Option<Action>,
	content: Option<String>,
	option: Option<String>,
}

impl EditContext {
	/// Create a new empty instance.
	#[must_use]
	#[inline]
	pub const fn new() -> Self {
		Self {
			action: None,
			content: None,
			option: None,
		}
	}

	/// Set the action.
	#[must_use]
	#[inline]
	pub const fn action(mut self, action: Action) -> Self {
		self.action = Some(action);
		self
	}

	/// Set the content.
	#[must_use]
	#[inline]
	pub fn content(mut self, content: &str) -> Self {
		self.content = Some(String::from(content));
		self
	}

	/// Set the option.
	#[must_use]
	#[inline]
	pub fn option(mut self, option: &str) -> Self {
		self.option = Some(String::from(option));
		self
	}

	/// Get the action.
	#[must_use]
	#[inline]
	pub const fn get_action(&self) -> Option<Action> {
		self.action
	}

	/// Get the content.
	#[must_use]
	#[inline]
	pub fn get_content(&self) -> Option<&str> {
		self.content.as_deref()
	}

	/// Get the option.
	#[must_use]
	#[inline]
	pub fn get_option(&self) -> Option<&str> {
		self.option.as_deref()
	}
}

#[cfg(test)]
mod tests {
	use claim::{assert_none, assert_some_eq};

	use super::*;

	#[test]
	fn empty() {
		let edit_context = EditContext::new();
		assert_none!(edit_context.get_action());
		assert_none!(edit_context.get_content());
		assert_none!(edit_context.get_option());
	}

	#[test]
	fn with_action() {
		let edit_context = EditContext::new().action(Action::Break);
		assert_some_eq!(edit_context.get_action(), Action::Break);
		assert_none!(edit_context.get_content());
		assert_none!(edit_context.get_option());
	}

	#[test]
	fn with_content() {
		let edit_context = EditContext::new().content("test content");
		assert_none!(edit_context.get_action());
		assert_some_eq!(edit_context.get_content(), "test content");
		assert_none!(edit_context.get_option());
	}

	#[test]
	fn with_option() {
		let edit_context = EditContext::new().option("-C");
		assert_none!(edit_context.get_action());
		assert_none!(edit_context.get_content());
		assert_some_eq!(edit_context.get_option(), "-C");
	}

	#[test]
	fn with_all() {
		let edit_context = EditContext::new()
			.action(Action::Edit)
			.content("test content")
			.option("-C");
		assert_some_eq!(edit_context.get_action(), Action::Edit);
		assert_some_eq!(edit_context.get_content(), "test content");
		assert_some_eq!(edit_context.get_option(), "-C");
	}
}