summaryrefslogtreecommitdiffstats
path: root/src/module/modules.rs
blob: e29aaeeb1ef8513bee9680218112eb0b83a92d52 (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
use std::sync::Arc;

use parking_lot::Mutex;

use crate::{
	config::Config,
	git::Repository,
	module::{Module, ModuleProvider, State},
	modules::{ConfirmAbort, ConfirmRebase, Error, ExternalEditor, Insert, List, ShowCommit, WindowSizeError},
	todo_file::TodoFile,
};

pub(crate) struct Modules {
	confirm_abort: ConfirmAbort,
	confirm_rebase: ConfirmRebase,
	error: Error,
	external_editor: ExternalEditor,
	insert: Insert,
	list: List,
	show_commit: ShowCommit,
	window_size_error: WindowSizeError,
}

impl ModuleProvider for Modules {
	fn new(config: &Config, repository: Repository, todo_file: &Arc<Mutex<TodoFile>>) -> Self {
		Self {
			error: Error::new(),
			list: List::new(config, Arc::clone(todo_file)),
			show_commit: ShowCommit::new(config, repository, Arc::clone(todo_file)),
			window_size_error: WindowSizeError::new(),
			confirm_abort: ConfirmAbort::new(
				&config.key_bindings.confirm_yes,
				&config.key_bindings.confirm_no,
				Arc::clone(todo_file),
			),
			confirm_rebase: ConfirmRebase::new(&config.key_bindings.confirm_yes, &config.key_bindings.confirm_no),
			external_editor: ExternalEditor::new(config.git.editor.as_str(), Arc::clone(todo_file)),
			insert: Insert::new(Arc::clone(todo_file)),
		}
	}

	fn get_mut_module(&mut self, state: State) -> &mut dyn Module {
		match state {
			State::ConfirmAbort => &mut self.confirm_abort,
			State::ConfirmRebase => &mut self.confirm_rebase,
			State::Error => &mut self.error,
			State::ExternalEditor => &mut self.external_editor,
			State::Insert => &mut self.insert,
			State::List => &mut self.list,
			State::ShowCommit => &mut self.show_commit,
			State::WindowSizeError => &mut self.window_size_error,
		}
	}

	fn get_module(&self, state: State) -> &dyn Module {
		match state {
			State::ConfirmAbort => &self.confirm_abort,
			State::ConfirmRebase => &self.confirm_rebase,
			State::Error => &self.error,
			State::ExternalEditor => &self.external_editor,
			State::Insert => &self.insert,
			State::List => &self.list,
			State::ShowCommit => &self.show_commit,
			State::WindowSizeError => &self.window_size_error,
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::test_helpers::{with_temp_repository, with_todo_file};

	pub(crate) fn modules_test<C>(callback: C)
	where C: FnOnce(Modules) {
		with_temp_repository(|repository| {
			with_todo_file(&[], |todo_file_context| {
				let (_todo_file_path, todo_file) = todo_file_context.to_owned();
				let config = Config::new();
				let modules = Modules::new(&config, repository, &Arc::new(Mutex::new(todo_file)));
				callback(modules);
			});
		});
	}

	// someday I would like to test the returned types for these
	#[test]
	fn get_mut_module() {
		modules_test(|mut modules| {
			_ = modules.get_mut_module(State::ConfirmAbort);
			_ = modules.get_mut_module(State::ConfirmRebase);
			_ = modules.get_mut_module(State::Error);
			_ = modules.get_mut_module(State::ExternalEditor);
			_ = modules.get_mut_module(State::Insert);
			_ = modules.get_mut_module(State::List);
			_ = modules.get_mut_module(State::ShowCommit);
			_ = modules.get_mut_module(State::WindowSizeError);
		});
	}

	#[test]
	fn get_module() {
		modules_test(|modules| {
			_ = modules.get_module(State::ConfirmAbort);
			_ = modules.get_module(State::ConfirmRebase);
			_ = modules.get_module(State::Error);
			_ = modules.get_module(State::ExternalEditor);
			_ = modules.get_module(State::Insert);
			_ = modules.get_module(State::List);
			_ = modules.get_module(State::ShowCommit);
			_ = modules.get_module(State::WindowSizeError);
		});
	}
}