blaze_common/
variables.rs1use std::path::PathBuf;
2
3use hash_value::Value;
4use serde::{de::Error, Deserialize};
5
6use crate::{configuration_file::ConfigurationFileFormat, util::normalize_path};
7
8pub struct ExtraVariablesFileEntry {
9 pub path: PathBuf,
10 pub optional: bool,
11}
12
13impl From<PathBuf> for ExtraVariablesFileEntry {
14 fn from(path: PathBuf) -> Self {
15 Self {
16 path,
17 optional: false,
18 }
19 }
20}
21
22impl<'de> Deserialize<'de> for ExtraVariablesFileEntry {
23 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
24 where
25 D: serde::Deserializer<'de>,
26 {
27 #[derive(Deserialize)]
28 #[serde(remote = "ExtraVariablesFileEntry")]
29 struct ExtraVariablesFileEntryObject {
30 path: PathBuf,
31 optional: bool,
32 }
33
34 #[derive(Deserialize)]
35 #[serde(untagged)]
36 enum ExtraVariablesFileEntryDeserializationMode {
37 AsPath(PathBuf),
38 #[serde(with = "ExtraVariablesFileEntryObject")]
39 AsObject(ExtraVariablesFileEntry),
40 }
41
42 Ok(
43 match ExtraVariablesFileEntryDeserializationMode::deserialize(deserializer)? {
44 ExtraVariablesFileEntryDeserializationMode::AsObject(mut obj) => {
45 obj.path = normalize_path(&obj.path).map_err(D::Error::custom)?;
46 obj
47 }
48 ExtraVariablesFileEntryDeserializationMode::AsPath(path) => {
49 normalize_path(path).map_err(D::Error::custom)?.into()
50 }
51 },
52 )
53 }
54}
55
56#[derive(Deserialize)]
57pub struct VariablesConfiguration {
58 #[serde(default)]
59 pub vars: Value,
60 #[serde(default)]
61 pub include: Vec<ExtraVariablesFileEntry>,
62}
63
64#[derive(Clone)]
65pub enum VariablesOverride {
66 String {
67 path: Vec<String>,
68 value: String,
69 },
70 Code {
71 format: ConfigurationFileFormat,
72 code: String,
73 },
74 File {
75 path: PathBuf,
76 },
77}