Skip to content

Commit b6ce59d

Browse files
committed
feature: validate presets before starting a server
1 parent abf631d commit b6ce59d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/lib/config.rs

+26
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ pub enum ProgramStartError {
5252
ConfigCliError(ConfigError),
5353
InvalidArgs(Error),
5454
FromFile(FromFileError),
55+
PresetOptions {
56+
name: String,
57+
error: String
58+
},
59+
PresetNotSupported {
60+
name: String,
61+
},
62+
Presets(Vec<ProgramStartError>),
5563
Ip,
5664
BindHttp(std::io::Error),
5765
BindHttps(std::io::Error),
@@ -93,6 +101,24 @@ impl std::fmt::Display for ProgramStartError {
93101
ProgramStartError::Ip => {
94102
write!(f, "could not retrieve the address for the config-server")
95103
}
104+
ProgramStartError::PresetOptions {
105+
name,
106+
error
107+
} => {
108+
write!(f, "preset {} could not be parsed\nerror: {}", name, error)
109+
}
110+
ProgramStartError::PresetNotSupported {
111+
name,
112+
} => {
113+
write!(f, "preset {} is not currently supported", name)
114+
}
115+
ProgramStartError::Presets(errors) => {
116+
let res = errors.iter()
117+
.map(|x| x.to_string())
118+
.collect::<Vec<String>>()
119+
.join("\n");
120+
write!(f, "{}", res)
121+
}
96122
}
97123
}
98124
}

src/lib/setup.rs

+36
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use serde_json;
1313
use std::collections::HashMap;
1414
use std::sync::Arc;
1515
use std::sync::Mutex;
16+
use config::ProgramStartError;
1617

1718
pub type PresetsMap = HashMap<usize, Box<Preset<AppState>>>;
1819

@@ -36,6 +37,41 @@ pub fn apply_presets(
3637
app.default_resource(|r| r.f(proxy_transform))
3738
}
3839

40+
pub fn validate_presets(presets: Vec<&str>, program_config: &ProgramConfig) -> Result<(), ProgramStartError> {
41+
let mut errors = vec![];
42+
program_config.presets.iter().enumerate().for_each(|(i, preset)| {
43+
match preset.name.as_str() {
44+
"m2" => {
45+
let preset_opts: Result<M2PresetOptions, serde_json::Error>
46+
= serde_json::from_value(preset.options.clone());
47+
48+
match preset_opts {
49+
Err(e) => {
50+
errors.push(ProgramStartError::PresetOptions {
51+
error: e.to_string(),
52+
name: "m2".to_string()
53+
})
54+
},
55+
_ => {}
56+
}
57+
}
58+
_ => {
59+
errors.push(
60+
ProgramStartError::PresetNotSupported {
61+
name: "m2".to_string()
62+
}
63+
);
64+
},
65+
}
66+
});
67+
68+
if errors.len() > 0 {
69+
Err(ProgramStartError::Presets(errors))
70+
} else {
71+
Ok(())
72+
}
73+
}
74+
3975
pub fn state_and_presets(
4076
opts: &ProgramOptions,
4177
program_config: &ProgramConfig,

src/lib/system.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use setup::state_and_presets;
99
use ssl;
1010
use std::net::SocketAddr;
1111
use std::net::SocketAddrV4;
12+
use setup::validate_presets;
1213

1314
pub fn create(opts: ProgramOptions) -> Result<(actix::SystemRunner, String), ProgramStartError> {
1415
//
@@ -41,6 +42,11 @@ pub fn create(opts: ProgramOptions) -> Result<(actix::SystemRunner, String), Pro
4142
//
4243
let maybe_seed = server_opts.seed_file.clone();
4344

45+
//
46+
// Exit early if any presets fail validation
47+
//
48+
let _validated_presets = validate_presets(vec!["m2"], &program_config)?;
49+
4450
//
4551
// Now start the server
4652
//

0 commit comments

Comments
 (0)