forked from swanandx/lemmeknow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
52 lines (45 loc) · 1.55 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::{env, fs, path::Path};
use fancy_regex::Regex;
use serde::Deserialize;
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
struct Data {
#[serde(rename(deserialize = "Name"))]
name: String,
#[serde(rename(deserialize = "Regex"))]
regex: String,
#[serde(skip_deserializing)]
boundaryless: String,
plural_name: bool,
#[serde(rename(deserialize = "Description"))]
description: Option<&'static str>,
#[serde(rename(deserialize = "Rarity"))]
rarity: f32,
#[serde(rename(deserialize = "URL"))]
url: Option<&'static str>,
#[serde(rename(deserialize = "Tags"))]
tags: Vec<&'static str>,
}
fn main() {
let mut data: Vec<Data> = serde_json::from_str(include_str!("./src/data/regex.json")).unwrap();
data.iter_mut().for_each(|d| {
d.boundaryless = Regex::new(r"(?<!\\)\^(?![^\[\]]*(?<!\\)\])")
.expect("can't compile for boundaryless")
.replace(&d.regex, "")
.to_string();
d.boundaryless = Regex::new(r"(?<!\\)\$(?![^\[\]]*(?<!\\)\])")
.expect("can't compile for boundaryless")
.replace(&d.boundaryless, "")
.to_string();
});
let mut out_data_str = format!("{:?}", data);
// we want reference to [], i.e. &[]
out_data_str = out_data_str.replace("tags: [", "tags: &[");
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("regex_data.rs");
fs::write(
&dest_path,
format!("const DATA: [Data; {}] = {};", data.len(), out_data_str),
)
.unwrap();
}