| 1 |
#[allow(unused_extern_crates)] |
| 2 |
extern crate proc_macro; |
| 3 |
|
| 4 |
use proc_macro::*; |
| 5 |
use std::iter::once; |
| 6 |
mod span; |
| 7 |
use crate::span::{gen_random_bytes, gen_random}; |
| 8 |
|
| 9 |
|
| 10 |
/// Create a TokenStream of an identifier out of a string |
| 11 |
fn ident(ident: &str) -> TokenStream { |
| 12 |
TokenTree::from(Ident::new(ident, Span::call_site())).into() |
| 13 |
} |
| 14 |
|
| 15 |
#[proc_macro] |
| 16 |
pub fn const_random(input: TokenStream) -> TokenStream { |
| 17 |
match &input.to_string()[..] { |
| 18 |
"u8" => TokenTree::from(Literal::u8_suffixed(gen_random())).into(), |
| 19 |
"u16" => TokenTree::from(Literal::u16_suffixed(gen_random())).into(), |
| 20 |
"u32" => TokenTree::from(Literal::u32_suffixed(gen_random())).into(), |
| 21 |
"u64" => TokenTree::from(Literal::u64_suffixed(gen_random())).into(), |
| 22 |
"u128" => TokenTree::from(Literal::u128_suffixed(gen_random())).into(), |
| 23 |
"i8" => TokenTree::from(Literal::i8_suffixed(gen_random())).into(), |
| 24 |
"i16" => TokenTree::from(Literal::i16_suffixed(gen_random())).into(), |
| 25 |
"i32" => TokenTree::from(Literal::i32_suffixed(gen_random())).into(), |
| 26 |
"i64" => TokenTree::from(Literal::i64_suffixed(gen_random())).into(), |
| 27 |
"i128" => TokenTree::from(Literal::i128_suffixed(gen_random())).into(), |
| 28 |
"usize" => { |
| 29 |
let value: TokenStream = TokenTree::from(Literal::u128_suffixed(gen_random())).into(); |
| 30 |
let type_cast: TokenStream = [value, ident("as"), ident("usize")] |
| 31 |
.iter() |
| 32 |
.cloned() |
| 33 |
.collect(); |
| 34 |
TokenTree::from(Group::new(Delimiter::Parenthesis, type_cast)).into() |
| 35 |
} |
| 36 |
"isize" => { |
| 37 |
let value: TokenStream = TokenTree::from(Literal::i128_suffixed(gen_random())).into(); |
| 38 |
let type_cast: TokenStream = [value, ident("as"), ident("isize")] |
| 39 |
.iter() |
| 40 |
.cloned() |
| 41 |
.collect(); |
| 42 |
TokenTree::from(Group::new(Delimiter::Parenthesis, type_cast)).into() |
| 43 |
} |
| 44 |
byte_array if byte_array.starts_with("[u8 ; ") && byte_array.ends_with(']')=> { |
| 45 |
let len = byte_array[6..byte_array.len()-1].parse().unwrap(); |
| 46 |
let mut random_bytes = vec![0; len]; |
| 47 |
gen_random_bytes(&mut random_bytes); |
| 48 |
let array_parts: TokenStream = random_bytes.into_iter().flat_map(|byte| { |
| 49 |
let val = TokenTree::from(Literal::u8_suffixed(byte)); |
| 50 |
let comma = TokenTree::from(Punct::new(',', Spacing::Alone)); |
| 51 |
once(val).chain(once(comma)) |
| 52 |
}).collect(); |
| 53 |
TokenTree::from(Group::new(Delimiter::Bracket, array_parts)).into() |
| 54 |
} |
| 55 |
_ => panic!("Invalid type"), |
| 56 |
} |
| 57 |
} |