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
use {crate::data::Value, std::cmp::Ordering};

impl PartialEq<str> for Value {
    fn eq(&self, other: &str) -> bool {
        match (self, other) {
            (Value::Str(l), r) => l == r,
            _ => false,
        }
    }
}

impl PartialOrd<str> for Value {
    fn partial_cmp(&self, other: &str) -> Option<Ordering> {
        match (self, other) {
            (Value::Str(l), r) => {
                let l: &str = l.as_ref();
                Some(l.cmp(r))
            }
            _ => None,
        }
    }
}

impl<T: AsRef<str>> PartialEq<T> for Value {
    fn eq(&self, other: &T) -> bool {
        PartialEq::<str>::eq(self, other.as_ref())
    }
}

impl<T: AsRef<str>> PartialOrd<T> for Value {
    fn partial_cmp(&self, other: &T) -> Option<Ordering> {
        PartialOrd::<str>::partial_cmp(self, other.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use std::{borrow::Cow, cmp::Ordering};

    use crate::{data::Literal, prelude::Value};

    #[test]
    fn eq() {
        assert_eq!(Value::Str("wolf".to_owned()), "wolf");
        assert_eq!(Value::Str("wolf".to_owned()), "wolf".to_owned());
        assert_ne!(Value::I8(2), "2");

        assert_eq!(Literal::Text(Cow::Borrowed("fox")), "fox");
        assert_eq!(Literal::Text(Cow::Borrowed("fox")), "fox".to_owned());
        assert_ne!(Literal::Boolean(true), "true");
    }

    #[test]
    fn cmp() {
        macro_rules! text {
            ($text: expr) => {
                Value::Str($text.to_owned())
            };
        }
        assert_eq!(text!("wolf").partial_cmp("wolf"), Some(Ordering::Equal));
        assert_eq!(text!("apple").partial_cmp("wolf"), Some(Ordering::Less));
        assert_eq!(text!("zoo").partial_cmp("wolf"), Some(Ordering::Greater));
        assert_eq!(Value::I64(0).partial_cmp("0"), None);
    }
}