@@ -9,28 +9,33 @@ use std::{
9
9
use super :: { CurrencySymbol , TokenName , Value } ;
10
10
11
11
impl Value {
12
+ /// Create a Value containing only ada tokens, given the quantity in lovelace.
12
13
pub fn ada_value ( amount : & BigInt ) -> Self {
13
14
Self :: token_value ( & CurrencySymbol :: Ada , & TokenName :: ada ( ) , amount)
14
15
}
15
16
17
+ /// Create a Value containing only the given quantity of the given token.
16
18
pub fn token_value ( cs : & CurrencySymbol , tn : & TokenName , amount : & BigInt ) -> Self {
17
19
Value ( singleton ( (
18
20
cs. clone ( ) ,
19
21
singleton ( ( tn. clone ( ) , amount. clone ( ) ) ) ,
20
22
) ) )
21
23
}
22
24
25
+ /// Lookup the quantity of the given token.
23
26
pub fn get_token_amount ( & self , cs : & CurrencySymbol , tn : & TokenName ) -> BigInt {
24
27
self . 0
25
28
. get ( cs)
26
29
. and_then ( |tn_map| tn_map. get ( & tn) )
27
30
. map_or ( BigInt :: zero ( ) , Clone :: clone)
28
31
}
29
32
33
+ /// Lookup the quantity of ada(unit: lovelace).
30
34
pub fn get_ada_amount ( & self ) -> BigInt {
31
35
self . get_token_amount ( & CurrencySymbol :: Ada , & TokenName :: ada ( ) )
32
36
}
33
37
38
+ /// Insert a new token into the value, or replace the existing quantity.
34
39
pub fn insert_token ( & self , cs : & CurrencySymbol , tn : & TokenName , a : & BigInt ) -> Self {
35
40
let mut result_map = self . 0 . clone ( ) ;
36
41
@@ -49,21 +54,29 @@ impl Value {
49
54
Self ( result_map)
50
55
}
51
56
57
+ /// Return true if the value don't have any entries.
52
58
pub fn is_empty ( & self ) -> bool {
53
59
self . 0 . is_empty ( )
54
60
}
55
61
62
+ /// Remove all tokens whose quantity is zero.
56
63
pub fn normalize ( & self ) -> Self {
57
64
self . filter_map_amount ( |_, _, a| a. is_zero ( ) . not ( ) . then ( || a. clone ( ) ) )
58
65
}
59
66
67
+ /// Apply a function to each token of the value, and use its result as the new amount.
60
68
pub fn map_amount < F > ( & self , mut f : F ) -> Self
61
69
where
62
70
F : FnMut ( & CurrencySymbol , & TokenName , & BigInt ) -> BigInt ,
63
71
{
64
72
self . filter_map_amount ( |cs, tn, a| Some ( f ( cs, tn, a) ) )
65
73
}
66
74
75
+ /// Apply a function to each token of the value. If the result is None, the token entry will be
76
+ /// removed.
77
+ ///
78
+ /// Note that if the name-quantity map of any given currency symbols is empty, the currency entry
79
+ /// will be removed from the top-level map entirely.
67
80
pub fn filter_map_amount < F > ( & self , mut f : F ) -> Self
68
81
where
69
82
F : FnMut ( & CurrencySymbol , & TokenName , & BigInt ) -> Option < BigInt > ,
@@ -113,8 +126,9 @@ impl Add<&Value> for &Value {
113
126
114
127
fn add ( self , rhs : & Value ) -> Self :: Output {
115
128
Value ( union_b_tree_maps_with (
116
- |lhs, rhs| union_b_tree_maps_with ( |lhs, rhs| lhs + rhs, [ lhs, rhs] ) ,
117
- [ & self . 0 , & rhs. 0 ] ,
129
+ |lhs, rhs| union_b_tree_maps_with ( |lhs, rhs| lhs + rhs, lhs, rhs) ,
130
+ & self . 0 ,
131
+ & rhs. 0 ,
118
132
) )
119
133
}
120
134
}
@@ -128,8 +142,9 @@ impl Sub<&Value> for &Value {
128
142
129
143
fn sub ( self , rhs : & Value ) -> Self :: Output {
130
144
Value ( union_b_tree_maps_with (
131
- |lhs, rhs| union_b_tree_maps_with ( |lhs, rhs| lhs - rhs, [ lhs, rhs] ) ,
132
- [ & self . 0 , & rhs. 0 ] ,
145
+ |lhs, rhs| union_b_tree_maps_with ( |lhs, rhs| lhs - rhs, lhs, rhs) ,
146
+ & self . 0 ,
147
+ & rhs. 0 ,
133
148
) )
134
149
}
135
150
}
0 commit comments