1 | use proc_macro2::TokenStream; |
2 | use quote::quote; |
3 | |
4 | #[derive (Clone, Debug)] |
5 | pub struct Protocol { |
6 | pub name: String, |
7 | pub copyright: Option<String>, |
8 | pub description: Option<(String, String)>, |
9 | pub interfaces: Vec<Interface>, |
10 | } |
11 | |
12 | impl Protocol { |
13 | pub fn new(name: String) -> Protocol { |
14 | Protocol { name, copyright: None, description: None, interfaces: Vec::new() } |
15 | } |
16 | } |
17 | |
18 | #[derive (Clone, Debug)] |
19 | pub struct Interface { |
20 | pub name: String, |
21 | pub version: u32, |
22 | pub description: Option<(String, String)>, |
23 | pub requests: Vec<Message>, |
24 | pub events: Vec<Message>, |
25 | pub enums: Vec<Enum>, |
26 | } |
27 | |
28 | impl Interface { |
29 | pub fn new() -> Interface { |
30 | Interface { |
31 | name: String::new(), |
32 | version: 1, |
33 | description: None, |
34 | requests: Vec::new(), |
35 | events: Vec::new(), |
36 | enums: Vec::new(), |
37 | } |
38 | } |
39 | } |
40 | |
41 | #[derive (Clone, Debug)] |
42 | pub struct Message { |
43 | pub name: String, |
44 | pub typ: Option<Type>, |
45 | pub since: u32, |
46 | pub description: Option<(String, String)>, |
47 | pub args: Vec<Arg>, |
48 | } |
49 | |
50 | impl Message { |
51 | pub fn new() -> Message { |
52 | Message { name: String::new(), typ: None, since: 1, description: None, args: Vec::new() } |
53 | } |
54 | |
55 | pub fn all_null(&self) -> bool { |
56 | self.args |
57 | .iter() |
58 | .all(|a: &Arg| !((a.typ == Type::Object || a.typ == Type::NewId) && a.interface.is_some())) |
59 | } |
60 | } |
61 | |
62 | #[derive (Clone, Debug)] |
63 | pub struct Arg { |
64 | pub name: String, |
65 | pub typ: Type, |
66 | pub interface: Option<String>, |
67 | pub summary: Option<String>, |
68 | pub description: Option<(String, String)>, |
69 | pub allow_null: bool, |
70 | pub enum_: Option<String>, |
71 | } |
72 | |
73 | impl Arg { |
74 | pub fn new() -> Arg { |
75 | Arg { |
76 | name: String::new(), |
77 | typ: Type::Object, |
78 | interface: None, |
79 | summary: None, |
80 | description: None, |
81 | allow_null: false, |
82 | enum_: None, |
83 | } |
84 | } |
85 | } |
86 | |
87 | #[derive (Clone, Debug)] |
88 | pub struct Enum { |
89 | pub name: String, |
90 | pub since: u16, |
91 | pub description: Option<(String, String)>, |
92 | pub entries: Vec<Entry>, |
93 | pub bitfield: bool, |
94 | } |
95 | |
96 | impl Enum { |
97 | pub fn new() -> Enum { |
98 | Enum { |
99 | name: String::new(), |
100 | since: 1, |
101 | description: None, |
102 | entries: Vec::new(), |
103 | bitfield: false, |
104 | } |
105 | } |
106 | } |
107 | |
108 | #[derive (Clone, Debug)] |
109 | pub struct Entry { |
110 | pub name: String, |
111 | pub value: u32, |
112 | pub since: u16, |
113 | pub description: Option<(String, String)>, |
114 | pub summary: Option<String>, |
115 | } |
116 | |
117 | impl Entry { |
118 | pub fn new() -> Entry { |
119 | Entry { name: String::new(), value: 0, since: 1, description: None, summary: None } |
120 | } |
121 | } |
122 | |
123 | #[derive (Debug, PartialEq, Eq, Copy, Clone)] |
124 | pub enum Type { |
125 | Int, |
126 | Uint, |
127 | Fixed, |
128 | String, |
129 | Object, |
130 | NewId, |
131 | Array, |
132 | Fd, |
133 | Destructor, |
134 | } |
135 | |
136 | impl Type { |
137 | pub fn nullable(self) -> bool { |
138 | matches!(self, Type::String | Type::Object) |
139 | } |
140 | |
141 | pub fn common_type(self) -> TokenStream { |
142 | match self { |
143 | Type::Int => quote!(Int), |
144 | Type::Uint => quote!(Uint), |
145 | Type::Fixed => quote!(Fixed), |
146 | Type::Array => quote!(Array), |
147 | Type::Fd => quote!(Fd), |
148 | Type::String => quote!(Str), |
149 | Type::Object => quote!(Object), |
150 | Type::NewId => quote!(NewId), |
151 | Type::Destructor => panic!("Destructor is not a valid argument type." ), |
152 | } |
153 | } |
154 | } |
155 | |