1use toml;
17use std::collections;
18use std::fs;
19
20#[derive(Clone, Debug)]
21pub struct Config {
22 pub data_dir: String,
23 pub tree: toml::Value,
24}
25
26#[derive(Clone, Debug)]
27pub struct ModuleConfig {
28 pub name: String,
29 pub version: String,
30 pub container_args: Vec<String>,
31 pub kernel_args: Vec<String>,
32 pub build_args: collections::HashMap<String, String>,
33}
34
35pub fn parse(path: &str) -> Config {
36 let file = fs::read_to_string(path)
38 .expect(&format!("Error while reading configuration file at {}", path));
39
40 let config = file
42 .parse::<toml::Value>()
43 .expect(&format!("Error while parsing configuration file at {}", path));
44
45 let data_dir = config
47 .get("data_dir")
48 .expect("Missing configuration option 'data_dir'")
49 .as_str()
50 .expect("Configuration option 'data_dir' must have a string value");
51
52 let data_dir = String::from(data_dir);
53
54 Config {
55 data_dir,
56 tree: config,
57 }
58}
59
60pub fn module(config: &toml::Value, module: &str) -> ModuleConfig {
61 let config = config
63 .get(module)
64 .expect(&format!("Missing configuration for {} module", module))
65 .as_table()
66 .expect(&format!("Configuration for {} module must be a table", module));
67
68 let build_config = config
69 .get("build")
70 .expect(&format!("Missing build configuration for {} module", module))
71 .as_table()
72 .expect(&format!("Build configuration for {} module must be a table", module));
73
74 let name = String::from(module);
76
77 let version = config
78 .get("version")
79 .expect(&format!("No version specified for {} module", module))
80 .as_str()
81 .expect(&format!("Version identifier for {} module must have a string value", module));
82
83 let version = String::from(version);
84
85 let container_args = toml::value::Value::try_from(Vec::<String>::new()).unwrap();
86 let container_args = config
87 .get("container_args")
88 .unwrap_or(&container_args)
89 .as_array()
90 .expect(&format!("Container arguments for {} module must be an array", module));
91
92 let msg = format!("Container argument for {} module must have a string value", module);
93 let container_args: Vec<_> = container_args
94 .iter()
95 .map(|v| v.as_str().expect(&msg))
96 .map(|v| String::from(v))
97 .collect();
98
99 let kernel_args = toml::value::Value::try_from(Vec::<String>::new()).unwrap();
100 let kernel_args = config
101 .get("kernel_args")
102 .unwrap_or(&kernel_args)
103 .as_array()
104 .expect(&format!("Kernel parameters for {} module must be an array", module));
105
106 let msg = format!("Kernel parameter for {} module must have a string value", module);
107 let kernel_args: Vec<_> = kernel_args
108 .iter()
109 .map(|v| v.as_str().expect(&msg))
110 .map(|v| String::from(v))
111 .collect();
112
113 let mut build_args = collections::HashMap::new();
114
115 for (key, value) in build_config {
116 let value = value
117 .as_str()
118 .expect(&format!("Build parameter for {} module must have a string value", module));
119
120 let key = String::from(key);
121 let value = String::from(value);
122
123 build_args.insert(key, value);
124 }
125
126 ModuleConfig {
127 name,
128 version,
129 container_args,
130 kernel_args,
131 build_args,
132 }
133}