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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use std::borrow;
use std::cmp::Ordering;
use std::fmt;

use itertools;

use super::map;
use super::Scalar;
use super::ScalarCow;

/// An enum to represent different value types
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Value {
    /// A scalar value.
    Scalar(Scalar),
    /// A sequence of `Value`s.
    Array(Array),
    /// A sequence of key/`Value` pairs.
    Object(Object),
    /// Nothing.
    Nil,
    /// No content.
    Empty,
    /// Evaluates to empty string.
    Blank,
}

/// Type representing a Liquid array, payload of the `Value::Array` variant
pub type Array = Vec<Value>;

/// Type representing a Liquid object, payload of the `Value::Object` variant
pub type Object = map::Map;

impl Value {
    /// Create as a `Scalar`.
    pub fn scalar<T: Into<Scalar>>(value: T) -> Self {
        Value::Scalar(Scalar::new(value))
    }

    /// Create as an `Array`.
    pub fn array<I: IntoIterator<Item = Self>>(iter: I) -> Self {
        let v: Array = iter.into_iter().collect();
        Value::Array(v)
    }

    /// Create as nothing.
    pub fn nil() -> Self {
        Value::Nil
    }

    /// A `Display` for a `Scalar` as source code.
    pub fn source(&self) -> ValueSource {
        ValueSource(&self)
    }

    /// A `Display` for a `Value` rendered for the user.
    pub fn render(&self) -> ValueRendered {
        ValueRendered(&self)
    }

    /// Interpret as a string.
    pub fn to_str(&self) -> borrow::Cow<str> {
        match *self {
            Value::Scalar(ref x) => x.to_str(),
            Value::Array(ref x) => {
                let arr: Vec<_> = x.iter().map(|v| v.render()).collect();
                borrow::Cow::Owned(itertools::join(arr, ""))
            }
            Value::Object(ref x) => {
                let arr: Vec<_> = x
                    .iter()
                    .map(|(k, v)| format!("{}{}", k, v.render()))
                    .collect();
                borrow::Cow::Owned(itertools::join(arr, ""))
            }
            Value::Nil | Value::Empty | Value::Blank => borrow::Cow::Borrowed(""),
        }
    }

    /// Extracts the scalar value if it is a scalar.
    pub fn as_scalar(&self) -> Option<&Scalar> {
        match *self {
            Value::Scalar(ref s) => Some(s),
            _ => None,
        }
    }

    /// Extracts the scalar value if it is a scalar.
    pub fn into_scalar(self) -> Option<Scalar> {
        match self {
            Value::Scalar(s) => Some(s),
            _ => None,
        }
    }

    /// Tests whether this value is a scalar
    pub fn is_scalar(&self) -> bool {
        self.as_scalar().is_some()
    }

    /// Extracts the array value if it is an array.
    pub fn as_array(&self) -> Option<&Array> {
        match *self {
            Value::Array(ref s) => Some(s),
            _ => None,
        }
    }

    /// Extracts the array value if it is an array.
    pub fn as_array_mut(&mut self) -> Option<&mut Array> {
        match *self {
            Value::Array(ref mut s) => Some(s),
            _ => None,
        }
    }

    /// Extracts the array value if it is an array.
    pub fn into_array(self) -> Option<Array> {
        match self {
            Value::Array(s) => Some(s),
            _ => None,
        }
    }

    /// Tests whether this value is an array
    pub fn is_array(&self) -> bool {
        self.as_array().is_some()
    }

    /// Extracts the object value if it is a object.
    pub fn as_object(&self) -> Option<&Object> {
        match *self {
            Value::Object(ref s) => Some(s),
            _ => None,
        }
    }

    /// Extracts the object value if it is a object.
    pub fn as_object_mut(&mut self) -> Option<&mut Object> {
        match *self {
            Value::Object(ref mut s) => Some(s),
            _ => None,
        }
    }

    /// Extracts the object value if it is a object.
    pub fn into_object(self) -> Option<Object> {
        match self {
            Value::Object(s) => Some(s),
            _ => None,
        }
    }

    /// Tests whether this value is an object
    pub fn is_object(&self) -> bool {
        self.as_object().is_some()
    }

    /// Extracts the nil value if it is nil
    pub fn as_nil(&self) -> Option<()> {
        match *self {
            Value::Nil => Some(()),
            _ => None,
        }
    }

    /// Tests whether this value is nil
    pub fn is_nil(&self) -> bool {
        match *self {
            Value::Nil => true,
            _ => false,
        }
    }

    /// Extracts the empty value if it is empty
    pub fn as_empty(&self) -> Option<()> {
        match *self {
            Value::Empty => Some(()),
            _ => None,
        }
    }

    /// Tests whether this value is empty
    pub fn is_empty(&self) -> bool {
        match *self {
            Value::Empty => true,
            _ => false,
        }
    }

    /// Extracts the blank value if it is blank
    pub fn as_blank(&self) -> Option<()> {
        match *self {
            Value::Blank => Some(()),
            _ => None,
        }
    }

    /// Tests whether this value is blank
    pub fn is_blank(&self) -> bool {
        match *self {
            Value::Blank => true,
            _ => false,
        }
    }

    /// Evaluate using Liquid "truthiness"
    pub fn is_truthy(&self) -> bool {
        // encode Ruby truthiness: all values except false and nil are true
        match *self {
            Value::Scalar(ref x) => x.is_truthy(),
            Value::Nil | Value::Empty | Value::Blank => false,
            _ => true,
        }
    }

    /// Whether a default constructed value.
    pub fn is_default(&self) -> bool {
        match *self {
            Value::Scalar(ref x) => x.is_default(),
            Value::Nil => true,
            Value::Empty => true,
            Value::Blank => true,
            Value::Array(ref x) => x.is_empty(),
            Value::Object(ref x) => x.is_empty(),
        }
    }

    /// Report the data type (generally for error reporting).
    pub fn type_name(&self) -> &'static str {
        match *self {
            Value::Scalar(ref x) => x.type_name(),
            Value::Nil => "nil",
            Value::Empty => "empty",
            Value::Blank => "blank",
            Value::Array(_) => "array",
            Value::Object(_) => "object",
        }
    }

    /// Access a contained `Value`.
    pub fn contains_key(&self, index: &Scalar) -> bool {
        match *self {
            Value::Array(ref x) => {
                if let Some(index) = index.to_integer() {
                    let index = convert_index(index, x.len());
                    index < x.len()
                } else {
                    match &*index.to_str() {
                        "first" | "last" => true,
                        _ => false,
                    }
                }
            }
            Value::Object(ref x) => x.contains_key(index.to_str().as_ref()),
            _ => false,
        }
    }

    /// Keys available for lookup.
    pub fn keys(&self) -> Keys {
        let v = match *self {
            Value::Array(ref x) => {
                let start: i32 = 0;
                let end = x.len() as i32;
                let mut keys: Vec<_> = (start..end).map(Scalar::new).collect();
                keys.push(Scalar::new("first"));
                keys.push(Scalar::new("last"));
                keys
            }
            Value::Object(ref x) => x
                .keys()
                .map(|s| match *s {
                    borrow::Cow::Borrowed(s) => Scalar::new(s),
                    borrow::Cow::Owned(ref s) => Scalar::new(s.to_owned()),
                })
                .collect(),
            _ => vec![],
        };
        Keys(v.into_iter())
    }

    /// Access a contained `Value`.
    pub fn get<'s>(&'s self, index: &ScalarCow) -> Option<&'s Self> {
        match *self {
            Value::Array(ref x) => {
                if let Some(index) = index.to_integer() {
                    let index = convert_index(index, x.len());
                    x.get(index as usize)
                } else {
                    match &*index.to_str() {
                        "first" => x.get(0),
                        "last" => x.get(x.len() - 1),
                        _ => None,
                    }
                }
            }
            Value::Object(ref x) => x.get(index.to_str().as_ref()),
            _ => None,
        }
    }
}

/// Iterator over a `Value`s keys.
#[derive(Debug)]
pub struct Keys(::std::vec::IntoIter<Scalar>);

impl Iterator for Keys {
    type Item = Scalar;

    #[inline]
    fn next(&mut self) -> Option<Scalar> {
        self.0.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    #[inline]
    fn count(self) -> usize {
        self.0.count()
    }
}

impl ExactSizeIterator for Keys {
    #[inline]
    fn len(&self) -> usize {
        self.0.len()
    }
}

fn convert_index(index: i32, max_size: usize) -> usize {
    let index = index as isize;
    let max_size = max_size as isize;
    let index = if 0 <= index { index } else { max_size + index };
    index as usize
}

impl Default for Value {
    fn default() -> Self {
        Self::nil()
    }
}

impl PartialEq<Value> for Value {
    fn eq(&self, other: &Self) -> bool {
        value_eq(self, other)
    }
}

impl Eq for Value {}

impl PartialOrd<Value> for Value {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        value_cmp(self, other)
    }
}

/// A `Display` for a `Scalar` as source code.
#[derive(Debug)]
pub struct ValueSource<'s>(&'s Value);

impl<'s> fmt::Display for ValueSource<'s> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            Value::Scalar(ref x) => write!(f, "{}", x.render())?,
            Value::Array(ref x) => {
                write!(f, "[")?;
                for item in x {
                    write!(f, "{}, ", item.render())?;
                }
                write!(f, "]")?;
            }
            Value::Object(ref x) => {
                write!(f, "{{")?;
                for (k, v) in x {
                    write!(f, r#""{}": {}, "#, k, v.render())?;
                }
                write!(f, "}}")?;
            }
            Value::Nil => write!(f, "nil")?,
            Value::Empty => write!(f, "empty")?,
            Value::Blank => write!(f, "blank")?,
        }
        Ok(())
    }
}

/// A `Display` for a `Value` rendered for the user.
#[derive(Debug)]
pub struct ValueRendered<'s>(&'s Value);

impl<'s> fmt::Display for ValueRendered<'s> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Must match `Value::to_str`
        match self.0 {
            Value::Scalar(ref x) => write!(f, "{}", x.render())?,
            Value::Array(ref x) => {
                for item in x {
                    write!(f, "{}", item.render())?;
                }
            }
            Value::Object(ref x) => {
                for (k, v) in x {
                    write!(f, "{}{}", k, v.render())?;
                }
            }
            Value::Nil | Value::Empty | Value::Blank => (),
        }
        Ok(())
    }
}

fn value_eq(lhs: &Value, rhs: &Value) -> bool {
    match (lhs, rhs) {
        (&Value::Scalar(ref x), &Value::Scalar(ref y)) => x == y,
        (&Value::Array(ref x), &Value::Array(ref y)) => x == y,
        (&Value::Object(ref x), &Value::Object(ref y)) => x == y,
        (&Value::Nil, &Value::Nil)
        | (&Value::Empty, &Value::Empty)
        | (&Value::Blank, &Value::Blank)
        | (&Value::Empty, &Value::Blank)
        | (&Value::Blank, &Value::Empty) => true,

        // encode a best-guess of empty rules
        // See tables in https://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
        (&Value::Empty, &Value::Scalar(ref s)) | (&Value::Scalar(ref s), &Value::Empty) => {
            s.to_str().is_empty()
        }
        (&Value::Empty, &Value::Array(ref s)) | (&Value::Array(ref s), &Value::Empty) => {
            s.is_empty()
        }
        (&Value::Empty, &Value::Object(ref s)) | (&Value::Object(ref s), &Value::Empty) => {
            s.is_empty()
        }

        // encode a best-guess of blank rules
        // See tables in https://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
        (&Value::Nil, &Value::Blank) | (&Value::Blank, &Value::Nil) => true,
        (&Value::Blank, &Value::Scalar(ref s)) | (&Value::Scalar(ref s), &Value::Blank) => {
            s.to_str().trim().is_empty() || !s.to_bool().unwrap_or(true)
        }
        (&Value::Blank, &Value::Array(ref s)) | (&Value::Array(ref s), &Value::Blank) => {
            s.is_empty()
        }
        (&Value::Blank, &Value::Object(ref s)) | (&Value::Object(ref s), &Value::Blank) => {
            s.is_empty()
        }

        // encode Ruby truthiness: all values except false and nil are true
        (&Value::Nil, &Value::Scalar(ref b)) | (&Value::Scalar(ref b), &Value::Nil) => {
            !b.to_bool().unwrap_or(true)
        }
        (_, &Value::Scalar(ref b)) | (&Value::Scalar(ref b), _) => b.to_bool().unwrap_or(false),

        _ => false,
    }
}

fn value_cmp(lhs: &Value, rhs: &Value) -> Option<Ordering> {
    match (lhs, rhs) {
        (&Value::Scalar(ref x), &Value::Scalar(ref y)) => x.partial_cmp(y),
        (&Value::Array(ref x), &Value::Array(ref y)) => x.iter().partial_cmp(y.iter()),
        (&Value::Object(ref x), &Value::Object(ref y)) => x.iter().partial_cmp(y.iter()),
        _ => None,
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_to_string_scalar() {
        let val = Value::scalar(42f64);
        assert_eq!(&val.render().to_string(), "42");
        assert_eq!(&val.to_str(), "42");
    }

    #[test]
    fn test_to_string_array() {
        let val = Value::Array(vec![
            Value::scalar(3f64),
            Value::scalar("test"),
            Value::scalar(5.3),
        ]);
        assert_eq!(&val.render().to_string(), "3test5.3");
        assert_eq!(&val.to_str(), "3test5.3");
    }

    // TODO make a test for object, remember values are in arbitrary orders in HashMaps

    #[test]
    fn test_to_string_nil() {
        assert_eq!(&Value::nil().render().to_string(), "");
        assert_eq!(&Value::nil().to_str(), "");
    }

    #[test]
    fn scalar_equality() {
        assert_eq!(Value::scalar("alpha"), Value::scalar("alpha"));
        assert_eq!(Value::scalar(""), Value::scalar(""));
        assert!(Value::scalar("alpha") != Value::scalar("beta"));
        assert!(Value::scalar("beta") != Value::scalar("alpha"));
    }

    #[test]
    fn scalars_have_ruby_truthiness() {
        // all strings in ruby are true
        assert_eq!(Value::scalar(true), Value::scalar("All strings are truthy"));
        assert_eq!(Value::scalar(true), Value::scalar(""));
        assert!(Value::scalar("").is_truthy());

        assert_eq!(Value::scalar(true), Value::scalar(true));
        assert!(Value::scalar(true) != Value::scalar(false));
    }

    #[test]
    fn array_equality() {
        let a = Value::Array(vec![Value::scalar("one"), Value::scalar("two")]);
        let b = Value::Array(vec![Value::scalar("alpha"), Value::scalar("beta")]);

        assert_eq!(a, a);
        assert!(a != b);
        assert!(b != a);
    }

    #[test]
    fn arrays_have_ruby_truthiness() {
        assert_eq!(Value::scalar(true), Value::Array(Vec::new()));
        assert!(Value::Array(Vec::new()).is_truthy());
    }

    #[test]
    fn object_equality() {
        let a: Object = [
            ("alpha".into(), Value::scalar("1")),
            ("beta".into(), Value::scalar(2f64)),
        ]
        .into_iter()
        .cloned()
        .collect();
        let a = Value::Object(a);

        let b: Object = [
            ("alpha".into(), Value::scalar("1")),
            ("beta".into(), Value::scalar(2f64)),
            ("gamma".into(), Value::Array(vec![])),
        ]
        .into_iter()
        .cloned()
        .collect();
        let b = Value::Object(b);

        assert_eq!(a, a);
        assert!(a != b);
        assert!(b != a);
    }

    #[test]
    fn objects_have_ruby_truthiness() {
        assert_eq!(Value::scalar(true), Value::Object(Object::new()));
        assert!(Value::Object(Object::new()).is_truthy());
    }

    #[test]
    fn nil_equality() {
        assert_eq!(Value::Nil, Value::Nil);
    }

    #[test]
    fn nils_have_ruby_truthiness() {
        assert_eq!(Value::scalar(false), Value::Nil);
        assert!(!Value::Nil.is_truthy());

        assert_eq!(Value::scalar(false), Value::Nil);
        assert!(Value::scalar(true) != Value::Nil);
        assert!(Value::scalar("") != Value::Nil);
    }

    #[test]
    fn empty_equality() {
        // Truth table from https://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
        assert_eq!(Value::Empty, Value::Empty);
        assert_eq!(Value::Empty, Value::Blank);
        assert_eq!(Value::Empty, liquid_value!(""));
        assert_ne!(Value::Empty, liquid_value!(" "));
        assert_eq!(Value::Empty, liquid_value!([]));
        assert_ne!(Value::Empty, liquid_value!([nil]));
        assert_eq!(Value::Empty, liquid_value!({}));
        assert_ne!(Value::Empty, liquid_value!({ "a": nil }));
    }

    #[test]
    fn blank_equality() {
        // Truth table from https://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
        assert_eq!(Value::Blank, Value::Blank);
        assert_eq!(Value::Blank, Value::Empty);
        assert_eq!(Value::Blank, liquid_value!(nil));
        assert_eq!(Value::Blank, liquid_value!(false));
        assert_ne!(Value::Blank, liquid_value!(true));
        assert_ne!(Value::Blank, liquid_value!(0));
        assert_ne!(Value::Blank, liquid_value!(1));
        assert_eq!(Value::Blank, liquid_value!(""));
        assert_eq!(Value::Blank, liquid_value!(" "));
        assert_eq!(Value::Blank, liquid_value!([]));
        assert_ne!(Value::Blank, liquid_value!([nil]));
        assert_eq!(Value::Blank, liquid_value!({}));
        assert_ne!(Value::Blank, liquid_value!({ "a": nil }));
    }
}