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
extern crate proc_macro;
#[macro_use]
extern crate proc_macro_hack;
extern crate data_encoding;
extern crate dotenv;
use std::io::Read;
use data_encoding::decode::Error;

fn helper<F>(input: &str, decoder: F)
    -> String
    where F: Fn(&[u8]) -> Result<Vec<u8>, Error>
{
    let input = input.trim_matches('"');

    let byte_vec = if input.starts_with("file:") {

        let mut file = std::fs::File::open(&input[5..]).expect("Error opening file");
        let mut contents = String::new();
        file.read_to_string(&mut contents).expect("Error reading file");
        decoder(contents.trim().as_bytes()).expect("Parse error")

    } else if input.starts_with("env:") {

        dotenv::dotenv().ok();
        let var = std::env::var(&input[4..]).expect("Error reading environment variable");
        decoder(var.as_bytes()).expect("Parse error")

    } else {

        decoder(input.as_bytes()).expect("Parse error")

    };

    format!("{{static _BIN: [u8; {}] = {:?}; &_BIN}}", byte_vec.len(), byte_vec)
}

proc_macro_expr_impl! {
    pub fn base2_impl(input: &str) -> String {
        helper(input, data_encoding::base2::decode)
    }
    pub fn base4_impl(input: &str) -> String {
        helper(input, data_encoding::base4::decode)
    }
    pub fn base8_impl(input: &str) -> String {
        helper(input, data_encoding::base8::decode)
    }
    pub fn base16_impl(input: &str) -> String {
        helper(input, data_encoding::base16::decode)
    }
    pub fn base32hex_impl(input: &str) -> String {
        helper(input, data_encoding::base32hex::decode)
    }
    pub fn base32_impl(input: &str) -> String {
        helper(input, data_encoding::base32::decode)
    }
    pub fn base64_impl(input: &str) -> String {
        helper(input, data_encoding::base64::decode)
    }
    pub fn base64url_impl(input: &str) -> String {
        helper(input, data_encoding::base64url::decode)
    }


    pub fn base2_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base2::decode_nopad)
    }
    pub fn base4_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base4::decode_nopad)
    }
    pub fn base8_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base8::decode_nopad)
    }
    pub fn base16_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base16::decode_nopad)
    }
    pub fn base32hex_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base32hex::decode_nopad)
    }
    pub fn base32_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base32::decode_nopad)
    }
    pub fn base64_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base64::decode_nopad)
    }
    pub fn base64url_nopad_impl(input: &str) -> String {
        helper(input, data_encoding::base64url::decode_nopad)
    }
}