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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2022 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Quirks of variables

use super::Value;
use super::Variable;
use either::{Left, Right};
use std::borrow::Cow;
use yash_syntax::source::Location;
use yash_syntax::source::Source;

/// Special characteristics of a variable
///
/// While most variables act as a simple store of a value, some variables
/// exhibit special effects when they get expanded or assigned to. Such
/// variables may have their value computed dynamically on expansion or may have
/// an internal state that is updated when the value is set. `Quirk` determines
/// the nature of a variable and contains the relevant state.
///
/// Use [`Variable::expand`] to apply the variable's quirk when expanding a
/// variable.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Quirk {
    /// Quirk for the `$LINENO` variable
    ///
    /// The value of a variable having this variant of `Quirk` is computed
    /// dynamically from the expanding context. The result is the line number of
    /// the location of the parameter expansion. This `Quirk` is lost when an
    /// assignment sets a new value to the variable.
    LineNumber,
    // TODO Random(RefCell<RandomState>)
    // TODO Path(...)
}

/// Expanded value of a variable
///
/// Variables with a [`Quirk`] may have their values computed dynamically when
/// expanded, hence [`Cow`] in the enum values.
/// Use [`Variable::expand`] to get an `Expansion` instance.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Expansion<'a> {
    /// The value does not exist.
    Unset,
    /// The value is a single string.
    Scalar(Cow<'a, str>),
    /// The value is an array of strings.
    Array(Cow<'a, [String]>),
}

/// Returns `Unset`.
impl Default for Expansion<'_> {
    fn default() -> Self {
        Self::Unset
    }
}

impl From<String> for Expansion<'static> {
    fn from(value: String) -> Self {
        Expansion::Scalar(Cow::Owned(value))
    }
}

impl<'a> From<&'a str> for Expansion<'a> {
    fn from(value: &'a str) -> Self {
        Expansion::Scalar(Cow::Borrowed(value))
    }
}

impl<'a> From<&'a String> for Expansion<'a> {
    fn from(value: &'a String) -> Self {
        Expansion::Scalar(Cow::Borrowed(value))
    }
}

impl From<Option<String>> for Expansion<'static> {
    fn from(value: Option<String>) -> Self {
        match value {
            Some(value) => value.into(),
            None => Expansion::Unset,
        }
    }
}

impl From<Vec<String>> for Expansion<'static> {
    fn from(values: Vec<String>) -> Self {
        Expansion::Array(Cow::Owned(values))
    }
}

impl<'a> From<&'a [String]> for Expansion<'a> {
    fn from(values: &'a [String]) -> Self {
        Expansion::Array(Cow::Borrowed(values))
    }
}

impl<'a> From<&'a Vec<String>> for Expansion<'a> {
    fn from(values: &'a Vec<String>) -> Self {
        Expansion::Array(Cow::Borrowed(values))
    }
}

impl From<Value> for Expansion<'static> {
    fn from(value: Value) -> Self {
        match value {
            Value::Scalar(value) => Expansion::from(value),
            Value::Array(values) => Expansion::from(values),
        }
    }
}

impl<'a> From<&'a Value> for Expansion<'a> {
    fn from(value: &'a Value) -> Self {
        match value {
            Value::Scalar(value) => Expansion::from(value),
            Value::Array(values) => Expansion::from(values),
        }
    }
}

impl From<Option<Value>> for Expansion<'static> {
    fn from(value: Option<Value>) -> Self {
        match value {
            Some(value) => value.into(),
            None => Expansion::Unset,
        }
    }
}

impl<'a, V> From<Option<&'a V>> for Expansion<'a>
where
    Expansion<'a>: From<&'a V>,
{
    fn from(value: Option<&'a V>) -> Self {
        match value {
            Some(value) => value.into(),
            None => Expansion::Unset,
        }
    }
}

impl From<Expansion<'_>> for Option<Value> {
    fn from(expansion: Expansion<'_>) -> Option<Value> {
        match expansion {
            Expansion::Unset => None,
            Expansion::Scalar(value) => Some(Value::Scalar(value.into_owned())),
            Expansion::Array(values) => Some(Value::Array(values.into_owned())),
        }
    }
}

impl<'a> From<&'a Expansion<'a>> for Expansion<'a> {
    fn from(expansion: &'a Expansion<'a>) -> Expansion<'a> {
        match expansion {
            Expansion::Unset => Expansion::Unset,
            Expansion::Scalar(value) => value.as_ref().into(),
            Expansion::Array(values) => values.as_ref().into(),
        }
    }
}

impl Expansion<'_> {
    /// Converts into an owned value
    #[must_use]
    pub fn into_owned(self) -> Option<Value> {
        self.into()
    }

    /// Converts to a borrowed value
    #[must_use]
    pub fn as_ref(&self) -> Expansion<'_> {
        self.into()
    }

    /// Returns the "length" of the value.
    ///
    /// For `Unset`, the length is 0.
    /// For `Scalar`, the length is the number of characters.
    /// For `Array`, the length is the number of strings.
    #[must_use]
    pub fn len(&self) -> usize {
        match self {
            Expansion::Unset => 0,
            Expansion::Scalar(value) => value.len(),
            Expansion::Array(values) => values.len(),
        }
    }

    /// Tests whether the [length](Self::len) is zero.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Splits the expansion by colons.
    ///
    /// If this expansion is `Scalar`, the value is separated at each occurrence
    /// of colon (`:`). For `Array`, each array item is returned without further
    /// splitting the value. For `Unset`, an empty iterator is returned.
    ///
    /// ```
    /// # use yash_env::variable::Expansion;
    /// let expansion = Expansion::from("/usr/local/bin:/usr/bin:/bin");
    /// let values: Vec<&str> = expansion.split().collect();
    /// assert_eq!(values, ["/usr/local/bin", "/usr/bin", "/bin"]);
    /// ```
    ///
    /// ```
    /// # use yash_env::variable::Expansion;
    /// let expansion = Expansion::from(vec!["foo".to_string(), "bar".to_string()]);
    /// let values: Vec<&str> = expansion.split().collect();
    /// assert_eq!(values, ["foo", "bar"]);
    /// ```
    ///
    /// ```
    /// # use yash_env::variable::Expansion;
    /// let expansion = Expansion::Unset;
    /// let values: Vec<&str> = expansion.split().collect();
    /// assert!(values.is_empty());
    /// ```
    pub fn split(&self) -> impl Iterator<Item = &str> {
        match self {
            Self::Unset => Right([].iter().map(String::as_str)),
            Self::Scalar(value) => Left(value.split(':')),
            Self::Array(values) => Right(values.iter().map(String::as_str)),
        }
    }
}

/// Implementation of [`Variable::expand`].
pub fn expand<'a>(var: &'a Variable, mut location: &Location) -> Expansion<'a> {
    match &var.quirk {
        None => var.value.as_ref().into(),

        Some(Quirk::LineNumber) => {
            while let Source::Alias { original, .. } = &*location.code.source {
                location = original;
            }
            let line_number = location.code.line_number(location.range.start);
            line_number.to_string().into()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::num::NonZeroU64;
    use std::rc::Rc;
    use yash_syntax::alias::Alias;
    use yash_syntax::source::Code;

    #[test]
    fn expand_no_quirk() {
        let var = Variable::new("foo");
        let loc = Location::dummy("somewhere");
        let result = var.expand(&loc);
        assert_eq!(result, Expansion::Scalar("foo".into()));
    }

    fn stub_code() -> Rc<Code> {
        Code {
            value: "foo\nbar\nbaz\n".to_string().into(),
            start_line_number: NonZeroU64::new(42).unwrap(),
            source: Source::Unknown.into(),
        }
        .into()
    }

    #[test]
    fn expand_line_number_of_first_line() {
        let var = Variable {
            quirk: Some(Quirk::LineNumber),
            ..Default::default()
        };
        let code = stub_code();
        let range = 1..3;
        let loc = Location { code, range };
        let result = var.expand(&loc);
        assert_eq!(result, Expansion::Scalar("42".into()));
    }

    #[test]
    fn expand_line_number_of_third_line() {
        let var = Variable {
            quirk: Some(Quirk::LineNumber),
            ..Default::default()
        };
        let code = stub_code();
        let range = 8..12;
        let loc = Location { code, range };
        let result = var.expand(&loc);
        assert_eq!(result, Expansion::Scalar("44".into()));
    }

    #[test]
    fn expand_line_number_in_alias() {
        fn to_alias(original: Location) -> Location {
            let alias = Alias {
                name: "name".to_string(),
                replacement: "replacement".to_string(),
                global: false,
                origin: Location::dummy("alias"),
            }
            .into();
            let code = Code {
                value: " \n \n ".to_string().into(),
                start_line_number: NonZeroU64::new(15).unwrap(),
                source: Source::Alias { original, alias }.into(),
            }
            .into();
            let range = 0..1;
            Location { code, range }
        }

        let var = Variable {
            quirk: Some(Quirk::LineNumber),
            ..Default::default()
        };
        let code = stub_code();
        let range = 8..12;
        let loc = to_alias(to_alias(Location { code, range }));
        let result = var.expand(&loc);
        assert_eq!(result, Expansion::Scalar("44".into()));
    }
}