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
//! Provides a generic scalar [`Value`] enum which supports collation.
//!
//! This library is part of Tinychain: [http://github.com/haydnv/tinychain](http://github.com/haydnv/tinychain)

use std::cmp::Ordering;

use bytes::Bytes;
use collate::{Collate, Collator};
use number_general::NumberCollator;

use tcgeneric::Instance;

pub use link::*;
pub use slice::*;
pub use value::*;

mod link;
mod slice;
mod value;

/// [`Collate`] support for [`Value`]
#[derive(Default, Clone)]
pub struct ValueCollator {
    bytes: Collator<Bytes>,
    link: Collator<Link>,
    number: NumberCollator,
    string: Collator<String>,
}

impl Collate for ValueCollator {
    type Value = Value;

    fn compare(&self, left: &Self::Value, right: &Self::Value) -> Ordering {
        match (left, right) {
            (Value::Bytes(l), Value::Bytes(r)) => self.bytes.compare(l, r),
            (Value::Link(l), Value::Link(r)) => self.link.compare(l, r),
            (Value::Number(l), Value::Number(r)) => self.number.compare(l, r),
            (Value::String(l), Value::String(r)) => self.string.compare(l, r),
            (Value::Tuple(l), Value::Tuple(r)) => self.compare_slice(l.as_slice(), r.as_slice()),
            (l, r) => l.class().cmp(&r.class()),
        }
    }
}