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
#![warn(missing_docs)]
use args::{Args, RunnerArgs};
use std::{fmt, process};
mod args;
pub mod builder;
pub mod config;
mod help;
mod subcommand;
enum Command {
NoSubcommand,
Build(Args),
Run(Args),
Test(Args),
Runner(RunnerArgs),
Help,
BuildHelp,
RunHelp,
TestHelp,
CargoBootimageHelp,
RunnerHelp,
Version,
}
pub fn lib_main() {
match run() {
Err(err) => {
eprintln!("Error: {}", err.message);
process::exit(1);
}
Ok(Some(exit_code)) => {
process::exit(exit_code);
}
Ok(None) => {}
}
}
pub fn run() -> Result<Option<i32>, ErrorMessage> {
let command = args::parse_args()?;
let none = |()| None;
match command {
Command::Build(args) => subcommand::build::build(args).map(none),
Command::Run(args) => subcommand::run::run(args).map(Some),
Command::Test(args) => subcommand::test::test(args).map(none),
Command::Runner(args) => subcommand::runner::runner(args).map(Some),
Command::Help => Ok(help::help()).map(none),
Command::BuildHelp => Ok(help::build_help()).map(none),
Command::CargoBootimageHelp => Ok(help::cargo_bootimage_help()).map(none),
Command::RunHelp => Ok(help::run_help()).map(none),
Command::RunnerHelp => Ok(help::runner_help()).map(none),
Command::TestHelp => Ok(help::test_help()).map(none),
Command::Version => Ok(println!("bootimage {}", env!("CARGO_PKG_VERSION"))).map(none),
Command::NoSubcommand => Err(help::no_subcommand()),
}
}
pub struct ErrorMessage {
pub message: Box<dyn fmt::Display + Send>,
}
impl fmt::Debug for ErrorMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.message.fmt(f)
}
}
impl<T> From<T> for ErrorMessage
where
T: fmt::Display + Send + 'static,
{
fn from(err: T) -> Self {
ErrorMessage {
message: Box::new(err),
}
}
}