Skip to main content

envl_config/misc/
config.rs

1use std::collections::HashMap;
2
3use envl_utils::{
4    types::Position,
5    variable::{Type, Value},
6};
7
8#[derive(Debug, Clone)]
9pub struct Setting<T> {
10    pub value: T,
11    pub position: Position,
12}
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct SettingWithoutPotision<T> {
16    pub value: T,
17}
18
19#[derive(Debug, Clone)]
20pub struct Settings {
21    pub envl_file_path: Option<Setting<String>>,
22}
23
24#[derive(Debug, Clone, PartialEq)]
25pub struct SettingsWithoutPosition {
26    pub envl_file_path: Option<SettingWithoutPotision<String>>,
27}
28
29#[derive(Debug, Clone)]
30pub struct Var<T = Type, U = Value> {
31    pub v_type: T,
32    pub default_value: U,
33    pub actions_value: U,
34    pub position: Position,
35}
36
37#[derive(Debug, PartialEq)]
38pub struct VarWithoutPosition<T = Type, U = Value> {
39    pub v_type: T,
40    pub default_value: U,
41    pub actions_value: U,
42}
43
44pub type Vars = HashMap<String, Var>;
45
46pub type VarsWithoutPosition = HashMap<String, VarWithoutPosition>;
47
48#[derive(Debug)]
49pub struct Config {
50    pub settings: Settings,
51    pub vars: Vars,
52}
53
54#[derive(Debug, PartialEq)]
55pub struct ConfigWithoutPosition {
56    pub settings: SettingsWithoutPosition,
57    pub vars: VarsWithoutPosition,
58}
59
60pub fn remove_setting_position_prop(settings: Settings) -> SettingsWithoutPosition {
61    SettingsWithoutPosition {
62        envl_file_path: if let Some(setting) = settings.envl_file_path {
63            Some(SettingWithoutPotision {
64                value: setting.value,
65            })
66        } else {
67            None
68        },
69    }
70}
71
72pub fn remove_position_prop(config: Config) -> ConfigWithoutPosition {
73    ConfigWithoutPosition {
74        settings: remove_setting_position_prop(config.settings),
75        vars: config
76            .vars
77            .iter()
78            .map(|(n, v)| {
79                (
80                    n.clone(),
81                    VarWithoutPosition {
82                        v_type: v.v_type.to_owned(),
83                        default_value: v.default_value.to_owned(),
84                        actions_value: v.actions_value.to_owned(),
85                    },
86                )
87            })
88            .collect::<HashMap<String, VarWithoutPosition>>(),
89    }
90}