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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Common types needed in the configuration and datamodel.

mod array;
mod constant;
mod documentation;
mod env;
mod function;
mod text;

pub use array::Array;
pub use constant::Constant;
pub use documentation::Documentation;
pub use env::Env;
pub use function::{Function, FunctionParam};
pub use text::Text;

use crate::{datamodel::IndexOps, Cow};
use base64::display::Base64Display;
use psl::GeneratorConfigValue;
use std::fmt;

/// A PSL value representation.
pub enum Value<'a> {
    /// A string value, quoted and escaped accordingly.
    Text(Text<Cow<'a, str>>),
    /// A byte value, quoted and base64-encoded.
    Bytes(Text<Base64Display<'a>>),
    /// A constant value without quoting.
    Constant(Cow<'a, str>),
    /// An array of values.
    Array(Array<Value<'a>>),
    /// A function has a name, and optionally named parameters.
    Function(Function<'a>),
    /// A value can be read from the environment.
    Env(Env<'a>),
    /// An index ops definition.
    IndexOps(IndexOps<'a>),
}

impl<'a> fmt::Debug for Value<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Text(t) => f.debug_tuple("Text").field(t).finish(),
            Value::Constant(val) => {
                write!(f, "Constant({val})")
            }
            Value::Array(ary) => f.debug_tuple("Array").field(ary).finish(),
            Value::Function(fun) => f.debug_tuple("Function").field(fun).finish(),
            Value::Env(e) => f.debug_tuple("Env").field(e).finish(),
            Value::Bytes(Text(b)) => write!(f, "Bytes({b})"),
            Value::IndexOps(ops) => write!(f, "IndexOps({ops})"),
        }
    }
}

impl<'a> From<IndexOps<'a>> for Value<'a> {
    fn from(ops: IndexOps<'a>) -> Self {
        Self::IndexOps(ops)
    }
}

impl<'a, T> From<Constant<T>> for Value<'a>
where
    T: fmt::Display,
{
    fn from(c: Constant<T>) -> Self {
        Self::Constant(c.to_string().into())
    }
}

impl<'a> From<Vec<u8>> for Value<'a> {
    fn from(bytes: Vec<u8>) -> Self {
        let display = Base64Display::with_config(&bytes, base64::STANDARD).to_string();
        Self::Text(Text::new(display))
    }
}

impl<'a> From<&'a [u8]> for Value<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        let display = Base64Display::with_config(bytes, base64::STANDARD);
        Self::Bytes(Text(display))
    }
}

impl<'a> From<Text<Cow<'a, str>>> for Value<'a> {
    fn from(t: Text<Cow<'a, str>>) -> Self {
        Self::Text(t)
    }
}

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

impl<'a> From<Function<'a>> for Value<'a> {
    fn from(t: Function<'a>) -> Self {
        Self::Function(t)
    }
}

impl<'a> From<Env<'a>> for Value<'a> {
    fn from(t: Env<'a>) -> Self {
        Self::Env(t)
    }
}

impl<'a> From<&'a str> for Value<'a> {
    fn from(s: &'a str) -> Self {
        Self::Text(Text(Cow::from(s)))
    }
}

impl<'a> From<Vec<Value<'a>>> for Value<'a> {
    fn from(vec: Vec<Value<'a>>) -> Self {
        Self::Array(vec.into())
    }
}

impl<'a> From<&'a GeneratorConfigValue> for Value<'a> {
    fn from(value: &'a GeneratorConfigValue) -> Self {
        match value {
            GeneratorConfigValue::String(s) => s.as_str().into(),
            GeneratorConfigValue::Array(elements) => elements.iter().map(From::from).collect(),
        }
    }
}

impl<'a> FromIterator<Value<'a>> for Value<'a> {
    fn from_iter<T: IntoIterator<Item = Value<'a>>>(iter: T) -> Self {
        Self::Array(Array::from(iter.into_iter().collect::<Vec<_>>()))
    }
}

impl<'a> fmt::Display for Value<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Text(val) => {
                val.fmt(f)?;
            }
            Value::Constant(val) => {
                val.fmt(f)?;
            }
            Value::Array(array) => {
                array.fmt(f)?;
            }
            Value::Function(fun) => {
                fun.fmt(f)?;
            }
            Value::Env(env) => {
                env.fmt(f)?;
            }
            Value::Bytes(val) => {
                write!(f, "{val}")?;
            }
            Value::IndexOps(ops) => ops.fmt(f)?,
        }

        Ok(())
    }
}