blob: 0fcca556fce5895f53b24eda7441e72b392c2b71 [file] [log] [blame]
David Tolnay14d463e2019-02-15 14:23:51 -08001use indexmap::IndexMap;
David Tolnay822790e2019-02-15 21:12:30 -08002use semver::Version;
David Tolnay10227122019-02-15 20:53:45 -08003use serde::{Deserialize, Deserializer, Serialize};
David Tolnay14d463e2019-02-15 14:23:51 -08004
David Tolnay440fe582019-02-15 20:23:14 -08005use std::collections::{BTreeMap, BTreeSet};
Carl Lerche058ff472019-02-13 16:23:52 -08006
David Tolnay4bc55232019-02-15 21:09:00 -08007#[derive(Debug, PartialEq, Serialize, Deserialize)]
David Tolnayf9bb8ff2019-02-15 13:10:14 -08008pub struct Definitions {
David Tolnay10227122019-02-15 20:53:45 -08009 /// The Syn version used to generate the introspection file.
David Tolnay822790e2019-02-15 21:12:30 -080010 pub version: Version,
David Tolnayf9bb8ff2019-02-15 13:10:14 -080011 pub types: Vec<Node>,
12 pub tokens: BTreeMap<String, String>,
13}
14
David Tolnay4bc55232019-02-15 21:09:00 -080015#[derive(Debug, PartialEq, Serialize, Deserialize)]
David Tolnayc2be7b22019-02-15 18:48:31 -080016pub struct Node {
17 pub ident: String,
18 pub features: Features,
19 #[serde(
20 flatten,
21 skip_serializing_if = "is_private",
David Tolnay6f1b7f22019-02-15 21:38:54 -080022 deserialize_with = "private_if_absent"
David Tolnayc2be7b22019-02-15 18:48:31 -080023 )]
24 pub data: Data,
Carl Lerche058ff472019-02-13 16:23:52 -080025}
26
David Tolnay4bc55232019-02-15 21:09:00 -080027#[derive(Debug, PartialEq, Serialize, Deserialize)]
David Tolnayc2be7b22019-02-15 18:48:31 -080028pub enum Data {
29 Private,
30 #[serde(rename = "fields")]
David Tolnay75c5a172019-02-15 20:35:41 -080031 Struct(Fields),
David Tolnayc2be7b22019-02-15 18:48:31 -080032 #[serde(rename = "variants")]
David Tolnay75c5a172019-02-15 20:35:41 -080033 Enum(Variants),
Carl Lerche058ff472019-02-13 16:23:52 -080034}
35
David Tolnay75c5a172019-02-15 20:35:41 -080036pub type Fields = IndexMap<String, Type>;
37pub type Variants = IndexMap<String, Vec<Type>>;
Carl Lerche058ff472019-02-13 16:23:52 -080038
David Tolnay4bc55232019-02-15 21:09:00 -080039#[derive(Debug, PartialEq, Serialize, Deserialize)]
David Tolnay295141b2019-02-15 12:45:33 -080040#[serde(rename_all = "lowercase")]
Carl Lerche058ff472019-02-13 16:23:52 -080041pub enum Type {
42 /// Type defined by `syn`
David Tolnayd3076572019-02-15 13:32:44 -080043 Syn(String),
Carl Lerche058ff472019-02-13 16:23:52 -080044
45 /// Type defined in `std`.
46 Std(String),
47
48 /// Type external to `syn`
David Tolnayd3076572019-02-15 13:32:44 -080049 #[serde(rename = "proc_macro2")]
Carl Lerche058ff472019-02-13 16:23:52 -080050 Ext(String),
51
52 /// Token type
David Tolnay157c7eb2019-02-15 13:21:48 -080053 Token(String),
Carl Lerche058ff472019-02-13 16:23:52 -080054
55 /// Token group
David Tolnay295141b2019-02-15 12:45:33 -080056 Group(String),
Carl Lerche058ff472019-02-13 16:23:52 -080057
58 /// Punctuated list
David Tolnay295141b2019-02-15 12:45:33 -080059 Punctuated(Punctuated),
60
Carl Lerche058ff472019-02-13 16:23:52 -080061 Option(Box<Type>),
62 Box(Box<Type>),
63 Vec(Box<Type>),
64 Tuple(Vec<Type>),
65}
66
David Tolnay4bc55232019-02-15 21:09:00 -080067#[derive(Debug, PartialEq, Serialize, Deserialize)]
David Tolnay295141b2019-02-15 12:45:33 -080068pub struct Punctuated {
David Tolnay485973a2019-02-15 14:42:48 -080069 pub element: Box<Type>,
70 pub punct: String,
David Tolnay295141b2019-02-15 12:45:33 -080071}
72
David Tolnay4bc55232019-02-15 21:09:00 -080073#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
Carl Lerche058ff472019-02-13 16:23:52 -080074pub struct Features {
David Tolnay440fe582019-02-15 20:23:14 -080075 pub any: BTreeSet<String>,
Carl Lerche058ff472019-02-13 16:23:52 -080076}
David Tolnayc2be7b22019-02-15 18:48:31 -080077
78fn is_private(data: &Data) -> bool {
79 match data {
80 Data::Private => true,
81 Data::Struct(_) | Data::Enum(_) => false,
82 }
83}
84
David Tolnay6f1b7f22019-02-15 21:38:54 -080085fn private_if_absent<'de, D>(deserializer: D) -> Result<Data, D::Error>
David Tolnayc2be7b22019-02-15 18:48:31 -080086where
87 D: Deserializer<'de>,
88{
89 let option = Option::deserialize(deserializer)?;
90 Ok(option.unwrap_or(Data::Private))
91}