Advertisement
xladomaz

set_variables backup

Apr 23rd, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. pub fn set_variables(variables: &HashMap<String, String>, component: &mut Component) {
  2. let colors = &mut component.data.constant.colors;
  3.  
  4. let mut resolved_vars = HashMap::<String, String>::new();
  5. for (var_key, var_value) in colors.iter_mut() {
  6. if var_key.starts_with('$') {
  7. // Find in local scope
  8. let var_key = &var_key[1..];
  9. let local_value = resolved_vars.iter().find_map(|(k, v)| {
  10. if var_key == *k {
  11. Some(v.to_owned())
  12. } else {
  13. None
  14. }
  15. });
  16. if let Some(value) = local_value {
  17. resolved_vars.insert(var_key.to_owned(), value);
  18. } else {
  19. let global_value = variables.iter().find_map(|(k, v)| {
  20. if var_key == *k {
  21. Some(v.to_owned())
  22. } else {
  23. None
  24. }
  25. });
  26. if let Some(value) = global_value {
  27. resolved_vars.insert(var_key.to_owned(), value);
  28. } else {
  29. error!("Variable {} not found!", var_key);
  30. }
  31. }
  32. } else {
  33. resolved_vars.insert(var_key.to_owned(), var_value.to_owned());
  34. }
  35. }
  36. component.data.constant.colors = resolved_vars;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement