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
use itertools::Itertools;
use serde::{Deserialize, Serialize};

pub type Array = Vec<Value>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
    Boolean(bool),
    Integer(i64),
    String(String),
    Array(Array),
}

impl Value {
    pub fn to_cmake_value(&self) -> String {
        match self {
            Value::Boolean(b) => b.to_string().to_uppercase(),
            Value::Integer(i) => i.to_string(),
            Value::String(s) => s.clone(),
            Value::Array(a) => a.iter().map(Value::to_cmake_value).join(","),
        }
    }
}

impl From<Array> for Value {
    fn from(value: Array) -> Self {
        Self::Array(value)
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Self::Boolean(value)
    }
}

impl From<i64> for Value {
    fn from(value: i64) -> Self {
        Self::Integer(value)
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::String(value)
    }
}

impl From<Vec<String>> for Value {
    fn from(value: Vec<String>) -> Self {
        Self::Array(value.into_iter().map(Value::from).collect())
    }
}