sidan_csl_rs/model/
value.rs

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
use serde::{Deserialize, Serialize};

use super::Asset;
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Value(pub HashMap<String, u64>);

impl Value {
    pub fn new() -> Self {
        Value(HashMap::new())
    }

    pub fn from_asset(asset: &Asset) -> Self {
        let mut asset_map = HashMap::new();
        asset_map.insert(
            Value::sanitize_unit(&asset.unit()),
            asset.quantity().parse::<u64>().unwrap(),
        );
        Value(asset_map)
    }

    pub fn from_asset_vec(assets: &[Asset]) -> Self {
        let mut asset_map = HashMap::new();
        for asset in assets {
            let current_value = asset_map
                .entry(Value::sanitize_unit(&asset.unit()))
                .or_insert(0);
            *current_value += asset.quantity().parse::<u64>().unwrap();
        }
        Value(asset_map)
    }

    pub fn add_asset(&mut self, unit: &str, quantity: u64) -> &mut Self {
        let current_value = self.0.entry(Value::sanitize_unit(unit)).or_insert(0);
        *current_value += quantity;
        self
    }

    pub fn add_assets(&mut self, assets: &[Asset]) -> &mut Self {
        for asset in assets {
            self.add_asset(&asset.unit(), asset.quantity().parse::<u64>().unwrap());
        }
        self
    }

    pub fn merge(&mut self, other: &Value) -> &mut Self {
        for (key, value) in other.0.clone() {
            let current_value = self.0.entry(Value::sanitize_unit(&key)).or_insert(0);
            *current_value += value;
        }
        self
    }

    pub fn negate_asset(&mut self, unit: &str, quantity: u64) -> &mut Self {
        let current_value = self.0.entry(Value::sanitize_unit(unit)).or_insert(0);
        if *current_value <= quantity {
            self.0.remove(unit);
        } else {
            *current_value -= quantity;
        };
        self
    }

    pub fn negate_assets(&mut self, other: &[Asset]) -> &mut Self {
        for asset in other {
            self.negate_asset(&asset.unit(), asset.quantity().parse::<u64>().unwrap());
        }
        self
    }

    pub fn negate_value(&mut self, other: &Value) -> &mut Self {
        for (key, value) in other.0.clone() {
            let unit = Value::sanitize_unit(&key);
            let current_value = self.0.entry(unit.clone()).or_insert(0);
            if *current_value <= value {
                self.0.remove(&unit);
            } else {
                *current_value -= value;
            }
        }
        self
    }

    pub fn to_asset_vec(&self) -> Vec<Asset> {
        let mut assets = vec![];
        for (unit, quantity) in &self.0 {
            assets.push(Asset::new(Value::sanitize_unit(unit), quantity.to_string()));
        }
        assets
    }

    // Accessor
    pub fn get(&self, key: &str) -> u64 {
        let key = if key.is_empty() { "lovelace" } else { key };
        match self.0.get(key) {
            Some(value) => *value,
            None => 0,
        }
    }

    pub fn keys(&self) -> Vec<String> {
        self.0.keys().cloned().collect()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    // Comparison function
    pub fn geq(&self, other: &Value) -> bool {
        for (key, value) in &other.0 {
            if self
                .0
                .get(&Value::sanitize_unit(key))
                .map_or(false, |v| v < value)
            {
                return false;
            }
        }
        true
    }

    pub fn leq(&self, other: &Value) -> bool {
        for (key, value) in &other.0 {
            if self
                .0
                .get(&Value::sanitize_unit(key))
                .map_or(false, |v| v > value)
            {
                return false;
            }
        }
        true
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn sanitize_unit(unit: &str) -> String {
        if unit.is_empty() {
            "lovelace".to_string()
        } else {
            unit.to_string()
        }
    }
}

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

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

    // Constructor tests

    // Operator tests
    #[test]
    fn test_add_asset() {
        let mut assets = Value::new();
        assets.0.insert("lovelace".to_string(), 50);
        assets.0.insert("asset1".to_string(), 60);
        assets.add_asset("lovelace", 100);
        assert_eq!(assets.0.get("lovelace").unwrap(), &150);
        assert_eq!(assets.0.get("asset1").unwrap(), &60);
    }

    #[test]
    fn test_add_assets() {
        let mut assets = Value::new();
        let other = vec![
            Asset::new_from_str("lovelace", "50"),
            Asset::new_from_str("asset1", "60"),
        ];
        assets.add_assets(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &50);
        assert_eq!(assets.0.get("asset1").unwrap(), &60);
    }

    #[test]
    fn test_merge_assets() {
        let mut assets = Value::new();
        let mut other = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        other.0.insert("lovelace".to_string(), 100);
        assets.merge(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &200);
    }

    #[test]
    fn test_merge_multiple_assets() {
        let mut assets = Value::new();
        let mut other = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        other.0.insert("lovelace".to_string(), 100);
        assets.0.insert("asset1".to_string(), 100);
        other.0.insert("asset2".to_string(), 50);
        assets.merge(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &200);
        assert_eq!(assets.0.get("asset1").unwrap(), &100);
        assert_eq!(assets.0.get("asset2").unwrap(), &50);
    }

    #[test]
    fn test_negate_asset() {
        let mut assets = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        assets.negate_asset("lovelace", 65);
        assert_eq!(assets.0.get("lovelace").unwrap(), &35);
    }

    #[test]
    fn test_negate_asset_to_zero() {
        let mut assets = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        assets.negate_asset("lovelace", 101);
        assert_eq!(assets.0.get("lovelace"), None);
    }

    #[test]
    fn test_negate_value() {
        let mut assets = Value::new();
        let mut other = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        other.0.insert("lovelace".to_string(), 65);
        assets.negate_value(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &35);
    }

    #[test]
    fn test_negate_assets() {
        let mut assets = Value::new();
        let other = vec![Asset::new_from_str("lovelace", "65")];
        assets.0.insert("lovelace".to_string(), 100);
        assets.negate_assets(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &35);
    }

    #[test]
    fn test_negate_value_to_zero() {
        let mut assets = Value::new();
        let mut other = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        other.0.insert("lovelace".to_string(), 101);
        assets.negate_value(&other);
        assert_eq!(assets.0.get("lovelace"), None);
    }

    #[test]
    fn test_negate_assets_to_zero() {
        let mut assets = Value::new();
        let other = vec![Asset::new_from_str("lovelace", "101")];
        assets.0.insert("lovelace".to_string(), 100);
        assets.negate_assets(&other);
        assert_eq!(assets.0.get("lovelace"), None);
    }

    #[test]
    fn test_negate_value_multiple() {
        let mut assets = Value::new();
        let mut other = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        other.0.insert("lovelace".to_string(), 65);
        assets.0.insert("asset1".to_string(), 100);
        other.0.insert("asset2".to_string(), 50);
        assets.negate_value(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &35);
        assert_eq!(assets.0.get("asset1").unwrap(), &100);
        assert_eq!(assets.0.get("asset2"), None);
    }

    #[test]
    fn test_negate_assets_multiple() {
        let mut assets = Value::new();
        let other = vec![
            Asset::new_from_str("lovelace", "65"),
            Asset::new_from_str("asset2", "50"),
        ];
        assets.0.insert("lovelace".to_string(), 100);
        assets.0.insert("asset1".to_string(), 100);
        assets.negate_assets(&other);
        assert_eq!(assets.0.get("lovelace").unwrap(), &35);
        assert_eq!(assets.0.get("asset1").unwrap(), &100);
        assert_eq!(assets.0.get("asset2"), None);
    }

    // Accessor tests
    #[test]
    fn test_get() {
        let mut assets = Value::new();
        assets.0.insert("lovelace".to_string(), 100);
        assert_eq!(assets.get("lovelace"), 100);
    }

    // Comparison function tests
    #[test]
    fn test_geq() {
        let mut first_assets = Value::new();
        first_assets
            .add_asset("lovelace", 1_012_760)
            .add_asset("asset1", 100);

        let mut second_assets = Value::new();
        second_assets
            .add_asset("lovelace", 1_000_000)
            .add_asset("asset1", 100);

        assert!(first_assets.geq(&second_assets));
    }

    #[test]
    fn test_leq() {
        let mut first_assets = Value::new();
        first_assets
            .add_asset("lovelace", 1_000_000)
            .add_asset("asset1", 100);

        let mut second_assets = Value::new();
        second_assets
            .add_asset("lovelace", 1_012_760)
            .add_asset("asset1", 100);

        assert!(first_assets.leq(&second_assets));
    }

    #[test]
    fn test_is_empty() {
        let assets = Value::new();
        assert!(assets.is_empty());
    }
}