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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::parser::{self, ArgKind};

#[derive(Debug)]
pub struct Protocol {
    pub name: String,
    pub copyright: Option<String>,
    pub description: Option<Description>,
    pub interfaces: Vec<Interface>,
}

#[derive(Debug)]
pub struct Description {
    pub summary: String,
    pub description: String,
}

#[derive(Debug)]
pub struct Interface {
    pub name: String,
    pub version: u32,
    pub description: Option<Description>,
    pub requests: Vec<Message>,
    pub events: Vec<Message>,
    pub enums: Vec<Enum>,
}

#[derive(Debug)]
pub struct Message {
    pub name: String,
    pub since: u32,
    pub request_type: Option<String>,
    pub description: Option<Description>,
    pub args: Vec<Arg>,
}

#[derive(Debug)]
pub struct Arg {
    pub name: String,
    pub kind: ArgKind,
    pub summary: Option<String>,
    pub interface: Option<String>,
    pub nullable: bool,
    pub enum_type: Option<String>,
    pub description: Option<Description>,
}

#[derive(Debug)]
pub struct Enum {
    pub name: String,
    pub since: u32,
    pub bitfield: bool,
    pub description: Option<Description>,
    pub entries: Vec<EnumEntry>,
}

#[derive(Debug)]
pub struct EnumEntry {
    pub name: String,
    pub value: i64,
    pub summary: Option<String>,
    pub since: u32,
    pub description: Option<Description>,
}

pub type AstError = String;
pub type AstResult<T> = Result<T, AstError>;

fn build_protocol(node: parser::ParseNode) -> AstResult<Protocol> {
    if let parser::ParseElement::Protocol { name } = node.element {
        let mut copyright: Option<String> = None;
        let mut description: Option<Description> = None;
        let mut interfaces: Vec<Interface> = Vec::new();
        for child in node.children {
            match &child.element {
                parser::ParseElement::Copyright => copyright = Some(build_copyright(child)?),
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                parser::ParseElement::Interface { .. } => interfaces.push(build_interface(child)?),
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(Protocol { name: name, copyright, description, interfaces })
    } else {
        Err("Unexpected Element; expected Protocol".to_owned())
    }
}

fn build_copyright(node: parser::ParseNode) -> AstResult<String> {
    if let Some(copyright) = node.body {
        Ok(copyright)
    } else {
        Err(format!("Unexpected node {:?}", node))
    }
}

fn build_description(node: parser::ParseNode) -> AstResult<Description> {
    if let parser::ParseElement::Description { summary } = node.element {
        Ok(Description { summary, description: node.body.unwrap_or("".to_owned()) })
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_interface(node: parser::ParseNode) -> AstResult<Interface> {
    if let parser::ParseElement::Interface { name, version } = node.element {
        let mut description: Option<Description> = None;
        let mut requests: Vec<Message> = Vec::new();
        let mut events: Vec<Message> = Vec::new();
        let mut enums: Vec<Enum> = Vec::new();
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                parser::ParseElement::Request { .. } => requests.push(build_request(child)?),
                parser::ParseElement::Event { .. } => events.push(build_event(child)?),
                parser::ParseElement::Enum { .. } => enums.push(build_enum(child)?),
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(Interface { name, version, description, requests, events, enums })
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_request(node: parser::ParseNode) -> AstResult<Message> {
    if let parser::ParseElement::Request { name, since, request_type } = node.element {
        let mut description: Option<Description> = None;
        let mut args: Vec<Arg> = Vec::new();
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                parser::ParseElement::Arg { .. } => args.append(&mut build_arg(child)?),
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(Message { name, since, request_type, description, args })
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_event(node: parser::ParseNode) -> AstResult<Message> {
    if let parser::ParseElement::Event { name, since } = node.element {
        let mut description: Option<Description> = None;
        let mut args: Vec<Arg> = Vec::new();
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                parser::ParseElement::Arg { .. } => args.append(&mut build_arg(child)?),
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(Message { name, since, description, args, request_type: None })
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_arg(node: parser::ParseNode) -> AstResult<Vec<Arg>> {
    if let parser::ParseElement::Arg { name, kind, summary, interface, nullable, enum_type } =
        node.element
    {
        let mut description: Option<Description> = None;
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                _ => return Err("Unsupported".to_owned()),
            }
        }
        let arg = Arg { name, kind, summary, interface, nullable, enum_type, description };
        // wayland has a slightly different serialization of untyped new_id
        // arguments. Instead of just sending the object id, the interface name
        // and version are sent first. This primarily impacts the
        // wl_registry::bind request.
        if arg.kind == ArgKind::NewId && arg.interface.is_none() {
            Ok(vec![
                Arg {
                    name: format!("{}_interface_name", arg.name),
                    kind: ArgKind::String,
                    summary: None,
                    interface: None,
                    nullable: false,
                    enum_type: None,
                    description: None,
                },
                Arg {
                    name: format!("{}_interface_version", arg.name),
                    kind: ArgKind::Uint,
                    summary: None,
                    interface: None,
                    nullable: false,
                    enum_type: None,
                    description: None,
                },
                arg,
            ])
        } else {
            Ok(vec![arg])
        }
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_enum(node: parser::ParseNode) -> AstResult<Enum> {
    if let parser::ParseElement::Enum { name, since, bitfield } = node.element {
        let mut description: Option<Description> = None;
        let mut entries: Vec<EnumEntry> = Vec::new();
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                parser::ParseElement::EnumEntry { .. } => entries.push(build_enum_entry(child)?),
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(Enum { name, since, bitfield, description, entries })
    } else {
        Err("Invalid node".to_owned())
    }
}

fn build_enum_entry(node: parser::ParseNode) -> AstResult<EnumEntry> {
    if let parser::ParseElement::EnumEntry { name, value, summary, since } = node.element {
        let mut description: Option<Description> = None;
        for child in node.children {
            match &child.element {
                parser::ParseElement::Description { .. } => {
                    description = Some(build_description(child)?)
                }
                _ => return Err("Unsupported".to_owned()),
            }
        }
        Ok(EnumEntry { name, value, summary, since, description })
    } else {
        Err("Invalid node".to_owned())
    }
}

impl Protocol {
    pub fn from_parse_tree(parse_tree: parser::ParseNode) -> AstResult<Protocol> {
        build_protocol(parse_tree)
    }
}