blob: d870a6458164d21b20c75bc9e28167d6d2fa23f4 [file] [log] [blame]
Carl Lerche9a7d5882019-02-15 12:27:04 -08001use crate::types;
2
David Tolnayf9bb8ff2019-02-15 13:10:14 -08003use std::collections::BTreeMap;
David Tolnay07b2cca2019-02-15 18:58:27 -08004use std::fs::{self, File};
Carl Lerche9a7d5882019-02-15 12:27:04 -08005use std::io::prelude::*;
6use std::path::Path;
7
David Tolnayf9bb8ff2019-02-15 13:10:14 -08008pub fn generate(defs: &types::Definitions) {
Carl Lerche9a7d5882019-02-15 12:27:04 -08009 let codegen_root = Path::new(env!("CARGO_MANIFEST_DIR"));
10
11 let mut f = File::open(codegen_root.join("../Cargo.toml")).unwrap();
12 let mut s = String::new();
13 f.read_to_string(&mut s).unwrap();
14
15 let manifest: Manifest = toml::from_str(&s).unwrap();
16
David Tolnay07b2cca2019-02-15 18:58:27 -080017 let introspect = Introspect {
18 version: manifest.package.version,
19 types: defs.types.clone(),
20 tokens: defs.tokens.clone(),
21 };
Carl Lerche9a7d5882019-02-15 12:27:04 -080022
David Tolnay07b2cca2019-02-15 18:58:27 -080023 let j = serde_json::to_string_pretty(&introspect).unwrap();
24 let check: Introspect = serde_json::from_str(&j).unwrap();
25 assert_eq!(introspect, check);
26
27 let json_path = codegen_root.join("../syn.json");
28 fs::write(json_path, j).unwrap();
Carl Lerche9a7d5882019-02-15 12:27:04 -080029}
30
31#[derive(Debug, Deserialize)]
32struct Manifest {
33 package: Package,
34}
35
36#[derive(Debug, Deserialize)]
37struct Package {
38 version: String,
39}
40
David Tolnay07b2cca2019-02-15 18:58:27 -080041#[derive(Debug, PartialEq, Serialize, Deserialize)]
42struct Introspect {
Carl Lerche9a7d5882019-02-15 12:27:04 -080043 /// The `syn` version used to generate the introspection file
David Tolnay07b2cca2019-02-15 18:58:27 -080044 version: String,
45 types: Vec<types::Node>,
46 tokens: BTreeMap<String, String>,
Carl Lerche9a7d5882019-02-15 12:27:04 -080047}