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
#![feature(proc_macro)]
extern crate rust_decimal;
extern crate proc_macro;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;
use rust_decimal::Decimal;
use std::str::FromStr;

#[proc_macro]
pub fn dec(input: TokenStream) -> TokenStream {
    let mut source = input.to_string();

    // If it starts with `- ` then get rid of the extra space
    // to_string will put a space between tokens
    if source.starts_with("- ") {
        source.remove(1);
    }
    
    let decimal = match Decimal::from_str(&source[..]) {
        Ok(d) => d,
        Err(e) => panic!("Unexpected decimal format for {}: {}", source, e),
    };

    let bytes = decimal.serialize();
    quote!(::rust_decimal::Decimal::deserialize(#bytes)).parse().unwrap()
}