-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
45 lines (36 loc) · 1.43 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
use std::{env, path::PathBuf};
use reqwest::blocking::ClientBuilder;
use typify::{TypeSpace, TypeSpaceSettings};
const WEBHOOK_SCHEMA: &str = "https://unpkg.com/@octokit/webhooks-schemas/schema.json";
/// Download .json and apply typify.
fn main() {
let json = if env::var_os("DOCS_RS").is_some() {
let local_file = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap())
.join("src")
.join("temps_docs")
.join("schema.json");
let json: schemars::schema::RootSchema =
serde_json::from_str(&std::fs::read_to_string(local_file).expect("Failed")).unwrap();
json
} else {
let client = ClientBuilder::new()
.build()
.expect("Failed to create a client");
let json = client
.get(WEBHOOK_SCHEMA)
.send()
.expect("Failed")
.json::<schemars::schema::RootSchema>()
.expect("Failed");
json
};
let webhook_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("webhook.rs");
let mut type_space = TypeSpace::new(TypeSpaceSettings::default().with_struct_builder(true));
type_space.add_root_schema(json).unwrap();
let contents = format!(
"{}\n{}",
"use serde::{Deserialize, Serialize};",
prettyplease::unparse(&syn::parse2::<syn::File>(type_space.to_stream()).unwrap())
);
std::fs::write(webhook_path, contents).unwrap();
}