Skip to main content

exint_macro/
lib.rs

1//! Exint Macros
2
3#![cfg_attr(feature = "proc_macro_diagnostic", feature(proc_macro_diagnostic))]
4#![allow(dead_code, reason = "feature-dependant")]
5
6use proc_macro::TokenStream;
7
8#[macro_use]
9mod backports;
10mod expanders;
11mod utilities;
12
13mod error;
14
15use expanders::uint;
16use expanders::uint::OutputFormat;
17use expanders::uint::TargetMode;
18
19// -----------------------------------------------------------------------------
20// Entrypoint
21// -----------------------------------------------------------------------------
22
23/// Create a generic integer from a literal expression.
24///
25/// # Examples
26///
27/// Basic usage:
28///
29/// ```
30/// exint::uint! {
31///   let a = 123_u24;
32///   let b = 456_u136;
33///   let c = 789_i80;
34/// }
35/// ```
36///
37/// You can also use binary/octal/hex:
38///
39/// ```
40/// exint::uint! {
41///   let a = 0b1010_u24;
42///   let b = 0xABCD_u136;
43///   let c = 0o3737_i80;
44/// }
45/// ```
46///
47/// Both forms are equivalent:
48///
49/// ```
50/// let a = exint::uint!(12345_u24);
51///
52/// exint::uint! {
53///   let a = 12345_u24;
54/// }
55/// ```
56#[proc_macro]
57pub fn uint(stream: TokenStream) -> TokenStream {
58  uint::expand(stream, TargetMode::IncludeStd, OutputFormat::ANY)
59}
60
61/// Similar to [`uint!`] but always generates code in big-endian format.
62#[proc_macro]
63pub fn uint_be(stream: TokenStream) -> TokenStream {
64  uint::expand(stream, TargetMode::IncludeStd, OutputFormat::BE)
65}
66
67/// Similar to [`uint!`] but always generates code in little-endian format.
68#[proc_macro]
69pub fn uint_le(stream: TokenStream) -> TokenStream {
70  uint::expand(stream, TargetMode::IncludeStd, OutputFormat::LE)
71}
72
73/// Similar to [`uint!`] but don't convert the built-in integer types (eg. `u8`, `u32`, `i64`).
74#[proc_macro]
75pub fn uint_strict(stream: TokenStream) -> TokenStream {
76  uint::expand(stream, TargetMode::ExcludeStd, OutputFormat::ANY)
77}
78
79/// Similar to [`uint_strict!`] but always generates code in big-endian format.
80#[proc_macro]
81pub fn uint_strict_be(stream: TokenStream) -> TokenStream {
82  uint::expand(stream, TargetMode::ExcludeStd, OutputFormat::BE)
83}
84
85/// Similar to [`uint_strict!`] but always generates code in little-endian format.
86#[proc_macro]
87pub fn uint_strict_le(stream: TokenStream) -> TokenStream {
88  uint::expand(stream, TargetMode::ExcludeStd, OutputFormat::LE)
89}