Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 1 | use crate::types; |
| 2 | |
David Tolnay | 397bd0b | 2019-02-15 20:51:10 -0800 | [diff] [blame^] | 3 | use serde::{Serialize, Deserialize}; |
| 4 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 5 | use std::collections::BTreeMap; |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame] | 6 | use std::fs::{self, File}; |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 7 | use std::io::prelude::*; |
| 8 | use std::path::Path; |
| 9 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame] | 10 | pub fn generate(defs: &types::Definitions) { |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 11 | let codegen_root = Path::new(env!("CARGO_MANIFEST_DIR")); |
| 12 | |
| 13 | let mut f = File::open(codegen_root.join("../Cargo.toml")).unwrap(); |
| 14 | let mut s = String::new(); |
| 15 | f.read_to_string(&mut s).unwrap(); |
| 16 | |
| 17 | let manifest: Manifest = toml::from_str(&s).unwrap(); |
| 18 | |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame] | 19 | let introspect = Introspect { |
| 20 | version: manifest.package.version, |
| 21 | types: defs.types.clone(), |
| 22 | tokens: defs.tokens.clone(), |
| 23 | }; |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 24 | |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame] | 25 | let j = serde_json::to_string_pretty(&introspect).unwrap(); |
| 26 | let check: Introspect = serde_json::from_str(&j).unwrap(); |
| 27 | assert_eq!(introspect, check); |
| 28 | |
| 29 | let json_path = codegen_root.join("../syn.json"); |
| 30 | fs::write(json_path, j).unwrap(); |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | #[derive(Debug, Deserialize)] |
| 34 | struct Manifest { |
| 35 | package: Package, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Deserialize)] |
| 39 | struct Package { |
| 40 | version: String, |
| 41 | } |
| 42 | |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame] | 43 | #[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 44 | struct Introspect { |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 45 | /// The `syn` version used to generate the introspection file |
David Tolnay | 07b2cca | 2019-02-15 18:58:27 -0800 | [diff] [blame] | 46 | version: String, |
| 47 | types: Vec<types::Node>, |
| 48 | tokens: BTreeMap<String, String>, |
Carl Lerche | 9a7d588 | 2019-02-15 12:27:04 -0800 | [diff] [blame] | 49 | } |