1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::path::PathBuf;

use hash_value::Value;
use serde::{de::Error, Deserialize};

use crate::{configuration_file::ConfigurationFileFormat, util::normalize_path};

pub struct ExtraVariablesFileEntry {
    pub path: PathBuf,
    pub optional: bool,
}

impl From<PathBuf> for ExtraVariablesFileEntry {
    fn from(path: PathBuf) -> Self {
        Self {
            path,
            optional: false,
        }
    }
}

impl<'de> Deserialize<'de> for ExtraVariablesFileEntry {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(remote = "ExtraVariablesFileEntry")]
        struct ExtraVariablesFileEntryObject {
            path: PathBuf,
            optional: bool,
        }

        #[derive(Deserialize)]
        #[serde(untagged)]
        enum ExtraVariablesFileEntryDeserializationMode {
            AsPath(PathBuf),
            #[serde(with = "ExtraVariablesFileEntryObject")]
            AsObject(ExtraVariablesFileEntry),
        }

        Ok(
            match ExtraVariablesFileEntryDeserializationMode::deserialize(deserializer)? {
                ExtraVariablesFileEntryDeserializationMode::AsObject(mut obj) => {
                    obj.path = normalize_path(&obj.path).map_err(D::Error::custom)?;
                    obj
                }
                ExtraVariablesFileEntryDeserializationMode::AsPath(path) => {
                    normalize_path(path).map_err(D::Error::custom)?.into()
                }
            },
        )
    }
}

#[derive(Deserialize)]
pub struct VariablesConfiguration {
    #[serde(default)]
    pub vars: Value,
    #[serde(default)]
    pub include: Vec<ExtraVariablesFileEntry>,
}

#[derive(Clone)]
pub enum VariablesOverride {
    String {
        path: Vec<String>,
        value: String,
    },
    Code {
        format: ConfigurationFileFormat,
        code: String,
    },
    File {
        path: PathBuf,
    },
}