1use std::collections::HashMap;
2
3use math_parse::MathParse;
4use serde::Deserialize;
5
6const CONSTANT_VALUES: &str = include_str!("const_values.toml");
7const MAX_DEPTH: usize = 5;
8
9#[derive(Debug)]
10pub enum ConstantTypes {
11 String { value: String },
12 Number { value: String },
13 Hex { value: String },
14}
15
16#[derive(Debug, Deserialize)]
17pub struct ConstantValue {
18 pub r#type: String,
19 pub value: String,
20 pub comment: Option<String>,
21}
22
23pub fn get_variables_map(a: &HashMap<String, ConstantValue>) -> HashMap<String, String> {
24 a.iter()
25 .map(|(k, v)| (k.clone(), v.value.to_string()))
26 .collect::<HashMap<_, _>>()
27}
28
29pub fn get_constant_values() -> HashMap<String, ConstantValue> {
30 let mut constant_values = toml::from_str(CONSTANT_VALUES).unwrap();
31 for _ in 0..MAX_DEPTH {
32 let mut has_changed = false;
33 let variables_map = get_variables_map(&constant_values);
34 constant_values = constant_values
35 .into_iter()
36 .map(|(k, mut v)| {
37 let value = match MathParse::parse(&v.value) {
38 Err(_) => v.value,
39 Ok(expression) => match expression.solve_int(Some(&variables_map)) {
40 Err(_) => v.value,
41 Ok(result) => {
42 let result = format!("0x{:x}", result).to_string();
43 if v.value != result {
44 has_changed = true
45 }
46 result
47 }
48 },
49 };
50 v.value = value;
51 (k, v)
52 })
53 .collect::<HashMap<_, _>>();
54 if !has_changed {
55 break;
56 }
57 }
58 constant_values
59}