pub fn parse_settings<T>(
contents: impl AsRef<str>,
updater_fns: &[fn(&mut HashMap<String, Value>, &T)],
args: &T,
) -> (File, Vec<ParseEntryError>)
Expand description
Converts a settings file into a layout + values, opposite of format_settings()
The generic T
is for passing generic data to the updater functions
Examples found in repository?
examples/main.rs (line 21)
6fn main() {
7
8
9
10 // load settings
11
12 pub const UPDATER_FUNCTIONS: &[fn(&mut HashMap<String, ecf::Value>, &())] = &[
13 update_1_to_2, // updates from format 1 to format 2
14 // etc
15 ]; // because there's 1 updater function, the crate will know that the current format version is 2
16
17 pub fn update_1_to_2(settings: &mut HashMap<String, ecf::Value>, args: &()) {
18 println!("this example doesn't actually have a format 2, this is just to show how updates would be done");
19 }
20
21 let (mut ecf_file, errors) = ecf::parse_settings(include_str!("example_settings.ecf"), UPDATER_FUNCTIONS, &());
22
23 // print file data
24 println!("======== Layout: ========");
25 for layout_entry in &ecf_file.layout {println!("{layout_entry:?}");}
26
27 println!("\n\n\n======== Values: ========");
28 for (key, value) in &ecf_file.values {println!("{key}: {value:?}");}
29
30 println!("\n\n\n======== Errors: ========");
31 for error in errors {println!("{error:?}");}
32
33
34
35 // alter settings
36
37 ecf_file.insert(String::from("example key"), ecf::Value::Empty);
38 ecf_file.insert(String::from("new key"), ecf::Value::String (String::from("new value")));
39
40
41
42 // save settings
43
44 let (contents, errors) = ecf::format_settings(&ecf_file);
45
46 println!("\n\n\n======== New Contents: ========");
47 println!("\"\"\"");
48 println!("{contents}");
49 println!("\"\"\"");
50
51 println!("\n\n\n======== Errors: ========");
52 for error in errors {println!("{error:?}");}
53
54
55
56}