Skip to content

Commit

Permalink
Improve formatting of strings and new ids
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Dec 19, 2024
1 parent f1be19d commit 5535fb8
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 224 deletions.
55 changes: 36 additions & 19 deletions gen/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quote::{format_ident, quote};
use tracing::debug;

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

Expand Down Expand Up @@ -116,18 +116,13 @@ fn write_dispatchers(interface: &Interface) -> Vec<TokenStream> {
for arg in &request.args {
let mut optional = quote! {};
let mut map_display = quote! {};
let mut fd_print = quote! {};

if !arg.allow_null && arg.is_return_option() {
optional = quote! {.ok_or(crate::wire::DecodeError::MalformedPayload)?};
} else if arg.allow_null {
map_display = quote! {.as_ref().map_or("null".to_string(), |v| v.to_string())}
}

if arg.ty.is_fd() {
fd_print = quote! { .as_raw_fd() }
}

let mut tryinto = quote! {};

if arg.r#enum.is_some() {
Expand All @@ -146,8 +141,24 @@ fn write_dispatchers(interface: &Interface) -> Vec<TokenStream> {
#name #tryinto
});

tracing_fmt.push("{}");
tracing_args.push(quote! { #name #fd_print #map_display });
match arg.ty {
ArgType::Array => {
tracing_fmt.push("array[{}]");
tracing_args.push(quote! { #name .len() });
}
ArgType::String => {
tracing_fmt.push("\"{}\"");
tracing_args.push(quote! { #name #map_display });
}
ArgType::Fd => {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name .as_raw_fd() #map_display });
}
_ => {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name #map_display });
}
}
}

let tracing_fmt = tracing_fmt.join(", ");
Expand Down Expand Up @@ -225,27 +236,33 @@ fn write_events(pairs: &[Pair], pair: &Pair, interface: &Interface) -> Vec<Token
let mut ty = arg.to_rust_type_token(arg.find_protocol(&pairs).as_ref().unwrap_or(pair));

let mut map_display = quote! {};
let mut fd_print = quote! {};

if arg.allow_null {
ty = quote! {Option<#ty>};
map_display = quote! {.as_ref().map_or("null".to_string(), |v| v.to_string())}
}

if arg.ty.is_fd() {
fd_print = quote! { .as_raw_fd() }
}

let name = make_ident(arg.name.to_snek_case());

args.push(quote! {#name: #ty});

if !arg.ty.is_array() {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name #fd_print #map_display });
} else {
tracing_fmt.push("array[{}]");
tracing_args.push(quote! { #name .len() });
match arg.ty {
ArgType::Array => {
tracing_fmt.push("array[{}]");
tracing_args.push(quote! { #name .len() });
}
ArgType::String => {
tracing_fmt.push("\"{}\"");
tracing_args.push(quote! { #name #map_display });
}
ArgType::Fd => {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name .as_raw_fd() #map_display });
}
_ => {
tracing_fmt.push("{}");
tracing_args.push(quote! { #name #map_display });
}
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/server/protocol/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub mod wayland {
message: String,
) -> crate::server::Result<()> {
tracing::debug!(
"-> wl_display#{}.error({}, {}, {})",
"-> wl_display#{}.error({}, {}, \"{}\")",
object.id,
object_id,
code,
Expand Down Expand Up @@ -232,7 +232,7 @@ pub mod wayland {
version: u32,
) -> crate::server::Result<()> {
tracing::debug!(
"-> wl_registry#{}.global({}, {}, {})",
"-> wl_registry#{}.global({}, \"{}\", {})",
object.id,
name,
interface,
Expand Down Expand Up @@ -1152,7 +1152,7 @@ pub mod wayland {
let serial = message.uint()?;
let mime_type = message.string()?;
tracing::debug!(
"wl_data_offer#{}.accept({}, {})",
"wl_data_offer#{}.accept({}, \"{}\")",
object.id,
serial,
mime_type
Expand All @@ -1167,7 +1167,7 @@ pub mod wayland {
.ok_or(crate::wire::DecodeError::MalformedPayload)?;
let fd = message.fd()?;
tracing::debug!(
"wl_data_offer#{}.receive({}, {})",
"wl_data_offer#{}.receive(\"{}\", {})",
object.id,
mime_type,
fd.as_raw_fd()
Expand Down Expand Up @@ -1316,7 +1316,7 @@ pub mod wayland {
client: &mut crate::server::Client,
mime_type: String,
) -> crate::server::Result<()> {
tracing::debug!("-> wl_data_offer#{}.offer({})", object.id, mime_type);
tracing::debug!("-> wl_data_offer#{}.offer(\"{}\")", object.id, mime_type);
let (payload, fds) = crate::wire::PayloadBuilder::new()
.put_string(Some(mime_type))
.build();
Expand Down Expand Up @@ -1454,7 +1454,7 @@ pub mod wayland {
let mime_type = message
.string()?
.ok_or(crate::wire::DecodeError::MalformedPayload)?;
tracing::debug!("wl_data_source#{}.offer({})", object.id, mime_type);
tracing::debug!("wl_data_source#{}.offer(\"{}\")", object.id, mime_type);
self.offer(object, client, mime_type).await
}
1u16 => {
Expand Down Expand Up @@ -1519,7 +1519,7 @@ pub mod wayland {
mime_type: Option<String>,
) -> crate::server::Result<()> {
tracing::debug!(
"-> wl_data_source#{}.target({})",
"-> wl_data_source#{}.target(\"{}\")",
object.id,
mime_type
.as_ref()
Expand All @@ -1544,7 +1544,7 @@ pub mod wayland {
fd: rustix::fd::OwnedFd,
) -> crate::server::Result<()> {
tracing::debug!(
"-> wl_data_source#{}.send({}, {})",
"-> wl_data_source#{}.send(\"{}\", {})",
object.id,
mime_type,
fd.as_raw_fd()
Expand Down Expand Up @@ -2362,14 +2362,14 @@ pub mod wayland {
let title = message
.string()?
.ok_or(crate::wire::DecodeError::MalformedPayload)?;
tracing::debug!("wl_shell_surface#{}.set_title({})", object.id, title);
tracing::debug!("wl_shell_surface#{}.set_title(\"{}\")", object.id, title);
self.set_title(object, client, title).await
}
9u16 => {
let class_ = message
.string()?
.ok_or(crate::wire::DecodeError::MalformedPayload)?;
tracing::debug!("wl_shell_surface#{}.set_class({})", object.id, class_);
tracing::debug!("wl_shell_surface#{}.set_class(\"{}\")", object.id, class_);
self.set_class(object, client, class_).await
}
_ => Err(crate::server::error::Error::UnknownOpcode),
Expand Down Expand Up @@ -3486,7 +3486,7 @@ pub mod wayland {
client: &mut crate::server::Client,
name: String,
) -> crate::server::Result<()> {
tracing::debug!("-> wl_seat#{}.name({})", object.id, name);
tracing::debug!("-> wl_seat#{}.name(\"{}\")", object.id, name);
let (payload, fds) = crate::wire::PayloadBuilder::new()
.put_string(Some(name))
.build();
Expand Down Expand Up @@ -4953,7 +4953,7 @@ pub mod wayland {
transform: Transform,
) -> crate::server::Result<()> {
tracing::debug!(
"-> wl_output#{}.geometry({}, {}, {}, {}, {}, {}, {}, {})",
"-> wl_output#{}.geometry({}, {}, {}, {}, {}, \"{}\", \"{}\", {})",
object.id,
x,
y,
Expand Down Expand Up @@ -5122,7 +5122,7 @@ pub mod wayland {
client: &mut crate::server::Client,
name: String,
) -> crate::server::Result<()> {
tracing::debug!("-> wl_output#{}.name({})", object.id, name);
tracing::debug!("-> wl_output#{}.name(\"{}\")", object.id, name);
let (payload, fds) = crate::wire::PayloadBuilder::new()
.put_string(Some(name))
.build();
Expand Down Expand Up @@ -5151,7 +5151,11 @@ pub mod wayland {
client: &mut crate::server::Client,
description: String,
) -> crate::server::Result<()> {
tracing::debug!("-> wl_output#{}.description({})", object.id, description);
tracing::debug!(
"-> wl_output#{}.description(\"{}\")",
object.id,
description
);
let (payload, fds) = crate::wire::PayloadBuilder::new()
.put_string(Some(description))
.build();
Expand Down
28 changes: 9 additions & 19 deletions src/server/protocol/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub mod cosmic_atspi_v1 {
let virtual_mods = message.array()?;
let key = message.uint()?;
tracing::debug!(
"cosmic_atspi_manager_v1#{}.add_key_grab({}, {}, {})",
"cosmic_atspi_manager_v1#{}.add_key_grab({}, array[{}], {})",
object.id,
mods,
virtual_mods,
virtual_mods.len(),
key
);
self.add_key_grab(object, client, mods, virtual_mods, key)
Expand All @@ -53,10 +53,10 @@ pub mod cosmic_atspi_v1 {
let virtual_mods = message.array()?;
let key = message.uint()?;
tracing::debug!(
"cosmic_atspi_manager_v1#{}.remove_key_grab({}, {}, {})",
"cosmic_atspi_manager_v1#{}.remove_key_grab({}, array[{}], {})",
object.id,
mods,
virtual_mods,
virtual_mods.len(),
key
);
self.remove_key_grab(object, client, mods, virtual_mods, key)
Expand Down Expand Up @@ -684,7 +684,7 @@ pub mod cosmic_output_management_unstable_v1 {
name: Option<String>,
) -> crate::server::Result<()> {
tracing::debug!(
"-> zcosmic_output_head_v1#{}.mirroring({})",
"-> zcosmic_output_head_v1#{}.mirroring(\"{}\")",
object.id,
name.as_ref().map_or("null".to_string(), |v| v.to_string())
);
Expand Down Expand Up @@ -1146,17 +1146,7 @@ pub mod cosmic_overlap_notify_unstable_v1 {
width: i32,
height: i32,
) -> crate::server::Result<()> {
tracing::debug!(
"-> zcosmic_overlap_notification_v1#{}.layer_enter({}, {}, {}, {}, {}, {}, {})",
object.id,
identifier,
exclusive,
layer,
x,
y,
width,
height
);
tracing :: debug ! ("-> zcosmic_overlap_notification_v1#{}.layer_enter(\"{}\", {}, {}, {}, {}, {}, {})" , object . id , identifier , exclusive , layer , x , y , width , height);
let (payload, fds) = crate::wire::PayloadBuilder::new()
.put_string(Some(identifier))
.put_uint(exclusive)
Expand All @@ -1179,7 +1169,7 @@ pub mod cosmic_overlap_notify_unstable_v1 {
identifier: String,
) -> crate::server::Result<()> {
tracing::debug!(
"-> zcosmic_overlap_notification_v1#{}.layer_leave({})",
"-> zcosmic_overlap_notification_v1#{}.layer_leave(\"{}\")",
object.id,
identifier
);
Expand Down Expand Up @@ -2338,7 +2328,7 @@ pub mod cosmic_toplevel_info_unstable_v1 {
title: String,
) -> crate::server::Result<()> {
tracing::debug!(
"-> zcosmic_toplevel_handle_v1#{}.title({})",
"-> zcosmic_toplevel_handle_v1#{}.title(\"{}\")",
object.id,
title
);
Expand All @@ -2362,7 +2352,7 @@ pub mod cosmic_toplevel_info_unstable_v1 {
app_id: String,
) -> crate::server::Result<()> {
tracing::debug!(
"-> zcosmic_toplevel_handle_v1#{}.app_id({})",
"-> zcosmic_toplevel_handle_v1#{}.app_id(\"{}\")",
object.id,
app_id
);
Expand Down
2 changes: 1 addition & 1 deletion src/server/protocol/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ pub mod hyprland_global_shortcuts_v1 {
let trigger_description = message
.string()?
.ok_or(crate::wire::DecodeError::MalformedPayload)?;
tracing :: debug ! ("hyprland_global_shortcuts_manager_v1#{}.register_shortcut({}, {}, {}, {}, {})" , object . id , shortcut , id , app_id , description , trigger_description);
tracing :: debug ! ("hyprland_global_shortcuts_manager_v1#{}.register_shortcut({}, \"{}\", \"{}\", \"{}\", \"{}\")" , object . id , shortcut , id , app_id , description , trigger_description);
self.register_shortcut(
object,
client,
Expand Down
Loading

0 comments on commit 5535fb8

Please sign in to comment.