Skip to content

Commit

Permalink
Fix code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Sep 14, 2024
1 parent 6462357 commit 2947756
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 108 deletions.
27 changes: 11 additions & 16 deletions gen/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use quote::{format_ident, quote};
use tracing::debug;

use crate::{
parser::{Interface, Protocol},
parser::{Interface, Pair},
utils::{description_to_docs, find_enum, make_ident, write_enums},
};

pub fn generate_client_code(current: &[Protocol], protocols: &[Protocol]) -> TokenStream {
pub fn generate_client_code(current: &[Pair], pairs: &[Pair]) -> TokenStream {
let mut modules = Vec::new();

for protocol in current {
for pair in current {
let protocol = &pair.protocol;
debug!("Generating client code for \"{}\"", &protocol.name);

let mut inner_modules = Vec::new();
Expand All @@ -27,7 +28,7 @@ pub fn generate_client_code(current: &[Protocol], protocols: &[Protocol]) -> Tok

let enums = write_enums(&interface);

let requests = write_requests(current, protocols, protocol, interface);
let requests = write_requests(pairs, pair, interface);

inner_modules.push(quote! {
#(#docs)*
Expand Down Expand Up @@ -76,12 +77,7 @@ pub fn generate_client_code(current: &[Protocol], protocols: &[Protocol]) -> Tok
}
}

fn write_requests(
_current: &[Protocol],
protocols: &[Protocol],
protocol: &Protocol,
interface: &Interface,
) -> Vec<TokenStream> {
fn write_requests(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<TokenStream> {
let mut requests = Vec::new();

for (opcode, request) in interface.requests.iter().enumerate() {
Expand All @@ -102,8 +98,7 @@ fn write_requests(
];

for arg in &request.args {
let mut ty =
arg.to_rust_type_token(arg.find_protocol(&protocols).as_ref().unwrap_or(protocol));
let mut ty = arg.to_rust_type_token(arg.find_protocol(pairs).as_ref().unwrap_or(pair));

if arg.allow_null {
ty = quote! {Option<#ty>};
Expand All @@ -124,10 +119,10 @@ fn write_requests(

if let Some((enum_interface, name)) = arg.to_enum_name() {
let e = if let Some(enum_interface) = enum_interface {
protocols
pairs
.iter()
.find_map(|protocol| {
protocol
.find_map(|pair| {
pair.protocol
.interfaces
.iter()
.find(|e| e.name == enum_interface)
Expand All @@ -138,7 +133,7 @@ fn write_requests(
.find(|e| e.name == name)
.unwrap()
} else {
find_enum(&protocol, &name)
find_enum(&pair.protocol, &name)
};

if e.bitfield {
Expand Down
30 changes: 16 additions & 14 deletions gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use parser::Protocol;
use parser::Pair;
use std::{fs::OpenOptions, io::Write as _};
use tracing::info;

Expand Down Expand Up @@ -166,11 +166,18 @@ fn main() -> Result<()> {

let Args { json } = Args::parse();

let protocols = PROTOCOLS
.into_iter()
.flat_map(|(_, proto)| proto)
.map(Protocol::from_path)
.collect::<Result<Vec<Protocol>>>()?;
let mut protocols = Vec::new();
let mut pairs = Vec::new();

for (module, protos) in PROTOCOLS {
let current = protos
.into_iter()
.map(|path| Pair::from_path(module, path))
.collect::<Result<Vec<Pair>>>()?;

pairs.extend(current.clone());
protocols.push((module, current))
}

if json {
info!("Generating json file");
Expand All @@ -183,12 +190,7 @@ fn main() -> Result<()> {

serde_json::to_writer(&mut json_path, &protocols)?;
} else {
for (module, current) in PROTOCOLS {
let current = current
.iter()
.map(Protocol::from_path)
.collect::<Result<Vec<Protocol>>>()?;

for (module, current) in protocols {
let mut server_path = OpenOptions::new()
.truncate(true)
.write(true)
Expand All @@ -198,7 +200,7 @@ fn main() -> Result<()> {
write!(
&mut server_path,
"{}",
generate_server_code(&current, &protocols)
generate_server_code(&current, &pairs)
)?;

let mut client_path = OpenOptions::new()
Expand All @@ -210,7 +212,7 @@ fn main() -> Result<()> {
write!(
&mut client_path,
"{}",
generate_client_code(&current, &protocols)
generate_client_code(&current, &pairs)
)?;
}

Expand Down
32 changes: 21 additions & 11 deletions gen/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::Path};
use std::{fmt::Display, fs, path::Path};

use anyhow::Result;
use heck::ToUpperCamelCase;
Expand All @@ -9,6 +9,12 @@ use tracing::debug;

use crate::utils::make_ident;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Pair {
pub protocol: Protocol,
pub module: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Protocol {
Expand Down Expand Up @@ -129,10 +135,13 @@ pub struct Entry {
pub description: Option<String>,
}

impl Protocol {
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
impl Pair {
pub fn from_path<D: Display, P: AsRef<Path>>(module: D, path: P) -> Result<Self> {
debug!("Parsing protocol {}", path.as_ref().display());
Ok(quick_xml::de::from_str(&fs::read_to_string(path)?)?)
Ok(Self {
protocol: quick_xml::de::from_str(&fs::read_to_string(path)?)?,
module: module.to_string(),
})
}
}

Expand All @@ -149,14 +158,14 @@ impl Arg {
None
}

pub fn find_protocol(&self, protocols: &[Protocol]) -> Option<Protocol> {
pub fn find_protocol(&self, pairs: &[Pair]) -> Option<Pair> {
if let Some((enum_interface, _name)) = self.to_enum_name() {
if let Some(enum_interface) = enum_interface {
return Some(
protocols
pairs
.iter()
.find(|protocol| {
protocol
.find(|pair| {
pair.protocol
.interfaces
.iter()
.find(|e| e.name == enum_interface)
Expand All @@ -173,14 +182,15 @@ impl Arg {
None
}

pub fn to_rust_type_token(&self, protocol: &Protocol) -> TokenStream {
pub fn to_rust_type_token(&self, pair: &Pair) -> TokenStream {
if let Some(e) = &self.r#enum {
if let Some((module, name)) = e.split_once('.') {
let protocol_name = make_ident(&protocol.name);
let protocol_name = make_ident(&pair.protocol.name);
let name = make_ident(name.to_upper_camel_case());
let module = make_ident(module);
let protocol_module = make_ident(&pair.module);

return quote! {super::super::#protocol_name::#module::#name};
return quote! {super::super::super::#protocol_module::#protocol_name::#module::#name};
} else {
return make_ident(e.to_upper_camel_case()).to_token_stream();
}
Expand Down
37 changes: 14 additions & 23 deletions gen/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use quote::{format_ident, quote};
use tracing::debug;

use crate::{
parser::{Interface, Protocol},
parser::{Interface, Pair},
utils::{description_to_docs, find_enum, make_ident, write_enums},
};

pub fn generate_server_code(current: &[Protocol], protocols: &[Protocol]) -> TokenStream {
pub fn generate_server_code(current: &[Pair], pairs: &[Pair]) -> TokenStream {
let mut modules = Vec::new();

for protocol in current {
for pair in current {
let protocol = &pair.protocol;
debug!("Generating server code for \"{}\"", &protocol.name);

let mut inner_modules = Vec::new();
Expand All @@ -27,8 +28,8 @@ pub fn generate_server_code(current: &[Protocol], protocols: &[Protocol]) -> Tok
let version = &interface.version;

let dispatchers = write_dispatchers(&interface);
let requests = write_requests(protocols, &protocol, &interface);
let events = write_events(protocols, &protocol, &interface);
let requests = write_requests(pairs, pair, &interface);
let events = write_events(pairs, pair, &interface);
let enums = write_enums(&interface);

inner_modules.push(quote! {
Expand Down Expand Up @@ -124,11 +125,7 @@ fn write_dispatchers(interface: &Interface) -> Vec<TokenStream> {

dispatchers
}
fn write_requests(
protocols: &[Protocol],
protocol: &Protocol,
interface: &Interface,
) -> Vec<TokenStream> {
fn write_requests(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<TokenStream> {
let mut requests = Vec::new();

for request in &interface.requests {
Expand All @@ -141,8 +138,7 @@ fn write_requests(
];

for arg in &request.args {
let mut ty =
arg.to_rust_type_token(arg.find_protocol(&protocols).as_ref().unwrap_or(protocol));
let mut ty = arg.to_rust_type_token(arg.find_protocol(pairs).as_ref().unwrap_or(pair));

if arg.allow_null {
ty = quote! {Option<#ty>};
Expand All @@ -162,11 +158,7 @@ fn write_requests(
requests
}

fn write_events(
protocols: &[Protocol],
protocol: &Protocol,
interface: &Interface,
) -> Vec<TokenStream> {
fn write_events(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<TokenStream> {
let mut events = Vec::new();

for (opcode, event) in interface.events.iter().enumerate() {
Expand All @@ -183,8 +175,7 @@ fn write_events(
];

for arg in &event.args {
let mut ty =
arg.to_rust_type_token(arg.find_protocol(protocols).as_ref().unwrap_or(protocol));
let mut ty = arg.to_rust_type_token(arg.find_protocol(&pairs).as_ref().unwrap_or(pair));

if arg.allow_null {
ty = quote! {Option<#ty>};
Expand All @@ -205,10 +196,10 @@ fn write_events(

if let Some((enum_interface, name)) = arg.to_enum_name() {
let e = if let Some(enum_interface) = enum_interface {
protocols
pairs
.iter()
.find_map(|protocol| {
protocol
.find_map(|pair| {
pair.protocol
.interfaces
.iter()
.find(|e| e.name == enum_interface)
Expand All @@ -219,7 +210,7 @@ fn write_events(
.find(|e| e.name == name)
.unwrap()
} else {
find_enum(&protocol, &name)
find_enum(&pair.protocol, &name)
};

if e.bitfield {
Expand Down
10 changes: 5 additions & 5 deletions src/client/protocol/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub mod wayland {
width: i32,
height: i32,
stride: i32,
format: super::super::wayland::wl_shm::Format,
format: super::super::super::core::wayland::wl_shm::Format,
) -> crate::client::Result<()> {
tracing::debug!("-> wl_shm_pool#{}.create_buffer()", object_id);
let (payload, fds) = crate::wire::PayloadBuilder::new()
Expand Down Expand Up @@ -1028,8 +1028,8 @@ pub mod wayland {
&self,
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
dnd_actions: super::super::wayland::wl_data_device_manager::DndAction,
preferred_action: super::super::wayland::wl_data_device_manager::DndAction,
dnd_actions: super::super::super::core::wayland::wl_data_device_manager::DndAction,
preferred_action : super :: super :: super :: core :: wayland :: wl_data_device_manager :: DndAction,
) -> crate::client::Result<()> {
tracing::debug!("-> wl_data_offer#{}.set_actions()", object_id);
let (payload, fds) = crate::wire::PayloadBuilder::new()
Expand Down Expand Up @@ -1129,7 +1129,7 @@ pub mod wayland {
&self,
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
dnd_actions: super::super::wayland::wl_data_device_manager::DndAction,
dnd_actions: super::super::super::core::wayland::wl_data_device_manager::DndAction,
) -> crate::client::Result<()> {
tracing::debug!("-> wl_data_source#{}.set_actions()", object_id);
let (payload, fds) = crate::wire::PayloadBuilder::new()
Expand Down Expand Up @@ -2165,7 +2165,7 @@ pub mod wayland {
&self,
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
transform: super::super::wayland::wl_output::Transform,
transform: super::super::super::core::wayland::wl_output::Transform,
) -> crate::client::Result<()> {
tracing::debug!("-> wl_surface#{}.set_buffer_transform()", object_id);
let (payload, fds) = crate::wire::PayloadBuilder::new()
Expand Down
6 changes: 3 additions & 3 deletions src/client/protocol/weston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ pub mod color_management_v1 {
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
image_description: crate::wire::ObjectId,
render_intent: super::super::color_management_v1::xx_color_manager_v4::RenderIntent,
render_intent : super :: super :: super :: weston :: color_management_v1 :: xx_color_manager_v4 :: RenderIntent,
) -> crate::client::Result<()> {
tracing::debug!(
"-> xx_color_management_surface_v4#{}.set_image_description()",
Expand Down Expand Up @@ -959,7 +959,7 @@ pub mod color_management_v1 {
&self,
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
tf: super::super::color_management_v1::xx_color_manager_v4::TransferFunction,
tf : super :: super :: super :: weston :: color_management_v1 :: xx_color_manager_v4 :: TransferFunction,
) -> crate::client::Result<()> {
tracing::debug!(
"-> xx_image_description_creator_params_v4#{}.set_tf_named()",
Expand Down Expand Up @@ -1022,7 +1022,7 @@ pub mod color_management_v1 {
&self,
socket: &mut crate::wire::Socket,
object_id: crate::wire::ObjectId,
primaries: super::super::color_management_v1::xx_color_manager_v4::Primaries,
primaries : super :: super :: super :: weston :: color_management_v1 :: xx_color_manager_v4 :: Primaries,
) -> crate::client::Result<()> {
tracing::debug!(
"-> xx_image_description_creator_params_v4#{}.set_primaries_named()",
Expand Down
Loading

0 comments on commit 2947756

Please sign in to comment.