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

wayland-scanner: Support deprecated-since #784

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 1 addition & 1 deletion wayland-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ use std::{
///
/// This module is automatically generated from the `wayland.xml` protocol specification,
/// and contains the interface definitions for the core Wayland protocol.
#[allow(missing_docs)]
#[allow(missing_docs, deprecated)]
pub mod protocol {
use self::__interfaces::*;
use crate as wayland_client;
Expand Down
2 changes: 1 addition & 1 deletion wayland-protocols/src/protocol_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ macro_rules! wayland_protocol(
mod generated {
#![allow(dead_code,non_camel_case_types,unused_unsafe,unused_variables)]
#![allow(non_upper_case_globals,non_snake_case,unused_imports)]
#![allow(missing_docs, clippy::all)]
#![allow(missing_docs, clippy::all, deprecated)]

#[cfg(feature = "client")]
pub mod client {
Expand Down
2 changes: 1 addition & 1 deletion wayland-scanner/src/client_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
let iface_name = Ident::new(&snake_to_camel(&interface.name), Span::call_site());
let iface_const_name = format_ident!("{}_INTERFACE", interface.name.to_ascii_uppercase());

let enums = crate::common::generate_enums_for(interface);
let enums = crate::common::generate_enums_for(interface, Side::Client);
let sinces = crate::common::gen_msg_constants(&interface.requests, &interface.events);

let requests = crate::common::gen_message_enum(
Expand Down
56 changes: 47 additions & 9 deletions wayland-scanner/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,48 @@ use quote::{format_ident, quote, ToTokens};

use crate::{protocol::*, util::*, Side};

pub(crate) fn generate_enums_for(interface: &Interface) -> TokenStream {
interface.enums.iter().map(ToTokens::into_token_stream).collect()
pub(crate) fn generate_enums_for(interface: &Interface, side: Side) -> TokenStream {
interface.enums.iter().map(|enu| EnumSide(enu, side)).map(ToTokens::into_token_stream).collect()
}

impl ToTokens for Enum {
struct EnumSide<'a>(&'a Enum, Side);

impl ToTokens for EnumSide<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let EnumSide(enu, side) = *self;

let enum_decl;
let enum_impl;

let doc_attr = self.description.as_ref().map(description_to_doc_attr);
let ident = Ident::new(&snake_to_camel(&self.name), Span::call_site());
let doc_attr = enu.description.as_ref().map(description_to_doc_attr);
let ident = Ident::new(&snake_to_camel(&enu.name), Span::call_site());

if self.bitfield {
let entries = self.entries.iter().map(|entry| {
if enu.bitfield {
let entries = enu.entries.iter().map(|entry| {
let doc_attr = entry
.description
.as_ref()
.map(description_to_doc_attr)
.or_else(|| entry.summary.as_ref().map(|s| to_doc_attr(s)));

// Deprecations are only for the client side since servers need to be backwards compatible
let deprecated_attr = (side == Side::Client)
.then(|| {
entry.deprecated_since.map(|since| {
let note = format!("Deprecated since version {since} of the interface");
quote! { #[deprecated = #note] }
})
})
.flatten();

let prefix = if entry.name.chars().next().unwrap().is_numeric() { "_" } else { "" };
let ident = format_ident!("{}{}", prefix, snake_to_camel(&entry.name));

let value = Literal::u32_unsuffixed(entry.value);

quote! {
#doc_attr
#deprecated_attr
const #ident = #value;
}
});
Expand Down Expand Up @@ -60,20 +75,31 @@ impl ToTokens for Enum {
}
};
} else {
let variants = self.entries.iter().map(|entry| {
let variants = enu.entries.iter().map(|entry| {
let doc_attr = entry
.description
.as_ref()
.map(description_to_doc_attr)
.or_else(|| entry.summary.as_ref().map(|s| to_doc_attr(s)));

// Deprecations are only for the client side since servers need to be backwards compatible
let deprecated_attr = (side == Side::Client)
.then(|| {
entry.deprecated_since.map(|since| {
let note = format!("Deprecated since version {since} of the interface");
quote! { #[deprecated = #note] }
})
})
.flatten();

let prefix = if entry.name.chars().next().unwrap().is_numeric() { "_" } else { "" };
let variant = format_ident!("{}{}", prefix, snake_to_camel(&entry.name));

let value = Literal::u32_unsuffixed(entry.value);

quote! {
#doc_attr
#deprecated_attr
#variant = #value
}
});
Expand All @@ -88,7 +114,7 @@ impl ToTokens for Enum {
}
};

let match_arms = self.entries.iter().map(|entry| {
let match_arms = enu.entries.iter().map(|entry| {
let value = Literal::u32_unsuffixed(entry.value);

let prefix = if entry.name.chars().next().unwrap().is_numeric() { "_" } else { "" };
Expand Down Expand Up @@ -181,6 +207,17 @@ pub(crate) fn gen_message_enum(
}

let doc_attr = to_doc_attr(&docs);

// Deprecations are only for the client side since servers need to be backwards compatible
let deprecated_attr = (side == Side::Client)
.then(|| {
msg.deprecated_since.map(|since| {
let note = format!("Deprecated since version {since} of the interface");
quote! { #[deprecated = #note] }
})
})
.flatten();

let msg_name = Ident::new(&snake_to_camel(&msg.name), Span::call_site());
let msg_variant_decl =
if msg.args.is_empty() {
Expand Down Expand Up @@ -277,6 +314,7 @@ pub(crate) fn gen_message_enum(

quote! {
#doc_attr
#deprecated_attr
#msg_variant_decl
}
})
Expand Down
3 changes: 3 additions & 0 deletions wayland-scanner/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ fn parse_request<R: BufRead>(reader: &mut Reader<R>, attrs: Attributes) -> Messa
b"name" => request.name = decode_utf8_or_panic(attr.value.into_owned()),
b"type" => request.typ = Some(parse_type(&attr.value)),
b"since" => request.since = parse_or_panic(&attr.value),
b"deprecated-since" => request.deprecated_since = Some(parse_or_panic(&attr.value)),
_ => {}
}
}
Expand Down Expand Up @@ -258,6 +259,7 @@ fn parse_event<R: BufRead>(reader: &mut Reader<R>, attrs: Attributes) -> Message
b"name" => event.name = decode_utf8_or_panic(attr.value.into_owned()),
b"type" => event.typ = Some(parse_type(&attr.value)),
b"since" => event.since = parse_or_panic(&attr.value),
b"deprecated-since" => event.deprecated_since = Some(parse_or_panic(&attr.value)),
_ => {}
}
}
Expand Down Expand Up @@ -355,6 +357,7 @@ fn parse_entry<R: BufRead>(reader: &mut Reader<R>, attrs: Attributes) -> Entry {
};
}
b"since" => entry.since = parse_or_panic(&attr.value),
b"deprecated-since" => entry.deprecated_since = Some(parse_or_panic(&attr.value)),
b"summary" => {
entry.summary = Some(
String::from_utf8_lossy(&attr.value)
Expand Down
20 changes: 18 additions & 2 deletions wayland-scanner/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ pub struct Message {
pub name: String,
pub typ: Option<Type>,
pub since: u32,
pub deprecated_since: Option<u32>,
pub description: Option<(String, String)>,
pub args: Vec<Arg>,
}

impl Message {
pub fn new() -> Message {
Message { name: String::new(), typ: None, since: 1, description: None, args: Vec::new() }
Message {
name: String::new(),
typ: None,
since: 1,
deprecated_since: None,
description: None,
args: Vec::new(),
}
}

pub fn all_null(&self) -> bool {
Expand Down Expand Up @@ -110,13 +118,21 @@ pub struct Entry {
pub name: String,
pub value: u32,
pub since: u16,
pub deprecated_since: Option<u16>,
pub description: Option<(String, String)>,
pub summary: Option<String>,
}

impl Entry {
pub fn new() -> Entry {
Entry { name: String::new(), value: 0, since: 1, description: None, summary: None }
Entry {
name: String::new(),
value: 0,
since: 1,
deprecated_since: None,
description: None,
summary: None,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion wayland-scanner/src/server_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn generate_objects_for(interface: &Interface) -> TokenStream {
let iface_name = Ident::new(&snake_to_camel(&interface.name), Span::call_site());
let iface_const_name = format_ident!("{}_INTERFACE", interface.name.to_ascii_uppercase());

let enums = crate::common::generate_enums_for(interface);
let enums = crate::common::generate_enums_for(interface, Side::Server);
let msg_constants = crate::common::gen_msg_constants(&interface.requests, &interface.events);

let requests = crate::common::gen_message_enum(
Expand Down
Loading