@@ -27,39 +27,39 @@ pub struct Price(NonZeroI64);
27
27
28
28
impl Price {
29
29
pub fn from_integer ( value : i64 , exponent : i16 ) -> Result < Price , PriceError > {
30
- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
30
+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
31
31
ExponentFactor :: Mul ( coef) => value. checked_mul ( coef) . ok_or ( PriceError :: Overflow ) ?,
32
32
ExponentFactor :: Div ( coef) => value. checked_div ( coef) . ok_or ( PriceError :: Overflow ) ?,
33
33
} ;
34
- let value = NonZeroI64 :: new ( value ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?;
35
- Ok ( Self ( value ) )
34
+ let mantissa = NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?;
35
+ Ok ( Self ( mantissa ) )
36
36
}
37
37
38
38
pub fn parse_str ( value : & str , exponent : i16 ) -> Result < Price , PriceError > {
39
39
let value: Decimal = value. parse ( ) ?;
40
- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
40
+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
41
41
ExponentFactor :: Mul ( coef) => value
42
42
. checked_mul ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
43
43
. ok_or ( PriceError :: Overflow ) ?,
44
44
ExponentFactor :: Div ( coef) => value
45
45
. checked_div ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
46
46
. ok_or ( PriceError :: Overflow ) ?,
47
47
} ;
48
- if !value . is_integer ( ) {
48
+ if !mantissa . is_integer ( ) {
49
49
return Err ( PriceError :: TooPrecise ) ;
50
50
}
51
- let value : i64 = value . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
52
- let value = NonZeroI64 :: new ( value ) . ok_or ( PriceError :: Overflow ) ?;
53
- Ok ( Self ( value ) )
51
+ let mantissa : i64 = mantissa . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
52
+ let mantissa = NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: Overflow ) ?;
53
+ Ok ( Self ( mantissa ) )
54
54
}
55
55
56
56
pub const fn from_nonzero_mantissa ( mantissa : NonZeroI64 ) -> Self {
57
57
Self ( mantissa)
58
58
}
59
59
60
60
pub const fn from_mantissa ( mantissa : i64 ) -> Result < Self , PriceError > {
61
- if let Some ( value ) = NonZeroI64 :: new ( mantissa) {
62
- Ok ( Self ( value ) )
61
+ if let Some ( mantissa ) = NonZeroI64 :: new ( mantissa) {
62
+ Ok ( Self ( mantissa ) )
63
63
} else {
64
64
Err ( PriceError :: ZeroPriceUnsupported )
65
65
}
@@ -75,81 +75,86 @@ impl Price {
75
75
76
76
pub fn to_f64 ( self , exponent : i16 ) -> Result < f64 , PriceError > {
77
77
match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
78
- // Mul/div is reversed for this conversion
78
+ // Mul/div is reversed for converting mantissa to value
79
79
ExponentFactor :: Mul ( coef) => Ok ( self . 0 . get ( ) as f64 / coef as f64 ) ,
80
80
ExponentFactor :: Div ( coef) => Ok ( self . 0 . get ( ) as f64 * coef as f64 ) ,
81
81
}
82
82
}
83
83
84
84
pub fn from_f64 ( value : f64 , exponent : i16 ) -> Result < Self , PriceError > {
85
85
let value = Decimal :: from_f64 ( value) . ok_or ( PriceError :: Overflow ) ?;
86
- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
86
+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
87
87
ExponentFactor :: Mul ( coef) => value
88
88
. checked_mul ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
89
89
. ok_or ( PriceError :: Overflow ) ?,
90
90
ExponentFactor :: Div ( coef) => value
91
91
. checked_div ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
92
92
. ok_or ( PriceError :: Overflow ) ?,
93
93
} ;
94
- let value : i64 = value . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
94
+ let mantissa : i64 = mantissa . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
95
95
Ok ( Self (
96
- NonZeroI64 :: new ( value ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?,
96
+ NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?,
97
97
) )
98
98
}
99
99
100
- pub fn add_with_same_mantissa ( self , other : Price ) -> Result < Self , PriceError > {
101
- let value = self
100
+ pub fn add_with_same_exponent ( self , other : Price ) -> Result < Self , PriceError > {
101
+ let mantissa = self
102
102
. 0
103
103
. get ( )
104
104
. checked_add ( other. 0 . get ( ) )
105
105
. ok_or ( PriceError :: Overflow ) ?;
106
- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
106
+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
107
107
}
108
108
109
- pub fn sub_with_same_mantissa ( self , other : Price ) -> Result < Self , PriceError > {
110
- let value = self
109
+ pub fn sub_with_same_exponent ( self , other : Price ) -> Result < Self , PriceError > {
110
+ let mantissa = self
111
111
. 0
112
112
. get ( )
113
113
. checked_sub ( other. 0 . get ( ) )
114
114
. ok_or ( PriceError :: Overflow ) ?;
115
- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
115
+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
116
116
}
117
117
118
118
pub fn mul_integer ( self , factor : i64 ) -> Result < Self , PriceError > {
119
- let value = self
119
+ let mantissa = self
120
120
. 0
121
121
. get ( )
122
122
. checked_mul ( factor)
123
123
. ok_or ( PriceError :: Overflow ) ?;
124
- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
124
+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
125
125
}
126
126
127
127
pub fn div_integer ( self , factor : i64 ) -> Result < Self , PriceError > {
128
- let value = self
128
+ let mantissa = self
129
129
. 0
130
130
. get ( )
131
131
. checked_div ( factor)
132
132
. ok_or ( PriceError :: Overflow ) ?;
133
- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
133
+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
134
134
}
135
135
136
- pub fn mul_decimal ( self , mantissa : i64 , rhs_exponent : i16 ) -> Result < Self , PriceError > {
137
- let left_value = i128:: from ( self . 0 . get ( ) ) ;
138
- let right_value = i128:: from ( mantissa) ;
136
+ pub fn mul_decimal ( self , mantissa : i64 , exponent : i16 ) -> Result < Self , PriceError > {
137
+ let left_mantissa = i128:: from ( self . 0 . get ( ) ) ;
138
+ let right_mantissa = i128:: from ( mantissa) ;
139
139
140
- let value = left_value
141
- . checked_mul ( right_value)
140
+ // multiplied_mantissas = left_mantissa * right_mantissa
141
+ let multiplied_mantissas = left_mantissa
142
+ . checked_mul ( right_mantissa)
142
143
. ok_or ( PriceError :: Overflow ) ?;
143
144
144
- let value = match ExponentFactor :: get ( rhs_exponent) . ok_or ( PriceError :: Overflow ) ? {
145
- ExponentFactor :: Mul ( coef) => {
146
- value. checked_div ( coef. into ( ) ) . ok_or ( PriceError :: Overflow ) ?
147
- }
148
- ExponentFactor :: Div ( coef) => {
149
- value. checked_mul ( coef. into ( ) ) . ok_or ( PriceError :: Overflow ) ?
150
- }
145
+ // result_mantissa = left_mantissa * right_mantissa * 10^exponent
146
+ // Mul/div is reversed for multiplying 10^exponent
147
+ let result_mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
148
+ ExponentFactor :: Mul ( coef) => multiplied_mantissas
149
+ . checked_div ( coef. into ( ) )
150
+ . ok_or ( PriceError :: Overflow ) ?,
151
+ ExponentFactor :: Div ( coef) => multiplied_mantissas
152
+ . checked_mul ( coef. into ( ) )
153
+ . ok_or ( PriceError :: Overflow ) ?,
151
154
} ;
152
- let value: i64 = value. try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
153
- Self :: from_mantissa ( value) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
155
+ let result_mantissa: i64 = result_mantissa
156
+ . try_into ( )
157
+ . map_err ( |_| PriceError :: Overflow ) ?;
158
+ Self :: from_mantissa ( result_mantissa) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
154
159
}
155
160
}
0 commit comments