Skip to content

Commit

Permalink
Merge pull request #343 from paupino/issue/316
Browse files Browse the repository at this point in the history
Allow reexportable macros via feature flags
  • Loading branch information
paupino committed Apr 16, 2021
2 parents e59749a + 711c0fb commit 4334b15
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ jobs:
with:
command: make
args: test-serde

- name: Run macro tests
uses: actions-rs/cargo@v1
with:
command: make
args: test-macros
6 changes: 5 additions & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ dependencies = [
"test-maths",
"test-misc",
"test-db",
"test-serde"
"test-serde",
"test-macros"
]

[tasks.test-db]
Expand All @@ -59,6 +60,9 @@ dependencies = [
"test-serde-arbitrary-precision-float"
]

[tasks.test-macros]
workspace = true

[tasks.test-no-std]
command = "cargo"
args = ["test", "--no-default-features"]
Expand Down
4 changes: 4 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ license = "MIT"
rust_decimal = { path = "..", version = "1.10.3" }
quote = "1.0"

[features]
default = []
reexportable = []

[lib]
proc-macro = true
13 changes: 13 additions & 0 deletions macros/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tasks.test-macros]
dependencies = [
"test-macros-default",
"test-macros-reexportable"
]

[tasks.test-macros-default]
command = "cargo"
args = ["test", "--tests"]

[tasks.test-macros-reexportable]
command = "cargo"
args = ["test", "--tests", "--features=reexportable"]
60 changes: 54 additions & 6 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
//!
//! A helpful macro for instantiating `Decimal` numbers.
//!
//! By default, this requires `rust_decimal` to be available at the project root. e.g. the macro
//! will effectively produce:
//!
//! ```ignore
//! ::rust_decimal::Decimal::from_parts(12345, 0, 0, false, 4)
//! ```
//!
//! While this is convenient for most use cases, it is sometimes not desired behavior when looking
//! to reexport the libray. Consequently, this behavior can be modified by enabling the feature
//! `reexportable`. When this feature is enabled, the macro will instead reproduce the functional
//! equivalent of:
//!
//! ```ignore
//! Decimal::from_parts(12345, 0, 0, false, 4)
//! ```
//!
//! # Examples
//!
//! ```rust
//! use rust_decimal_macros::dec;
//!
//! // If the reexportable feature is enabled, `Decimal` needs to be in scope
//! #[cfg(feature = "reexportable")]
//! use rust_decimal::Decimal;
//!
//! let number = dec!(1.2345);
//! assert_eq!("1.2345", number.to_string());
//! let number = dec!(-5.4321);
//! assert_eq!("-5.4321", number.to_string());
//! ```
//!

use core::str::FromStr;
use proc_macro::TokenStream;
use quote::quote;
Expand All @@ -19,14 +54,27 @@ pub fn dec(input: TokenStream) -> TokenStream {
};

let unpacked = decimal.unpack();
// We need to further unpack these for quote for now
let lo = unpacked.lo;
let mid = unpacked.mid;
let hi = unpacked.hi;
let negative = unpacked.is_negative;
let scale = unpacked.scale;
expand(
unpacked.lo,
unpacked.mid,
unpacked.hi,
unpacked.is_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()
}
4 changes: 4 additions & 0 deletions macros/tests/macro_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use rust_decimal_macros::dec;

// Require using for reexportable feature
#[cfg(feature = "reexportable")]
use rust_decimal::Decimal;

#[test]
fn it_can_parse_decimal() {
let tests = &[
Expand Down

0 comments on commit 4334b15

Please sign in to comment.