Skip to content

Commit 1fc7814

Browse files
authored
Headerless xml parsing fix (#715)
* Add tests for parsing * Fix headerless xml parsing * Add comment * Enable some protocols disabled before * Remove useless body from tested xml file * Fix code formatting * Add changelogs --------- Co-authored-by: Leyman Max <jiumoh2011@yandex.ru>
1 parent 8d161e5 commit 1fc7814

File tree

5 files changed

+59
-27
lines changed

5 files changed

+59
-27
lines changed

wayland-protocols-plasma/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Make available protocols that based on headerless xml files.
6+
57
## 0.2.0 -- 2023-09-02
68

79
### Breaking changes

wayland-protocols-plasma/src/lib.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,12 @@ pub mod fake_input {
4949
);
5050
}
5151

52-
// This protocol is disabled for now as the file is not valid XML because it does not have a XML header
53-
//
54-
// pub mod fullscreen_shell {
55-
// wayland_protocol!(
56-
// "./plasma-wayland-protocols/src/protocols/fullscreen-shell.xml",
57-
// []
58-
// );
59-
// }
52+
pub mod fullscreen_shell {
53+
wayland_protocol!(
54+
"./plasma-wayland-protocols/src/protocols/fullscreen-shell.xml",
55+
[]
56+
);
57+
}
6058

6159
pub mod idle {
6260
wayland_protocol!(
@@ -179,14 +177,12 @@ pub mod slide {
179177
);
180178
}
181179

182-
// This protocol is disabled for now as the file is not valid XML because it does not have a XML header
183-
//
184-
// pub mod surface_extension {
185-
// wayland_protocol!(
186-
// "./plasma-wayland-protocols/src/protocols/surface-extension.xml",
187-
// []
188-
// );
189-
// }
180+
pub mod surface_extension {
181+
wayland_protocol!(
182+
"./plasma-wayland-protocols/src/protocols/surface-extension.xml",
183+
[]
184+
);
185+
}
190186

191187
pub mod text_input {
192188
pub mod v1 {

wayland-scanner/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Use wrapper type implementing `Sync` instead of `static mut`s.
6+
- Add headerless xml file parsing possibility for `parse` function.
67

78
## 0.31.1 -- 2024-01-29
89

wayland-scanner/src/parse.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ macro_rules! extract_end_tag(
2929
pub fn parse<S: Read>(stream: S) -> Protocol {
3030
let mut reader = Reader::from_reader(BufReader::new(stream));
3131
reader.trim_text(true).expand_empty_elements(true);
32-
// Skip first <?xml ... ?> event
33-
let _ = reader.read_event_into(&mut Vec::new());
3432
parse_protocol(reader)
3533
}
3634

@@ -52,17 +50,33 @@ fn parse_or_panic<T: FromStr>(txt: &[u8]) -> T {
5250
}
5351
}
5452

55-
fn parse_protocol<R: BufRead>(mut reader: Reader<R>) -> Protocol {
56-
let mut protocol = extract_from!(
57-
reader => Event::Start(bytes) => {
58-
assert!(bytes.name().into_inner() == b"protocol", "Missing protocol toplevel tag");
59-
if let Some(attr) = bytes.attributes().filter_map(|res| res.ok()).find(|attr| attr.key.into_inner() == b"name") {
60-
Protocol::new(decode_utf8_or_panic(attr.value.into_owned()))
61-
} else {
62-
panic!("Protocol must have a name");
53+
fn init_protocol<R: BufRead>(reader: &mut Reader<R>) -> Protocol {
54+
// Check two firsts lines for protocol tag
55+
for _ in 0..2 {
56+
match reader.read_event_into(&mut Vec::new()) {
57+
Ok(Event::Decl(_)) => {
58+
continue;
59+
}
60+
Ok(Event::Start(bytes)) => {
61+
assert!(bytes.name().into_inner() == b"protocol", "Missing protocol toplevel tag");
62+
if let Some(attr) = bytes
63+
.attributes()
64+
.filter_map(|res| res.ok())
65+
.find(|attr| attr.key.into_inner() == b"name")
66+
{
67+
return Protocol::new(decode_utf8_or_panic(attr.value.into_owned()));
68+
} else {
69+
panic!("Protocol must have a name");
70+
}
6371
}
72+
_ => panic!("Ill-formed protocol file"),
6473
}
65-
);
74+
}
75+
panic!("Ill-formed protocol file");
76+
}
77+
78+
fn parse_protocol<R: BufRead>(mut reader: Reader<R>) -> Protocol {
79+
let mut protocol = init_protocol(&mut reader);
6680

6781
loop {
6882
match reader.read_event_into(&mut Vec::new()) {
@@ -366,3 +380,20 @@ fn parse_entry<R: BufRead>(reader: &mut Reader<R>, attrs: Attributes) -> Entry {
366380

367381
entry
368382
}
383+
384+
#[cfg(test)]
385+
mod tests {
386+
#[test]
387+
fn xml_parse() {
388+
let protocol_file =
389+
std::fs::File::open("./tests/scanner_assets/test-protocol.xml").unwrap();
390+
let _ = crate::parse::parse(protocol_file);
391+
}
392+
393+
#[test]
394+
fn headerless_xml_parse() {
395+
let protocol_file =
396+
std::fs::File::open("./tests/scanner_assets/test-headerless-protocol.xml").unwrap();
397+
let _ = crate::parse::parse(protocol_file);
398+
}
399+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<protocol name="test-protocol">
2+
</protocol>

0 commit comments

Comments
 (0)