1use anyhow::Result;
2use serde::Serialize;
3
4#[derive(Clone, Debug)]
5pub enum Variables {
6 Json(serde_json::Value),
7 Toml(toml::Value),
8 Yaml(serde_yaml::Value),
9}
10
11impl Serialize for Variables {
12 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
13 where
14 S: serde::Serializer,
15 {
16 match self {
17 Variables::Json(v) => v.serialize(serializer),
18 Variables::Toml(v) => v.serialize(serializer),
19 Variables::Yaml(v) => v.serialize(serializer),
20 }
21 }
22}
23
24impl Default for Variables {
25 fn default() -> Self {
26 Self::Json(serde_json::Value::default())
27 }
28}
29
30impl Variables {
31 pub fn from_str(s_type: &str, s: &str) -> Result<Self> {
32 match s_type {
33 "json" => {
34 let value: serde_json::Value =
35 serde_json::from_str(s).map_err(|e| anyhow::anyhow!("Invalid JSON: {}", e))?;
36 Ok(Variables::Json(value))
37 }
38 "toml" => {
39 let value: toml::Value =
40 toml::from_str(s).map_err(|e| anyhow::anyhow!("Invalid TOML: {}", e))?;
41 Ok(Variables::Toml(value))
42 }
43 "yaml" | "yml" => {
44 let value: serde_yaml::Value =
45 serde_yaml::from_str(s).map_err(|e| anyhow::anyhow!("Invalid YAML: {}", e))?;
46 Ok(Variables::Yaml(value))
47 }
48 _ => Err(anyhow::anyhow!(
49 "Unsupported file format (expected .json, .toml, or .yaml)"
50 )),
51 }
52 }
53}