scallop 0.0.30

Wrapper library for bash
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::ffi::{CStr, CString, c_long};
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;

use crate::variables::{Attr, find_variable};
use crate::{Error, bash};

/// Wrapper type for bash arrays.
pub struct Array<'a> {
    inner: *mut bash::Array,
    phantom: PhantomData<&'a mut bash::Array>,
}

impl Array<'_> {
    /// Create a new Array using the given variable name.
    pub fn new<S: AsRef<str>>(name: S) -> Self {
        let cstr = CString::new(name.as_ref()).unwrap();
        unsafe {
            let ptr = bash::make_new_array_variable(cstr.as_ptr() as *mut _);
            Self {
                inner: (*ptr).value as *mut _,
                phantom: PhantomData,
            }
        }
    }

    /// Create a new Array from an existing variable.
    pub fn from<S: AsRef<str>>(name: S) -> crate::Result<Self> {
        let name = name.as_ref();
        let ptr = match find_variable!(name) {
            None => Err(Error::Base(format!("undefined variable: {name}"))),
            Some(v) => {
                if (v.attributes as u32 & Attr::ARRAY.bits()) != 0 {
                    Ok(v.value)
                } else {
                    Err(Error::Base(format!("variable is not an array: {name}")))
                }
            }
        }?;

        Ok(Self {
            inner: ptr as *mut _,
            phantom: PhantomData,
        })
    }

    /// Return a shared iterator for the array.
    pub fn iter(&self) -> ArrayIter<'_> {
        self.into_iter()
    }

    /// Insert an element into the array with a given index value.
    pub fn insert<I, S>(&mut self, index: I, value: S)
    where
        I: Into<c_long>,
        S: AsRef<str>,
    {
        let cstr = CString::new(value.as_ref()).unwrap();
        let cstr = cstr.as_ptr() as *mut _;
        unsafe {
            bash::array_insert(self.inner, index.into(), cstr);
        }
    }

    /// Return a reference to a value at a given index.
    pub fn get<I: Into<c_long>>(&mut self, index: I) -> Option<&str> {
        unsafe {
            let value = bash::array_reference(self.inner, index.into());
            value.as_ref().map(|s| CStr::from_ptr(s).to_str().unwrap())
        }
    }

    /// Remove and return the value at a given index.
    pub fn remove<I: Into<c_long>>(&mut self, index: I) -> Option<String> {
        unsafe {
            let element = bash::array_remove(self.inner, index.into());
            let value = element
                .as_ref()
                .map(|e| CStr::from_ptr(e.value).to_str().unwrap().to_string());
            bash::array_dispose_element(element);
            value
        }
    }

    /// Remove and return the last value if it exists.
    pub fn pop(&mut self) -> Option<String> {
        let index = unsafe { (*self.inner).max_index };
        self.remove(index)
    }

    /// Append a value to the array.
    pub fn push<S: AsRef<str>>(&mut self, value: S) {
        let index = unsafe { (*self.inner).max_index + 1 };
        self.insert(index, value);
    }

    /// Return the length of the array.
    pub fn len(&self) -> usize {
        unsafe { (*self.inner).num_elements.try_into().unwrap() }
    }

    /// Return true if the array is empty, otherwise false.
    pub fn is_empty(&self) -> bool {
        unsafe { (*self.inner).num_elements == 0 }
    }
}

impl<S: AsRef<str>> Extend<S> for Array<'_> {
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = S>,
    {
        for value in iter {
            self.push(value);
        }
    }
}

// Note that this only compares array values and their order, not indices.
impl PartialEq for Array<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.iter().eq(other.iter())
    }
}

impl Eq for Array<'_> {}

impl fmt::Debug for Array<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let values: Vec<_> = self.iter().collect();
        write!(f, "Array {{ {values:?} }}")
    }
}

impl<'a> IntoIterator for &'a Array<'a> {
    type Item = &'a str;
    type IntoIter = ArrayIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe {
            let head = (*self.inner).head;
            ArrayIter {
                head,
                element: head,
                phantom: PhantomData,
            }
        }
    }
}

/// Borrowed iterator for bash arrays.
pub struct ArrayIter<'a> {
    head: *mut bash::array_element,
    element: *mut bash::array_element,
    phantom: PhantomData<&'a mut bash::Array>,
}

impl<'a> Iterator for ArrayIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            self.element = (*self.element).next;
            if self.element != self.head {
                Some(CStr::from_ptr((*self.element).value).to_str().unwrap())
            } else {
                None
            }
        }
    }
}

impl<'a> IntoIterator for Array<'a> {
    type Item = String;
    type IntoIter = ArrayIntoIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe {
            let head = (*self.inner).head;
            let iter = ArrayIter {
                head,
                element: head,
                phantom: PhantomData,
            };
            ArrayIntoIter { iter }
        }
    }
}

/// Owned iterator for bash arrays.
pub struct ArrayIntoIter<'a> {
    iter: ArrayIter<'a>,
}

impl Iterator for ArrayIntoIter<'_> {
    type Item = String;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|s| s.to_string())
    }
}

/// Provide access to bash's $PIPESTATUS shell variable.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PipeStatus(Vec<i32>);

impl PipeStatus {
    /// Get the current value for $PIPESTATUS.
    pub fn get() -> Self {
        let statuses = match Array::from("PIPESTATUS") {
            Ok(array) => array.iter().map(|s| s.parse().unwrap_or(-1)).collect(),
            Err(_) => Default::default(),
        };
        Self(statuses)
    }

    /// Determine if a process failed in the related pipeline.
    pub fn failed(&self) -> bool {
        self.status() != 0
    }

    /// Return the last non-zero exit status, otherwise zero.
    pub fn status(&self) -> i32 {
        self.iter().rev().copied().find(|s| *s != 0).unwrap_or(0)
    }
}

impl Deref for PipeStatus {
    type Target = [i32];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(test)]
mod tests {
    use crate::source;

    use super::*;

    #[test]
    fn new() {
        let mut array = Array::new("ARRAY");
        assert_eq!(format!("{array:?}"), "Array { [] }");
        array.push("a");
        assert_eq!(format!("{array:?}"), r#"Array { ["a"] }"#);

        // verify native bash existence
        source::string("[[ ${ARRAY[0]} == a ]]").unwrap();
    }

    #[test]
    fn from() {
        // nonexistent
        assert!(Array::from("ARRAY").is_err());

        // non-array
        source::string("ARRAY=a").unwrap();
        assert!(Array::from("ARRAY").is_err());

        // empty
        source::string("ARRAY=()").unwrap();
        let array = Array::from("ARRAY").unwrap();
        assert_eq!(format!("{array:?}"), "Array { [] }");
        assert_eq!(array.len(), 0);
        assert!(array.is_empty());

        // non-empty
        source::string("ARRAY=( 1 2 3 )").unwrap();
        let array = Array::from("ARRAY").unwrap();
        assert_eq!(format!("{array:?}"), r#"Array { ["1", "2", "3"] }"#);
        assert_eq!(array.len(), 3);
        assert!(!array.is_empty());
    }

    #[test]
    fn eq() {
        // empty
        let mut array1 = Array::new("ARRAY1");
        source::string("ARRAY2=()").unwrap();
        let mut array2 = Array::from("ARRAY2").unwrap();
        assert_eq!(&array1, &array2);

        // non-empty
        array1.push("1");
        array2.push("1");
        assert_eq!(&array1, &array2);

        // non-matching indices
        array1.insert(100, "2");
        array2.insert(101, "2");
        assert_eq!(&array1, &array2);

        // non-matching values
        array1.push("3");
        assert_ne!(&array1, &array2);
    }

    #[test]
    fn iterator() {
        // empty array
        let array = Array::new("ARRAY");
        assert!(array.iter().next().is_none());
        assert!(array.into_iter().next().is_none());

        // non-empty array
        let mut array = Array::new("ARRAY");
        array.extend(["1", "2", "3"]);
        assert_eq!(array.iter().collect::<Vec<_>>(), ["1", "2", "3"]);
        assert_eq!(array.into_iter().collect::<Vec<_>>(), ["1", "2", "3"]);
    }

    #[test]
    fn manipulation() {
        // empty array
        let mut array = Array::new("ARRAY");

        // remove nonexistent
        assert!(array.remove(0).is_none());
        assert!(array.get(0).is_none());
        assert!(array.pop().is_none());

        // push
        array.push("1");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["1"]);
        assert_eq!(array.get(0).unwrap(), "1");

        // insert overriding existing
        array.insert(0, "2");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["2"]);
        assert_eq!(array.get(0).unwrap(), "2");

        // insert new
        array.insert(1, "3");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["2", "3"]);
        assert_eq!(array.get(1).unwrap(), "3");

        // insert non-sequential
        array.insert(100, "5");
        array.insert(99, "4");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["2", "3", "4", "5"]);
        assert_eq!(array.get(99).unwrap(), "4");

        // push starts at the latest index
        array.push("6");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["2", "3", "4", "5", "6"]);
        assert_eq!(array.get(101).unwrap(), "6");

        // remove existing
        assert_eq!(array.remove(0).unwrap(), "2");
        assert!(array.get(0).is_none());
        assert_eq!(array.iter().collect::<Vec<_>>(), ["3", "4", "5", "6"]);

        // pop values
        assert_eq!(array.pop().unwrap(), "6");
        assert_eq!(array.iter().collect::<Vec<_>>(), ["3", "4", "5"]);
    }

    #[test]
    fn extend() {
        let mut array = Array::new("ARRAY");
        array.extend(["1"]);
        assert_eq!(array.iter().collect::<Vec<_>>(), ["1"]);
        array.extend(vec!["2".to_string(), "3".to_string()]);
        assert_eq!(array.iter().collect::<Vec<_>>(), ["1", "2", "3"]);
    }

    #[test]
    fn pipestatus() {
        // nonexistent
        let pipestatus = PipeStatus::get();
        assert_eq!(pipestatus.status(), 0);
        assert!(!pipestatus.failed());
        assert!(pipestatus.is_empty());

        // single success
        source::string("true").ok();
        let pipestatus = PipeStatus::get();
        assert_eq!(pipestatus.status(), 0);
        assert!(!pipestatus.failed());
        assert_eq!(pipestatus.iter().copied().collect::<Vec<_>>(), [0]);

        // single failure
        source::string("false").ok();
        let pipestatus = PipeStatus::get();
        assert_eq!(pipestatus.status(), 1);
        assert!(pipestatus.failed());
        assert_eq!(pipestatus.iter().copied().collect::<Vec<_>>(), [1]);

        // multiple commands
        source::string("true | false | true").ok();
        let pipestatus = PipeStatus::get();
        assert_eq!(pipestatus.status(), 1);
        assert!(pipestatus.failed());
        assert_eq!(pipestatus.iter().copied().collect::<Vec<_>>(), [0, 1, 0]);

        // custom return status
        source::string(indoc::indoc! {"
            func1() {
                return 1
            }

            func2() {
                return 123
            }

            func1 | func2 | true
        "})
        .unwrap();
        let pipestatus = PipeStatus::get();
        assert_eq!(pipestatus.status(), 123);
        assert!(pipestatus.failed());
        assert_eq!(pipestatus.iter().copied().collect::<Vec<_>>(), [1, 123, 0]);
    }
}