1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#[derive(Debug)]
pub struct Protocol {
pub name: String,
pub copyright: Option<String>,
pub description: Option<(String, String)>,
pub interfaces: Vec<Interface>
}
impl Protocol {
pub fn new(name: String) -> Protocol {
Protocol {
name: name,
copyright: None,
description: None,
interfaces: Vec::new()
}
}
}
#[derive(Debug)]
pub struct Interface {
pub name: String,
pub version: u32,
pub description: Option<(String, String)>,
pub requests: Vec<Message>,
pub events: Vec<Message>,
pub enums: Vec<Enum>
}
impl Interface {
pub fn new() -> Interface {
Interface {
name: String::new(),
version: 1,
description: None,
requests: Vec::new(),
events: Vec::new(),
enums: Vec::new()
}
}
pub fn destructor_sanitize(&self) -> Result<bool, String> {
let mut found_destructor = false;
for req in &self.requests {
if req.name == "destroy" {
if let Some(Type::Destructor) = req.typ {
} else {
return Err(format!(
"Request '{}.destroy' is not a destructor.",
self.name
))
}
}
if let Some(Type::Destructor) = req.typ {
if req.args.len() > 0 {
return Err(format!(
"Destructor request '{}.{}' cannot take arguments.",
self.name, req.name
))
}
if found_destructor {
return Err(format!(
"Interface {} has more than one destructor.",
self.name
))
}
found_destructor = true
}
}
Ok(found_destructor)
}
}
#[derive(Debug)]
pub struct Message {
pub name: String,
pub typ: Option<Type>,
pub since: u16,
pub description: Option<(String, String)>,
pub args: Vec<Arg>,
pub type_index: usize
}
impl Message {
pub fn new() -> Message {
Message {
name: String::new(),
typ: None,
since: 1,
description: None,
args: Vec::new(),
type_index: 0
}
}
pub fn all_null(&self) -> bool {
self.args.iter().all(|a| !((a.typ == Type::Object || a.typ == Type::NewId) && a.interface.is_some()))
}
}
#[derive(Debug)]
pub struct Arg {
pub name: String,
pub typ: Type,
pub interface: Option<String>,
pub summary: Option<String>,
pub description: Option<(String, String)>,
pub allow_null: bool,
pub enum_: Option<String>
}
impl Arg {
pub fn new() -> Arg {
Arg {
name: String::new(),
typ: Type::Object,
interface: None,
summary: None,
description: None,
allow_null: false,
enum_: None,
}
}
}
#[derive(Debug)]
pub struct Enum {
pub name: String,
pub since: u16,
pub description: Option<(String, String)>,
pub entries: Vec<Entry>,
pub bitfield: bool
}
impl Enum {
pub fn new() -> Enum {
Enum {
name: String::new(),
since: 1,
description: None,
entries: Vec::new(),
bitfield: false
}
}
}
#[derive(Debug)]
pub struct Entry {
pub name: String,
pub value: String,
pub since: u16,
pub description: Option<(String, String)>,
pub summary: Option<String>
}
impl Entry {
pub fn new() -> Entry {
Entry {
name: String::new(),
value: "0".to_owned(),
since: 1,
description: None,
summary: None,
}
}
}
#[derive(Debug,PartialEq,Eq,Copy,Clone)]
pub enum Type {
Int,
Uint,
Fixed,
String,
Object,
NewId,
Array,
Fd,
Destructor
}
impl Type {
pub fn nullable(&self) -> bool {
match *self {
Type::String | Type::Object | Type::NewId | Type::Array => true,
_ => false
}
}
pub fn rust_type(&self) -> &'static str {
match *self {
Type::Int => "i32",
Type::Uint => "u32",
Type::Fixed => "f64",
Type::Array => "Vec<u8>",
Type::Fd => "::std::os::unix::io::RawFd",
Type::String => "String",
Type::Object => "ProxyId",
_ => "()"
}
}
}