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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;

use std::time;

use quote::quote;
use syn::{parse_macro_input, Ident};

/// Expands to the current UNIX time as of compilation.
///
/// By default, this provides the amount of seconds since the UNIX epoch.
/// If given with `ms` or `s`, it will provide the amount of milliseconds or seconds
/// since the UNIX epoch respectively.
#[proc_macro]
pub fn unix_time(item: TokenStream) -> TokenStream {
    if item.is_empty() {
        let t = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
        let t = match t {
            Ok(t) => t,
            Err(e) => panic!("couldn't get duration since unix epoch: {}", e),
        };
        let t = t.as_secs();
        return quote! { #t }.into();
    }

    let parsed: proc_macro2::Ident = parse_macro_input!(item as Ident);
    let parsed = parsed.to_string();

    let t = time::SystemTime::now().duration_since(time::UNIX_EPOCH);
    let t = match t {
        Ok(t) => t,
        Err(e) => panic!("couldn't get duration since unix epoch: {}", e),
    };

    match parsed.as_str() {
        "millisecond" | "milliseconds" | "millis" | "milli" | "ms" => {
            // it hopefully fits in 64 bits at the time of usage
            let t = t.as_millis() as u64;
            return quote! { #t }.into();
        }
        "second" | "seconds" | "secs" | "sec" | "s" => {
            let t = t.as_secs();
            return quote! { #t }.into();
        }
        e => panic!("unknown time unit: {}", e),
    }
}

/// Expands to a random integer.
/// 
/// By default, this generates a random i32 integer.
/// If given an integer or floating point type, it will generate for those
/// such that it always fits. For a random within certain limits, use something
/// akin to: `rand!() % 10` for 0-9, or `-(rand!() % 10) + (rand!() % 20)` for
/// -10 to +10.
///
/// This requires the feature `rand`.
#[proc_macro]
#[cfg(feature = "rand")]
pub fn rand(item: TokenStream) -> TokenStream {
    if item.is_empty() {
        let random = rand::random::<i32>();
        return quote! { #random }.into();
    }

    let ty: proc_macro2::Ident = parse_macro_input!(item as Ident);
    let ty = ty.to_string();

    match ty.as_str() {
        "u8" => {
            let random = rand::random::<u8>();
            return quote! { #random }.into();
        }
        "i8" => {
            let random = rand::random::<i8>();
            return quote! { #random }.into();
        }
        "u16" => {
            let random = rand::random::<u16>();
            return quote! { #random }.into();
        }
        "i16" => {
            let random = rand::random::<i16>();
            return quote! { #random }.into();
        }
        "u32" => {
            let random = rand::random::<u32>();
            return quote! { #random }.into();
        }
        "i32" => {
            let random = rand::random::<i32>();
            return quote! { #random }.into();
        }
        "u64" => {
            let random = rand::random::<u64>();
            return quote! { #random }.into();
        }
        "i64" => {
            let random = rand::random::<i64>();
            return quote! { #random }.into();
        }
        "u128" => {
            let random = rand::random::<u128>();
            return quote! { #random }.into();
        }
        "i128" => {
            let random = rand::random::<i128>();
            return quote! { #random }.into();
        }
        "f32" => {
            let random = rand::random::<f32>();
            return quote! { #random }.into();
        }
        "f64" => {
            let random = rand::random::<f64>();
            return quote! { #random }.into();
        }
        e => panic!("unknown type: {}", e),
    }
}