-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
38 lines (32 loc) · 1001 Bytes
/
build.rs
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
use rand::Rng;
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(*&^%$#@!~";
const PASSWORD_LEN: usize = 16;
fn rand_str() -> String {
let mut rng = rand::thread_rng();
let password: String = (0..PASSWORD_LEN)
.map(|_| {
let idx = rng.gen_range(0, CHARSET.len());
CHARSET[idx] as char
})
.collect();
password
}
type DynError = Box<dyn std::error::Error>;
fn main() -> Result<(), DynError> {
println!("cargo:rerun-if-changed=build.rs");
let secret_key = rand_str();
let out_path = PathBuf::from(env::var("OUT_DIR")?);
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(out_path.join("secret.rs"))?;
write!(file, "const SECRET_KEY: &[u8] = b\"{}\";", secret_key)?;
Ok(())
}