todo_file/
edit_content.rs

1use super::action::Action;
2
3/// Describes a edit context for modifying a line.
4#[derive(Debug)]
5pub struct EditContext {
6	action: Option<Action>,
7	content: Option<String>,
8	option: Option<String>,
9}
10
11impl EditContext {
12	/// Create a new empty instance.
13	#[must_use]
14	#[inline]
15	pub const fn new() -> Self {
16		Self {
17			action: None,
18			content: None,
19			option: None,
20		}
21	}
22
23	/// Set the action.
24	#[must_use]
25	#[inline]
26	pub const fn action(mut self, action: Action) -> Self {
27		self.action = Some(action);
28		self
29	}
30
31	/// Set the content.
32	#[must_use]
33	#[inline]
34	pub fn content(mut self, content: &str) -> Self {
35		self.content = Some(String::from(content));
36		self
37	}
38
39	/// Set the option.
40	#[must_use]
41	#[inline]
42	pub fn option(mut self, option: &str) -> Self {
43		self.option = Some(String::from(option));
44		self
45	}
46
47	/// Get the action.
48	#[must_use]
49	#[inline]
50	pub const fn get_action(&self) -> Option<Action> {
51		self.action
52	}
53
54	/// Get the content.
55	#[must_use]
56	#[inline]
57	pub fn get_content(&self) -> Option<&str> {
58		self.content.as_deref()
59	}
60
61	/// Get the option.
62	#[must_use]
63	#[inline]
64	pub fn get_option(&self) -> Option<&str> {
65		self.option.as_deref()
66	}
67}
68
69#[cfg(test)]
70mod tests {
71	use claim::{assert_none, assert_some_eq};
72
73	use super::*;
74
75	#[test]
76	fn empty() {
77		let edit_context = EditContext::new();
78		assert_none!(edit_context.get_action());
79		assert_none!(edit_context.get_content());
80		assert_none!(edit_context.get_option());
81	}
82
83	#[test]
84	fn with_action() {
85		let edit_context = EditContext::new().action(Action::Break);
86		assert_some_eq!(edit_context.get_action(), Action::Break);
87		assert_none!(edit_context.get_content());
88		assert_none!(edit_context.get_option());
89	}
90
91	#[test]
92	fn with_content() {
93		let edit_context = EditContext::new().content("test content");
94		assert_none!(edit_context.get_action());
95		assert_some_eq!(edit_context.get_content(), "test content");
96		assert_none!(edit_context.get_option());
97	}
98
99	#[test]
100	fn with_option() {
101		let edit_context = EditContext::new().option("-C");
102		assert_none!(edit_context.get_action());
103		assert_none!(edit_context.get_content());
104		assert_some_eq!(edit_context.get_option(), "-C");
105	}
106
107	#[test]
108	fn with_all() {
109		let edit_context = EditContext::new()
110			.action(Action::Edit)
111			.content("test content")
112			.option("-C");
113		assert_some_eq!(edit_context.get_action(), Action::Edit);
114		assert_some_eq!(edit_context.get_content(), "test content");
115		assert_some_eq!(edit_context.get_option(), "-C");
116	}
117}