radix_common_derive/lib.rs
1use proc_macro::TokenStream;
2
3#[cfg(not(panic = "unwind"))]
4compile_error!("The `catch_unwind` crate requires that `panic = \"unwind\"` in the profile");
5
6mod unwind;
7use unwind::handle_catch_unwind;
8
9mod decimal;
10use decimal::{to_decimal, to_precise_decimal};
11
12#[proc_macro_attribute]
13pub fn catch_unwind(metadata: TokenStream, input: TokenStream) -> TokenStream {
14 handle_catch_unwind(metadata.into(), input.into())
15 .unwrap_or_else(|err| err.to_compile_error())
16 .into()
17}
18
19#[proc_macro_attribute]
20pub fn ignore(_: TokenStream, input: TokenStream) -> TokenStream {
21 input
22}
23
24/// Creates a `Decimal` from literals.
25/// It is a compile-time macro. It allows to declare constants and statics.
26/// Example:
27/// const D1: Decimal = dec!("1111.11111")
28/// const D2: Decimal = dec!("-1111.11111")
29/// const D3: Decimal = dec!(1)
30/// const D4: Decimal = dec!(-1_0000_000_u128)
31///
32// NOTE: Decimal arithmetic operation safe unwrap.
33// In general, it is assumed that reasonable literals are provided.
34// If not then something is definitely wrong and panic is fine.
35#[proc_macro]
36pub fn dec(input: TokenStream) -> TokenStream {
37 to_decimal(input).unwrap_or_else(|err| err.to_compile_error().into())
38}
39
40/// Creates a `PreciseDecimal` from literals.
41/// It is a compile-time macro. It allows to declare constants and statics.
42/// Example:
43/// const D1: PreciseDecimal = pdec!("1111.11111")
44/// const D2: PreciseDecimal = pdec!("-1111.11111")
45/// const D3: PreciseDecimal = pdec!(1)
46/// const D4: PreciseDecimal = pdec!(-1_0000_000_u128)
47///
48// NOTE: Decimal arithmetic operation safe unwrap.
49// In general, it is assumed that reasonable literals are provided.
50// If not then something is definitely wrong and panic is fine.
51#[proc_macro]
52pub fn pdec(input: TokenStream) -> TokenStream {
53 to_precise_decimal(input).unwrap_or_else(|err| err.to_compile_error().into())
54}