primitive_fixed_point_decimal 0.3.2

Primitive fixed-point decimal types.
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::fpdec_inner::FpdecInner;
use crate::ParseError;
use int_div_cum_error::{Rounding, checked_divide};
use num_traits::{Num, cast::FromPrimitive};
use std::fmt;


/// Static-precision fixed-point decimal.
///
/// `I` is the inner integer type, could be `i8`, `i16`, `i32`, `i64`, or `i128`.
///
/// `P` is the static precision.
///
/// For example, `StaticPrecFpdec<i64, 4>` means using `i64` as the inner
/// integer with about 18 significant digits, and having `4` fraction precision.
///
/// See [the module-level documentation](super) for more information.
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Default)]
pub struct StaticPrecFpdec<I, const P: i32>(pub(crate) I);

impl<I, const P: i32> StaticPrecFpdec<I, P>
where I: FpdecInner
{
    crate::none_prec_common::define_none_prec_common!();

    /// Checked multiplication. Computes `self * rhs`, returning `None` if
    /// overflow occurred.
    ///
    /// Equivalent to [`Self::checked_mul_ext`] with `Rounding::Round`.
    pub fn checked_mul<J, const Q: i32, const R: i32>(
        self,
        rhs: StaticPrecFpdec<J, Q>,
    ) -> Option<StaticPrecFpdec<I, R>>
        where J: FpdecInner
    {
        self.checked_mul_ext(rhs, Rounding::Round, None)
    }

    /// Checked multiplication. Computes `self * rhs`, returning `None` if
    /// overflow occurred.
    ///
    /// The type of `rhs` can have different inner integer `J` and precision `Q`
    /// with `self`. The type of result must have the same inner integer `I`
    /// while have different precision `R`.
    ///
    /// If the precision of the result's type `R` is less than the sum of
    /// precisions of the two multiplicands `P + Q`, then rounding operations
    /// are required and precision may be lost.
    /// You can specify the rounding type and cumulative error.
    ///
    /// See the [cumulative error section](index.html#cumulative-error)
    /// for more information and examples.
    ///
    /// # Examples
    /// 
    /// ```
    /// use primitive_fixed_point_decimal::{StaticPrecFpdec, Rounding, fpdec};
    /// type Balance = StaticPrecFpdec<i64, 2>;
    /// type FeeRate = StaticPrecFpdec<i16, 4>; // different types
    ///
    /// let balance: Balance = fpdec!(12.60);
    /// let rate: FeeRate = fpdec!(0.01);
    ///
    /// // calculate fee 3 times with same arguments, with `cum_error`.
    /// // but have different results: 0.13, 0.13 and 0.12
    /// let mut cum_error: i64 = 0;
    ///
    /// let fee: Balance = balance.checked_mul_ext(rate, Rounding::Ceiling, Some(&mut cum_error)).unwrap();
    /// assert_eq!(fee, fpdec!(0.13));
    ///
    /// let fee: Balance = balance.checked_mul_ext(rate, Rounding::Ceiling, Some(&mut cum_error)).unwrap();
    /// assert_eq!(fee, fpdec!(0.13));
    ///
    /// let fee: Balance = balance.checked_mul_ext(rate, Rounding::Ceiling, Some(&mut cum_error)).unwrap();
    /// assert_eq!(fee, fpdec!(0.12)); // here, different
    /// ```
    pub fn checked_mul_ext<J, const Q: i32, const R: i32>(
        self,
        rhs: StaticPrecFpdec<J, Q>,
        rounding: Rounding,
        cum_error: Option<&mut I>,
    ) -> Option<StaticPrecFpdec<I, R>>
        where J: FpdecInner
    {
        self.0.checked_mul_ext(I::from(rhs.0)?, P + Q - R, rounding, cum_error)
            .map(StaticPrecFpdec)
    }

    /// Checked division. Computes `self / rhs`, returning `None` if
    /// division by 0 or overflow occurred.
    ///
    /// Equivalent to [`Self::checked_div_ext`] with `Rounding::Round`.
    pub fn checked_div<J, const Q: i32, const R: i32>(
        self,
        rhs: StaticPrecFpdec<J, Q>,
    ) -> Option<StaticPrecFpdec<I, R>>
        where J: FpdecInner
    {
        self.checked_div_ext(rhs, Rounding::Round, None)
    }

    /// Checked division. Computes `self / rhs`, returning `None` if
    /// division by 0 or overflow occurred.
    ///
    /// The type of `rhs` can have different inner integer `J` and precision `Q`
    /// with `self`. The type of result must have the same inner integer `I`
    /// while have different precision `R`.
    ///
    /// You can specify the rounding type and cumulative error.
    /// See the [cumulative error section](index.html#cumulative-error)
    /// for more information and examples.
    ///
    /// # Examples
    /// 
    /// ```
    /// use primitive_fixed_point_decimal::{StaticPrecFpdec, Rounding, fpdec};
    /// type Balance = StaticPrecFpdec<i64, 2>;
    /// type FeeRate = StaticPrecFpdec<i16, 4>; // different types
    ///
    /// let rate: FeeRate = fpdec!(0.03);
    /// let fee: Balance = fpdec!(0.13);
    ///
    /// let balance: Balance = fee.checked_div_ext(rate, Rounding::Ceiling, None).unwrap();
    /// assert_eq!(balance, fpdec!(4.34));
    /// ```
    pub fn checked_div_ext<J, const Q: i32, const R: i32>(
        self,
        rhs: StaticPrecFpdec<J, Q>,
        rounding: Rounding,
        cum_error: Option<&mut I>,
    ) -> Option<StaticPrecFpdec<I, R>>
        where J: FpdecInner
    {
        self.0.checked_div_ext(I::from(rhs.0)?, P - Q - R, rounding, cum_error)
            .map(StaticPrecFpdec)
    }

    /// Shrink to a lower precision.
    ///
    /// Equivalent to [`Self::shrink_to_with_rounding`] with `Rounding::Round`.
    pub fn shrink_to(self, retain_precision: i32) -> Self {
        self.shrink_to_with_rounding(retain_precision, Rounding::Round)
    }

    /// Shrink to a lower precision.
    ///
    /// The `retain_precision` argument specifies the number of precision to be
    /// retained, rather than the number to be reduced.
    ///
    /// Examples:
    ///
    /// ```
    /// use primitive_fixed_point_decimal::{StaticPrecFpdec, Rounding, fpdec};
    /// type Price = StaticPrecFpdec<i64, 8>;
    ///
    /// let price: Price = fpdec!(12.12345678);
    ///
    /// assert_eq!(price.shrink_to(6), fpdec!(12.123457)); // Rounding::Round as default
    ///
    /// assert_eq!(price.shrink_to_with_rounding(6, Rounding::Floor), fpdec!(12.123456));
    /// ```
    pub fn shrink_to_with_rounding(
        self,
        retain_precision: i32,
        rounding: Rounding,
    ) -> Self {
        Self(self.0.shrink_with_rounding(P - retain_precision, rounding))
    }
}

impl<I, const P: i32> fmt::Debug for StaticPrecFpdec<I, P>
where I: fmt::Display
{
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "Fpdec({},{})", self.0, P)
    }
}

/// Format the decimal.
///
/// Examples:
///
/// ```
/// use primitive_fixed_point_decimal::{StaticPrecFpdec, ParseError};
/// type Decimal = StaticPrecFpdec<i16, 4>;
/// type BigPrec = StaticPrecFpdec<i16, 8>;
/// type NegPrec = StaticPrecFpdec<i16, -2>;
///
/// assert_eq!(format!("{}", Decimal::try_from(1.230).unwrap()), String::from("1.23"));
/// assert_eq!(format!("{}", Decimal::try_from(-1.230).unwrap()), String::from("-1.23")); // negative
/// assert_eq!(format!("{}", Decimal::try_from(-3.2768).unwrap()), String::from("-3.2768")); // i16::MIN
///
/// assert_eq!(format!("{}", BigPrec::try_from(0.00001230).unwrap()), String::from("0.0000123"));
/// assert_eq!(format!("{}", BigPrec::try_from(-0.00001230).unwrap()), String::from("-0.0000123"));
/// assert_eq!(format!("{}", BigPrec::try_from(-0.00032768).unwrap()), String::from("-0.00032768")); // i16::MIN
///
/// assert_eq!(format!("{}", NegPrec::try_from(12300).unwrap()), String::from("12300"));
/// assert_eq!(format!("{}", NegPrec::try_from(-12300).unwrap()), String::from("-12300"));
/// assert_eq!(format!("{}", NegPrec::try_from(-3276800).unwrap()), String::from("-3276800"));
/// ```
impl<I, const P: i32> fmt::Display for StaticPrecFpdec<I, P>
where I: FpdecInner + fmt::Display
{
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.0.display_fmt(P, f)
    }
}

/// Read decimal from string.
///
/// This method has 2 limitations:
/// 1. Support decimal format only but not scientific notation;
/// 2. Return `ParseError::Precision` if the string has more precision than `P`.
///
/// If you want to skip these limitations, you can parse the string
/// to float number first and then convert the number to this decimal.
///
/// Examples:
///
/// ```
/// use std::str::FromStr;
/// use primitive_fixed_point_decimal::{StaticPrecFpdec, ParseError};
/// type Decimal = StaticPrecFpdec<i16, 4>;
///
/// assert_eq!(Decimal::from_str("1.23"), Decimal::try_from(1.23));
/// assert_eq!(Decimal::from_str("9999"), Err(ParseError::Overflow));
/// assert_eq!(Decimal::from_str("1.23456"), Err(ParseError::Precision));
/// ```
impl<I, const P: i32> std::str::FromStr for StaticPrecFpdec<I, P>
where I: FpdecInner,
      ParseError: From<<I as Num>::FromStrRadixErr>
{
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, ParseError> {
        I::try_from_str(s, P).map(Self)
    }
}

macro_rules! convert_from_int {
    ($from_int_type:ty) => {
        impl<I, const P: i32> TryFrom<$from_int_type> for StaticPrecFpdec<I, P>
            where I: FpdecInner
        {
            type Error = ParseError;

            /// Convert from integer. Returning error if overflow occurred
            /// or lossing precision under `precision < 0`.
            ///
            /// Examples:
            ///
            /// ```
            /// use std::str::FromStr;
            /// use primitive_fixed_point_decimal::{StaticPrecFpdec, ParseError};
            /// type Decimal = StaticPrecFpdec<i32, 4>;
            /// type NegPrec = StaticPrecFpdec<i32, -4>;
            ///
            /// assert_eq!(Decimal::try_from(123).unwrap(), Decimal::from_str("123").unwrap());
            /// assert_eq!(Decimal::try_from(9999999), Err(ParseError::Overflow));
            /// assert_eq!(NegPrec::try_from(123), Err(ParseError::Precision));
            /// ```
            fn try_from(i: $from_int_type) -> Result<Self, Self::Error> {
                let i2 = <$from_int_type>::checked_from_int(i, P)?;
                I::from(i2).ok_or(ParseError::Overflow).map(Self)
            }
        }
    }
}
convert_from_int!(i8);
convert_from_int!(i16);
convert_from_int!(i32);
convert_from_int!(i64);
convert_from_int!(i128);

macro_rules! convert_from_float {
    ($float_type:ty, $from_fn:ident, $to_fn:ident) => {
        impl<I, const P: i32> TryFrom<$float_type> for StaticPrecFpdec<I, P>
            where I: FromPrimitive + FpdecInner
        {
            type Error = ParseError;

            /// Convert from float type. Returning error if overflow occurred.
            ///
            /// Since it's hard for the float types to represent decimal fraction
            /// exactly, so this method always rounds the float number into
            /// StaticPrecFpdec.
            ///
            /// Examples:
            ///
            /// ```
            /// use std::str::FromStr;
            /// use primitive_fixed_point_decimal::{StaticPrecFpdec, ParseError};
            /// type Decimal = StaticPrecFpdec<i32, 4>;
            ///
            /// assert_eq!(Decimal::try_from(1.23).unwrap(), Decimal::from_str("1.23").unwrap());
            /// assert_eq!(Decimal::try_from(1.23456789).unwrap(), Decimal::from_str("1.2346").unwrap());
            /// ```
            fn try_from(f: $float_type) -> Result<Self, Self::Error> {
                let base: $float_type = 10.0;
                let inner_f = f * base.powi(P) as $float_type;
                I::$from_fn(inner_f.round())
                    .map(Self)
                    .ok_or(ParseError::Overflow)
            }
        }

        impl<I, const P: i32> From<StaticPrecFpdec<I, P>> for $float_type
            where I: FpdecInner
        {
            /// Convert into float type.
            ///
            /// Examples:
            ///
            /// ```
            /// use std::str::FromStr;
            /// use primitive_fixed_point_decimal::{StaticPrecFpdec, ParseError, fpdec};
            /// type Decimal = StaticPrecFpdec<i32, 4>;
            ///
            /// let dec: Decimal = fpdec!(1.23);
            /// let f: f32 = dec.into();
            /// assert_eq!(f, 1.23);
            /// ```
            fn from(dec: StaticPrecFpdec<I, P>) -> Self {
                let base: $float_type = 10.0;
                dec.0.$to_fn().unwrap() / base.powi(P)
            }
        }
    }
}

convert_from_float!(f32, from_f32, to_f32);
convert_from_float!(f64, from_f64, to_f64);

impl<I, const P: i32> std::ops::Neg for StaticPrecFpdec<I, P>
where I: FpdecInner
{
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self(-self.0)
    }
}

impl<I, const P: i32> std::ops::Add for StaticPrecFpdec<I, P>
where I: FpdecInner
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl<I, const P: i32> std::ops::Sub for StaticPrecFpdec<I, P>
where I: FpdecInner
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0 - rhs.0)
    }
}

impl<I, const P: i32> std::ops::AddAssign for StaticPrecFpdec<I, P>
where I: FpdecInner
{
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
    }
}

impl<I, const P: i32> std::ops::SubAssign for StaticPrecFpdec<I, P>
where I: FpdecInner
{
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
    }
}


#[cfg(feature="serde")]
use serde::{Serialize, Deserialize, Serializer, Deserializer};

#[cfg(feature="serde")]
impl<I, const P: i32> Serialize for StaticPrecFpdec<I, P>
where I: FpdecInner + fmt::Display
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        // XXX how to selete dump type?
        if serializer.is_human_readable() {
            serializer.collect_str(self)
        } else {
            Into::<f64>::into(*self).serialize(serializer)
        }
    }
}

#[cfg(feature="serde")]
impl<'de, I, const P: i32> Deserialize<'de> for StaticPrecFpdec<I, P>
where I: FromPrimitive + FpdecInner,
      ParseError: From<<I as Num>::FromStrRadixErr>
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        use serde::de::{self, Visitor};
        use std::str::FromStr;
        use std::marker::PhantomData;

        struct StaticPrecFpdecVistor<I, const P: i32>(PhantomData<I>);

        macro_rules! visit_num {
            ($func_name:ident, $num_type:ty) => {
                fn $func_name<E: de::Error>(self, n: $num_type) -> Result<Self::Value, E> {
                    StaticPrecFpdec::try_from(n)
                        .map_err(|_| E::custom("decimal overflow"))
                }
            }
        }

        impl<'de, I, const P: i32> Visitor<'de> for StaticPrecFpdecVistor<I, P>
        where I: FromPrimitive + FpdecInner,
              ParseError: From<<I as Num>::FromStrRadixErr>
        {
            type Value = StaticPrecFpdec<I, P>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                write!(formatter, "decimal")
            }

            fn visit_str<E: de::Error>(self, s: &str) -> Result<Self::Value, E> {
                StaticPrecFpdec::from_str(s)
                    .map_err(|e| E::custom(format!("decimal {:?}", e)))
            }

            visit_num!(visit_f32, f32);
            visit_num!(visit_f64, f64);
            visit_num!(visit_i8, i8);
            visit_num!(visit_i16, i16);
            visit_num!(visit_i32, i32);
            visit_num!(visit_i64, i64);
            visit_num!(visit_i128, i128);
        }

        deserializer.deserialize_any(StaticPrecFpdecVistor(PhantomData))
    }
}