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
use crate::ErrorMessage;
use std::path::Path;
use toml::Value;
#[derive(Debug, Clone)]
pub struct Config {
pub default_target: Option<String>,
pub run_command: Vec<String>,
pub run_args: Option<Vec<String>>,
pub test_args: Option<Vec<String>>,
pub test_timeout: u32,
pub test_success_exit_code: Option<i32>,
non_exhaustive: (),
}
pub(crate) fn read_config(manifest_path: &Path) -> Result<Config, ErrorMessage> {
let config = read_config_inner(manifest_path)
.map_err(|err| format!("Failed to read bootimage configuration: {:?}", err))?;
Ok(config)
}
pub(crate) fn read_config_inner(manifest_path: &Path) -> Result<Config, ErrorMessage> {
use std::{fs::File, io::Read};
let cargo_toml: Value = {
let mut content = String::new();
File::open(manifest_path)
.map_err(|e| format!("Failed to open Cargo.toml: {}", e))?
.read_to_string(&mut content)
.map_err(|e| format!("Failed to read Cargo.toml: {}", e))?;
content
.parse::<Value>()
.map_err(|e| format!("Failed to parse Cargo.toml: {}", e))?
};
let metadata = cargo_toml
.get("package")
.and_then(|table| table.get("metadata"))
.and_then(|table| table.get("bootimage"));
let metadata = match metadata {
None => {
return Ok(ConfigBuilder::default().into());
}
Some(metadata) => metadata
.as_table()
.ok_or(format!("Bootimage configuration invalid: {:?}", metadata))?,
};
let mut config = ConfigBuilder::default();
for (key, value) in metadata {
match (key.as_str(), value.clone()) {
("default-target", Value::String(s)) => config.default_target = From::from(s),
("test-timeout", Value::Integer(timeout)) if timeout.is_negative() => {
Err(format!("test-timeout must not be negative"))?
}
("test-timeout", Value::Integer(timeout)) => {
config.test_timeout = Some(timeout as u32);
}
("test-success-exit-code", Value::Integer(exit_code)) => {
config.test_success_exit_code = Some(exit_code as i32);
}
("run-command", Value::Array(array)) => {
let mut command = Vec::new();
for value in array {
match value {
Value::String(s) => command.push(s),
_ => Err(format!("run-command must be a list of strings"))?,
}
}
config.run_command = Some(command);
}
("run-args", Value::Array(array)) => {
let mut args = Vec::new();
for value in array {
match value {
Value::String(s) => args.push(s),
_ => Err(format!("run-args must be a list of strings"))?,
}
}
config.run_args = Some(args);
}
("test-args", Value::Array(array)) => {
let mut args = Vec::new();
for value in array {
match value {
Value::String(s) => args.push(s),
_ => Err(format!("test-args must be a list of strings"))?,
}
}
config.test_args = Some(args);
}
(key, value) => Err(format!(
"unexpected `package.metadata.bootimage` \
key `{}` with value `{}`",
key, value
))?,
}
}
Ok(config.into())
}
#[derive(Default)]
struct ConfigBuilder {
default_target: Option<String>,
run_command: Option<Vec<String>>,
run_args: Option<Vec<String>>,
test_args: Option<Vec<String>>,
test_timeout: Option<u32>,
test_success_exit_code: Option<i32>,
}
impl Into<Config> for ConfigBuilder {
fn into(self) -> Config {
Config {
default_target: self.default_target,
run_command: self.run_command.unwrap_or(vec![
"qemu-system-x86_64".into(),
"-drive".into(),
"format=raw,file={}".into(),
]),
run_args: self.run_args,
test_args: self.test_args,
test_timeout: self.test_timeout.unwrap_or(60 * 5),
test_success_exit_code: self.test_success_exit_code,
non_exhaustive: (),
}
}
}