rust_fixed_point_decimal_core/
lib.rs

1// ---------------------------------------------------------------------------
2// Copyright:   (c) 2021 ff. Michael Amrhein (michael@adrhinum.de)
3// License:     This program is part of a larger application. For license
4//              details please read the file LICENSE.TXT provided together
5//              with the application.
6// ---------------------------------------------------------------------------
7// $Source: core/src/lib.rs $
8// $Revision: 2021-10-22T21:53:08+02:00 $
9
10mod parser;
11mod powers_of_ten;
12
13/// The maximum number of fractional decimal digits supported by `Decimal<P>`.
14pub const MAX_PREC: u8 = 9;
15
16use std::cmp::Ordering;
17
18pub use parser::{dec_repr_from_str, ParseDecimalError};
19pub use powers_of_ten::{checked_mul_pow_ten, mul_pow_ten, ten_pow};
20
21#[doc(hidden)]
22#[inline]
23pub fn adjust_prec(x: i128, p: u8, y: i128, q: u8) -> (i128, i128) {
24    match p.cmp(&q) {
25        Ordering::Equal => (x, y),
26        Ordering::Greater => (x, mul_pow_ten(y, p - q)),
27        Ordering::Less => (mul_pow_ten(x, q - p), y),
28    }
29}
30
31#[doc(hidden)]
32#[inline]
33pub fn checked_adjust_prec(
34    x: i128,
35    p: u8,
36    y: i128,
37    q: u8,
38) -> (Option<i128>, Option<i128>) {
39    match p.cmp(&q) {
40        Ordering::Equal => (Some(x), Some(y)),
41        Ordering::Greater => (Some(x), checked_mul_pow_ten(y, p - q)),
42        Ordering::Less => (checked_mul_pow_ten(x, q - p), Some(y)),
43    }
44}