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
use super::{Constant, Text, Value};
use std::{borrow::Cow, fmt};

/// Represents a function parameter in the PSL.
#[derive(Debug)]
pub enum FunctionParam<'a> {
    /// key: value
    KeyValue(Cow<'a, str>, Value<'a>),
    /// value (only)
    OnlyValue(Value<'a>),
}

impl<'a> From<Value<'a>> for FunctionParam<'a> {
    fn from(v: Value<'a>) -> Self {
        Self::OnlyValue(v)
    }
}

impl<'a> From<&'a str> for FunctionParam<'a> {
    fn from(v: &'a str) -> Self {
        Self::OnlyValue(Value::Text(Text::new(v)))
    }
}

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

impl<'a, T> From<Constant<T>> for FunctionParam<'a>
where
    T: fmt::Display + 'a,
{
    fn from(v: Constant<T>) -> Self {
        Self::OnlyValue(Value::from(Constant::new_no_validate(v)))
    }
}

impl<'a, T> From<(Cow<'a, str>, T)> for FunctionParam<'a>
where
    T: Into<Value<'a>>,
{
    fn from(kv: (Cow<'a, str>, T)) -> Self {
        Self::KeyValue(kv.0, kv.1.into())
    }
}

impl<'a, T> From<(&'a str, T)> for FunctionParam<'a>
where
    T: Into<Value<'a>>,
{
    fn from(kv: (&'a str, T)) -> Self {
        Self::KeyValue(kv.0.into(), kv.1.into())
    }
}

impl<'a> fmt::Display for FunctionParam<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FunctionParam::KeyValue(k, v) => {
                write!(f, "{k}: {v}")
            }
            FunctionParam::OnlyValue(v) => v.fmt(f),
        }
    }
}

/// Represents a function value in the PSL.
#[derive(Debug)]
pub struct Function<'a> {
    name: Constant<Cow<'a, str>>,
    params: Vec<FunctionParam<'a>>,
    render_empty_parentheses: bool,
}

impl<'a> From<Constant<Cow<'a, str>>> for Function<'a> {
    fn from(name: Constant<Cow<'a, str>>) -> Self {
        Self {
            name,
            params: Vec::new(),
            render_empty_parentheses: false,
        }
    }
}

impl<'a> Function<'a> {
    /// Creates a plain function with no parameters.
    pub fn new(name: impl Into<Cow<'a, str>>) -> Self {
        Self {
            name: Constant::new_no_validate(name.into()),
            params: Vec::new(),
            render_empty_parentheses: false,
        }
    }

    /// Add a new parameter to the function. If no parameters are
    /// added, the parentheses are not rendered.
    pub fn push_param(&mut self, param: impl Into<FunctionParam<'a>>) {
        self.params.push(param.into());
    }

    pub(crate) fn render_empty_parentheses(&mut self) {
        self.render_empty_parentheses = true;
    }
}

impl<'a> fmt::Display for Function<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.name.fmt(f)?;

        if !self.params.is_empty() || self.render_empty_parentheses {
            f.write_str("(")?;
        }

        if !self.params.is_empty() {
            for (i, param) in self.params.iter().enumerate() {
                param.fmt(f)?;

                if i < self.params.len() - 1 {
                    f.write_str(", ")?;
                }
            }
        }

        if !self.params.is_empty() || self.render_empty_parentheses {
            f.write_str(")")?;
        }

        Ok(())
    }
}