Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headerless xml parsing fix #715

Merged
merged 7 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this test file is only used to ensure the scanner doesn't choke on it, I don't think it needs to be as big as the main one, it can be much more minimalistic I'd say.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done, thanks.

</protocol>
Loading