Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 1 | use crate::types; |
| 2 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 3 | use std::collections::BTreeMap; |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame^] | 4 | use std::fs::{self, File}; |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 5 | use std::io::prelude::*; |
| 6 | use std::path::Path; |
| 7 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 8 | pub fn generate(defs: &types::Definitions) { |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 9 | 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 Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame^] | 17 | let introspect = Introspect { |
| 18 | version: manifest.package.version, |
| 19 | types: defs.types.clone(), |
| 20 | tokens: defs.tokens.clone(), |
| 21 | }; |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 22 | |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame^] | 23 | 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 Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | #[derive(Debug, Deserialize)] |
| 32 | struct Manifest { |
| 33 | package: Package, |
| 34 | } |
| 35 | |
| 36 | #[derive(Debug, Deserialize)] |
| 37 | struct Package { |
| 38 | version: String, |
| 39 | } |
| 40 | |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame^] | 41 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 42 | struct Introspect { |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 43 | /// The `syn` version used to generate the introspection file |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame^] | 44 | version: String, |
| 45 | types: Vec<types::Node>, |
| 46 | tokens: BTreeMap<String, String>, |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 47 | } |