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
extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree};
use std::{env, fmt::Display, fs, path::PathBuf};
fn git_sbp_url(sub_uri: &impl ToString) -> String {
format!(
"https://raw.githubusercontent.com/devraymondsh/static-file-http-server/v{}/single-binary-producer/{}",
env::var("CARGO_PKG_VERSION").unwrap(),
sub_uri.to_string()
)
}
fn download_from_repo(sub_uri: &(impl ToString + Display)) -> String {
let contents = String::from_utf8_lossy(
&reqwest::blocking::get(git_sbp_url(sub_uri))
.unwrap()
.bytes()
.unwrap(),
)
.to_string();
escape_string::escape(contents.as_str()).to_string()
}
const MAIN_RS_ORIGINAL_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../single-binary-producer/src/main.rs"
);
const CARGO_TOML_ORIGINAL_PATH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../single-binary-producer/Cargo.toml"
);
fn get_main_rs_path() -> String {
if !PathBuf::from(MAIN_RS_ORIGINAL_PATH).exists() {
let main_rs_contents = fs::read_to_string(MAIN_RS_ORIGINAL_PATH).unwrap();
main_rs_contents
} else {
download_from_repo(&"src/main.rs")
}
}
fn get_cargo_toml_path() -> String {
if !PathBuf::from(CARGO_TOML_ORIGINAL_PATH).exists() {
let cargo_toml_contents = fs::read_to_string(CARGO_TOML_ORIGINAL_PATH).unwrap();
cargo_toml_contents
} else {
download_from_repo(&"Cargo.toml")
}
}
fn quote(name: impl ToString, contents: impl ToString) -> TokenStream {
format!(
"const {}: &str = r\"{}\";",
name.to_string(),
contents.to_string()
)
.parse()
.unwrap()
}
fn parse_input(input: TokenStream) -> String {
let name = match &input.into_iter().collect::<Vec<_>>()[0] {
TokenTree::Literal(lit) => {
let mut repr = lit.to_string();
if !repr.starts_with('"') || !repr.ends_with('"') {
panic!("This macro only accepts a single, non-empty string argument")
}
repr.remove(0);
repr.pop();
repr
}
_ => panic!("This macro only accepts a single, non-empty string argument"),
};
name
}
#[proc_macro]
pub fn get_sbp_main_rs(input: TokenStream) -> TokenStream {
let contents = get_main_rs_path();
let name = parse_input(input);
quote(name, contents)
}
#[proc_macro]
pub fn get_sbp_cargo_toml(input: TokenStream) -> TokenStream {
let contents = get_cargo_toml_path();
let name = parse_input(input);
quote(name, contents)
}