summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: d88e12f0664cd0b87d4fefdb2eb34567de6ca358 (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
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy))]

mod action;
mod application;
mod cli;
mod color;
mod commit;
mod config;
mod constants;
mod exit_status;
mod git_interactive;
mod input;
mod line;
mod scroll;
mod view;
mod window;

use crate::application::Application;
use crate::config::Config;
use crate::exit_status::ExitStatus;
use crate::git_interactive::GitInteractive;
use crate::input::InputHandler;
use crate::view::View;
use crate::window::Window;
use std::process;

struct Exit {
	message: String,
	status: ExitStatus,
}

fn main() {
	match try_main() {
		Ok(code) => process::exit(code.to_code()),
		Err(err) => {
			eprintln!("{}", err.message);
			process::exit(err.status.to_code());
		},
	}
}

fn try_main() -> Result<ExitStatus, Exit> {
	let matches = cli::build_cli().get_matches();

	let filepath = matches.value_of("rebase-todo-filepath").unwrap();

	let config = match Config::new() {
		Ok(c) => c,
		Err(message) => {
			return Err(Exit {
				message,
				status: ExitStatus::ConfigError,
			});
		},
	};

	let git_interactive = match GitInteractive::new_from_filepath(filepath, config.comment_char.as_str()) {
		Ok(gi) => gi,
		Err(message) => {
			return Err(Exit {
				message,
				status: ExitStatus::FileReadError,
			});
		},
	};

	if git_interactive.get_lines().is_empty() {
		return Err(Exit {
			message: String::from("Nothing to rebase"),
			status: ExitStatus::FileReadError,
		});
	}

	let window = Window::new(&config);

	let input_handler = InputHandler::new(&window);

	let mut application = Application::new(git_interactive, View::new(&window), &input_handler, &config);

	let result = application.run();
	window.end();

	let exit_code = match result {
		Ok(c) => c,
		Err(message) => {
			return Err(Exit {
				message,
				status: ExitStatus::FileWriteError,
			});
		},
	};

	Ok(exit_code.unwrap_or(ExitStatus::Good))
}