compile_ints/
lib.rs

1extern crate proc_macro;
2extern crate proc_macro2;
3extern crate quote;
4extern crate syn;
5
6use proc_macro::TokenStream;
7
8use std::time;
9
10use quote::quote;
11use syn::{parse_macro_input, Ident};
12
13/// Expands to the current UNIX time as of compilation.
14///
15/// By default, this provides the amount of seconds since the UNIX epoch.
16/// If given with `ms` or `s`, it will provide the amount of milliseconds or seconds
17/// since the UNIX epoch respectively.
18#[proc_macro]
19pub fn unix_time(item: TokenStream) -> TokenStream {
20    if item.is_empty() {
21        let t = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
22        let t = match t {
23            Ok(t) => t,
24            Err(e) => panic!("couldn't get duration since unix epoch: {}", e),
25        };
26        let t = t.as_secs();
27        return quote! { #t }.into();
28    }
29
30    let parsed: proc_macro2::Ident = parse_macro_input!(item as Ident);
31    let parsed = parsed.to_string();
32
33    let t = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
34    let t = match t {
35        Ok(t) => t,
36        Err(e) => panic!("couldn't get duration since unix epoch: {}", e),
37    };
38
39    match parsed.as_str() {
40        "millisecond" | "milliseconds" | "millis" | "milli" | "ms" => {
41            // it hopefully fits in 64 bits at the time of usage
42            let t = t.as_millis() as u64;
43            return quote! { #t }.into();
44        }
45        "second" | "seconds" | "secs" | "sec" | "s" => {
46            let t = t.as_secs();
47            return quote! { #t }.into();
48        }
49        e => panic!("unknown time unit: {}", e),
50    }
51}
52
53/// Expands to a random integer.
54/// 
55/// By default, this generates a random i32 integer.
56/// If given an integer or floating point type, it will generate for those
57/// such that it always fits. For a random within certain limits, use something
58/// akin to: `rand!() % 10` for 0-9, or `-(rand!() % 10) + (rand!() % 20)` for
59/// -10 to +10.
60///
61/// This requires the feature `rand`.
62#[proc_macro]
63#[cfg(feature = "rand")]
64pub fn rand(item: TokenStream) -> TokenStream {
65    if item.is_empty() {
66        let random = rand::random::<i32>();
67        return quote! { #random }.into();
68    }
69
70    let ty: proc_macro2::Ident = parse_macro_input!(item as Ident);
71    let ty = ty.to_string();
72
73    match ty.as_str() {
74        "u8" => {
75            let random = rand::random::<u8>();
76            return quote! { #random }.into();
77        }
78        "i8" => {
79            let random = rand::random::<i8>();
80            return quote! { #random }.into();
81        }
82        "u16" => {
83            let random = rand::random::<u16>();
84            return quote! { #random }.into();
85        }
86        "i16" => {
87            let random = rand::random::<i16>();
88            return quote! { #random }.into();
89        }
90        "u32" => {
91            let random = rand::random::<u32>();
92            return quote! { #random }.into();
93        }
94        "i32" => {
95            let random = rand::random::<i32>();
96            return quote! { #random }.into();
97        }
98        "u64" => {
99            let random = rand::random::<u64>();
100            return quote! { #random }.into();
101        }
102        "i64" => {
103            let random = rand::random::<i64>();
104            return quote! { #random }.into();
105        }
106        "u128" => {
107            let random = rand::random::<u128>();
108            return quote! { #random }.into();
109        }
110        "i128" => {
111            let random = rand::random::<i128>();
112            return quote! { #random }.into();
113        }
114        "f32" => {
115            let random = rand::random::<f32>();
116            return quote! { #random }.into();
117        }
118        "f64" => {
119            let random = rand::random::<f64>();
120            return quote! { #random }.into();
121        }
122        e => panic!("unknown type: {}", e),
123    }
124}