Skip to content

Commit

Permalink
Headerless xml parsing fix (#715)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lmaxyz and Leyman Max authored Apr 23, 2024
1 parent 8d161e5 commit 1fc7814
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
2 changes: 2 additions & 0 deletions wayland-protocols-plasma/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Make available protocols that based on headerless xml files.

## 0.2.0 -- 2023-09-02

### Breaking changes
Expand Down
28 changes: 12 additions & 16 deletions wayland-protocols-plasma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ pub mod fake_input {
);
}

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

pub mod idle {
wayland_protocol!(
Expand Down Expand Up @@ -179,14 +177,12 @@ pub mod slide {
);
}

// This protocol is disabled for now as the file is not valid XML because it does not have a XML header
//
// pub mod surface_extension {
// wayland_protocol!(
// "./plasma-wayland-protocols/src/protocols/surface-extension.xml",
// []
// );
// }
pub mod surface_extension {
wayland_protocol!(
"./plasma-wayland-protocols/src/protocols/surface-extension.xml",
[]
);
}

pub mod text_input {
pub mod v1 {
Expand Down
1 change: 1 addition & 0 deletions wayland-scanner/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

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

## 0.31.1 -- 2024-01-29

Expand Down
53 changes: 42 additions & 11 deletions wayland-scanner/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ macro_rules! extract_end_tag(
pub fn parse<S: Read>(stream: S) -> Protocol {
let mut reader = Reader::from_reader(BufReader::new(stream));
reader.trim_text(true).expand_empty_elements(true);
// Skip first <?xml ... ?> event
let _ = reader.read_event_into(&mut Vec::new());
parse_protocol(reader)
}

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

fn parse_protocol<R: BufRead>(mut reader: Reader<R>) -> Protocol {
let mut protocol = extract_from!(
reader => Event::Start(bytes) => {
assert!(bytes.name().into_inner() == b"protocol", "Missing protocol toplevel tag");
if let Some(attr) = bytes.attributes().filter_map(|res| res.ok()).find(|attr| attr.key.into_inner() == b"name") {
Protocol::new(decode_utf8_or_panic(attr.value.into_owned()))
} else {
panic!("Protocol must have a name");
fn init_protocol<R: BufRead>(reader: &mut Reader<R>) -> Protocol {
// Check two firsts lines for protocol tag
for _ in 0..2 {
match reader.read_event_into(&mut Vec::new()) {
Ok(Event::Decl(_)) => {
continue;
}
Ok(Event::Start(bytes)) => {
assert!(bytes.name().into_inner() == b"protocol", "Missing protocol toplevel tag");
if let Some(attr) = bytes
.attributes()
.filter_map(|res| res.ok())
.find(|attr| attr.key.into_inner() == b"name")
{
return Protocol::new(decode_utf8_or_panic(attr.value.into_owned()));
} else {
panic!("Protocol must have a name");
}
}
_ => panic!("Ill-formed protocol file"),
}
);
}
panic!("Ill-formed protocol file");
}

fn parse_protocol<R: BufRead>(mut reader: Reader<R>) -> Protocol {
let mut protocol = init_protocol(&mut reader);

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

entry
}

#[cfg(test)]
mod tests {
#[test]
fn xml_parse() {
let protocol_file =
std::fs::File::open("./tests/scanner_assets/test-protocol.xml").unwrap();
let _ = crate::parse::parse(protocol_file);
}

#[test]
fn headerless_xml_parse() {
let protocol_file =
std::fs::File::open("./tests/scanner_assets/test-headerless-protocol.xml").unwrap();
let _ = crate::parse::parse(protocol_file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<protocol name="test-protocol">
</protocol>

0 comments on commit 1fc7814

Please sign in to comment.