summaryrefslogtreecommitdiffstats
path: root/src/process/process_result.rs
blob: c45be700cc79451665c03819e4c5ec739c806486 (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
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
use anyhow::Error;

use crate::{
	input::Event,
	process::{ExitStatus, State},
};

#[derive(Debug)]
pub struct ProcessResult {
	pub(super) error: Option<Error>,
	pub(super) exit_status: Option<ExitStatus>,
	pub(super) event: Option<Event>,
	pub(super) state: Option<State>,
	pub(super) external_command: Option<(String, Vec<String>)>,
}

impl ProcessResult {
	pub(crate) const fn new() -> Self {
		Self {
			error: None,
			exit_status: None,
			event: None,
			state: None,
			external_command: None,
		}
	}

	pub(crate) const fn event(mut self, event: Event) -> Self {
		self.event = Some(event);
		self
	}

	pub(crate) fn error(mut self, error: Error) -> Self {
		self.error = Some(error);
		self
	}

	pub(crate) const fn exit_status(mut self, status: ExitStatus) -> Self {
		self.exit_status = Some(status);
		self
	}

	#[allow(clippy::missing_const_for_fn)] // false positive
	pub(crate) fn state(mut self, new_state: State) -> Self {
		self.state = Some(new_state);
		self
	}

	pub(crate) fn external_command(mut self, command: String, arguments: Vec<String>) -> Self {
		self.external_command = Some((command, arguments));
		self
	}
}

impl From<Event> for ProcessResult {
	fn from(event: Event) -> Self {
		Self {
			error: None,
			exit_status: None,
			event: Some(event),
			state: None,
			external_command: None,
		}
	}
}

#[cfg(test)]
mod tests {
	use anyhow::anyhow;

	use super::*;

	#[test]
	fn empty() {
		let result = ProcessResult::new();
		assert!(result.error.is_none());
		assert_eq!(result.exit_status, None);
		assert_eq!(result.event, None);
		assert_eq!(result.state, None);
		assert_eq!(result.external_command, None);
	}

	#[test]
	fn with_event() {
		let result = ProcessResult::new().event(Event::from('a'));
		assert!(result.error.is_none());
		assert_eq!(result.exit_status, None);
		assert_eq!(result.event, Some(Event::from('a')));
		assert_eq!(result.state, None);
		assert_eq!(result.external_command, None);
	}

	#[test]
	fn with_error() {
		let result = ProcessResult::new().error(anyhow!("Test Error"));
		assert_eq!(result.error.unwrap().to_string(), String::from("Test Error"));
		assert_eq!(result.exit_status, None);
		assert_eq!(result.event, None);
		assert_eq!(result.state, None);
		assert_eq!(result.external_command, None);
	}

	#[test]
	fn exit_status() {
		let result = ProcessResult::new().exit_status(ExitStatus::Good);
		assert!(result.error.is_none());
		assert_eq!(result.exit_status, Some(ExitStatus::Good));
		assert_eq!(result.event, None);
		assert_eq!(result.state, None);
		assert_eq!(result.external_command, None);
	}

	#[test]
	fn state() {
		let result = ProcessResult::new().state(State::List);
		assert!(result.error.is_none());
		assert_eq!(result.exit_status, None);
		assert_eq!(result.event, None);
		assert_eq!(result.state, Some(State::List));
		assert_eq!(result.external_command, None);
	}

	#[test]
	fn external_command() {
		let result = ProcessResult::new()
			.external_command(String::from("editor"), vec![String::from("arg1"), String::from("arg2")]);
		assert!(result.error.is_none());
		assert_eq!(result.exit_status, None);
		assert_eq!(result.event, None);
		assert_eq!(result.state, None);
		assert_eq!(
			result.external_command,
			Some((String::from("editor"), vec![String::from("arg1"), String::from("arg2")]))
		);
	}

	#[test]
	fn everything() {
		let result = ProcessResult::new()
			.error(anyhow!("Test Error"))
			.state(State::List)
			.exit_status(ExitStatus::Good)
			.event(Event::from('a'))
			.external_command(String::from("editor"), vec![String::from("arg1"), String::from("arg2")]);
		assert_eq!(result.error.unwrap().to_string(), String::from("Test Error"));
		assert_eq!(result.exit_status, Some(ExitStatus::Good));
		assert_eq!(result.event, Some(Event::from('a')));
		assert_eq!(result.state, Some(State::List));
		assert_eq!(
			result.external_command,
			Some((String::from("editor"), vec![String::from("arg1"), String::from("arg2")]))
		);
	}
}