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
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;
use quote::quote;
use rust_decimal::Decimal;
use std::str::FromStr;
#[proc_macro_hack]
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();
let lo = unpacked.lo;
let mid = unpacked.mid;
let hi = unpacked.hi;
let negative = unpacked.is_negative;
let scale = unpacked.scale;
let expanded = quote! {
::rust_decimal::Decimal::from_parts(#lo, #mid, #hi, #negative, #scale)
};
expanded.into()
}