summary refs log tree commit diff stats
path: root/tests/common/mod.rs
blob: 48153af2016d0e2b7c1e28b17d0eedda71599fbd (plain) (blame)
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
/*
 * This file is part of Datafu
 * Copyright (C) 2021  Soni L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::borrow::Borrow;
use std::borrow::Cow;
use std::collections::BTreeMap;

use datafu::RefOwn;
use datafu::PatternTypes;
use datafu::KVPair;

#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub enum Value {
    U(usize),
    B(bool),
    M(BTreeMap<Value, Value>),
    S(Cow<'static, str>),
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Dummy {
}

trait ValueHelper {
    fn as_value(&self) -> Result<&Value, &str>;
}

impl ValueHelper for Value {
    fn as_value(&self) -> Result<&Value, &str> {
        Ok(self)
    }
}

impl ValueHelper for &str {
    fn as_value(&self) -> Result<&Value, &str> {
        Err(self)
    }
}

impl PartialEq<str> for Value {
    fn eq(&self, other: &str) -> bool {
        matches!(self, Value::S(l) if l == other)
    }
}

impl PartialEq<Value> for str {
    fn eq(&self, other: &Value) -> bool {
        matches!(other, Value::S(r) if self == r)
    }
}

impl PartialEq<Dummy> for Value {
    fn eq(&self, other: &Dummy) -> bool {
        unreachable!()
    }
}

impl PartialEq<Value> for Dummy {
    fn eq(&self, other: &Value) -> bool {
        unreachable!()
    }
}

impl PartialEq<str> for Dummy {
    fn eq(&self, other: &str) -> bool {
        unreachable!()
    }
}

impl PartialEq<Dummy> for str {
    fn eq(&self, other: &Dummy) -> bool {
        unreachable!()
    }
}

impl<'a> PartialEq for dyn ValueHelper + 'a {
    fn eq(&self, other: &(dyn ValueHelper + 'a)) -> bool {
        match (self.as_value(), other.as_value()) {
            (a, b) if a == b => true,
            (Ok(Value::S(a)), Err(b)) | (Err(b), Ok(Value::S(a))) => {
                a.eq(b)
            },
            _ => false,
        }
    }
}

impl<'a> PartialOrd for dyn ValueHelper + 'a {
    fn partial_cmp(&self, other: &(dyn ValueHelper + 'a)) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a> Eq for dyn ValueHelper + 'a {
}

impl<'a> Ord for dyn ValueHelper + 'a {
    fn cmp(&self, other: &(dyn ValueHelper + 'a)) -> std::cmp::Ordering {
        match (self.as_value(), other.as_value()) {
            (Ok(a), Ok(b)) => a.cmp(b),
            (Err(a), Err(b)) => a.cmp(b),
            (Ok(Value::S(a)), Err(b)) => (**a).cmp(b),
            (Err(a), Ok(Value::S(b))) => a.cmp(b.borrow()),
            (Ok(_), Err(_)) => std::cmp::Ordering::Less,
            (Err(_), Ok(_)) => std::cmp::Ordering::Greater,
        }
    }
}

impl From<&'static str> for Value {
    fn from(x: &'static str) -> Value {
        Value::S(x.into())
    }
}


impl<'a> Borrow<dyn ValueHelper + 'a> for Value {
    fn borrow(&self) -> &(dyn ValueHelper + 'a) {
        self
    }
}

impl PatternTypes for Value {
    type Ref = Self;
    type Own = Dummy;

    fn pairs<'b>(
        item: RefOwn<'b, Self, Dummy>
    ) -> Option<Box<dyn Iterator<Item=KVPair<'b, Self>> + 'b>> {
        match item {
            RefOwn::Ref(Value::M(map)) => {
                Some(Box::new(map.iter().map(|(a, b)| (a.into(), b.into()))))
            },
            _ => None
        }
    }

    fn get<'a, 'b>(
        item: RefOwn<'b, Self, Dummy>,
        key: RefOwn<'a, Self, Dummy>
    ) -> Option<Option<KVPair<'b, Self>>> {
        match item {
            RefOwn::Ref(Value::M(map)) => {
                Some(match key {
                    RefOwn::Ref(key) => map.get_key_value(key),
                    RefOwn::Own(key) => unreachable!(),//map.get_key_value(&Value::U(key)),
                    RefOwn::Str(key) => map.get_key_value(&key as &dyn ValueHelper),
                }.map(|(k,v)| (k.into(), v.into())))
            },
            _ => None
        }
    }

    fn matches(
        left: RefOwn<'_, Self, Dummy>,
        right: RefOwn<'_, Self, Dummy>
    ) -> bool {
        left == right
    }
}