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
use core::str::FromStr;
use proc_macro::TokenStream;
use quote::quote;
use rust_decimal::Decimal;
#[proc_macro]
pub fn dec(input: TokenStream) -> TokenStream {
let mut source = input.to_string();
if source.starts_with("- ") {
source.remove(1);
}
let decimal = match Decimal::from_str(&source[..]).or_else(|_| Decimal::from_scientific(&source[..])) {
Ok(d) => d,
Err(e) => panic!("Unexpected decimal format for {}: {}", source, e),
};
let unpacked = decimal.unpack();
expand(
unpacked.lo,
unpacked.mid,
unpacked.hi,
unpacked.negative,
unpacked.scale,
)
}
#[cfg(not(feature = "reexportable"))]
fn expand(lo: u32, mid: u32, hi: u32, negative: bool, scale: u32) -> TokenStream {
let expanded = quote! {
::rust_decimal::Decimal::from_parts(#lo, #mid, #hi, #negative, #scale)
};
expanded.into()
}
#[cfg(feature = "reexportable")]
fn expand(lo: u32, mid: u32, hi: u32, negative: bool, scale: u32) -> TokenStream {
let expanded = quote! {
Decimal::from_parts(#lo, #mid, #hi, #negative, #scale)
};
expanded.into()
}