Skip to content
Draft
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ default-members = [
"gdk-pixbuf/sys",
"gio",
"gio/sys",
"gio-macros",
"gio-unix",
"gio-unix/sys",
"glib-build-tools",
Expand All @@ -33,6 +34,7 @@ members = [
"gdk-pixbuf/sys",
"gio",
"gio/sys",
"gio-macros",
"gio-unix",
"gio-unix/sys",
"gio-win32",
Expand Down Expand Up @@ -79,6 +81,7 @@ glib = { path = "glib", version = "0.23.0-alpha", default-features = false }
glib-unix = { path = "glib-unix", version = "0.23.0-alpha" }
glib-win32 = { path = "glib-win32", version = "0.23.0-alpha" }
gio-sys = { path = "gio/sys", version = "0.23.0-alpha" }
gio-macros = { path = "gio-macros", version = "0.23.0-alpha", default-features = false }
gio-unix-sys = { path = "gio-unix/sys", version = "0.23.0-alpha" }
gio-win32-sys = { path = "gio-win32/sys", version = "0.23.0-alpha" }
gio = { path = "gio", version = "0.23.0-alpha" }
Expand Down
37 changes: 37 additions & 0 deletions examples/gio_dbus_register_object/hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use gio::DBusCallFlags;
use std::time::Duration;

#[gio::dbus_interface(
name = "com.github.gtk_rs.examples.HelloWorld",
type_name = "GtkRustHelloWorld",
emits_changed_signal = "true",
// crate = gio,
)]
pub(crate) trait HelloWorld {
#[deprecated]
fn hello(&self, name: String) -> String;

#[dbus(name = "Hello2", out_args("name", "effective_delay"))]
fn hello_with_delay(&self, name: String, delay: u32) -> (String, u32);

#[dbus(no_reply)]
fn no_greeting(&self, name: String);

#[dbus(property, get, name = "Name")]
fn last_greeted_name(&self) -> String;

#[dbus(property, get)]
#[deprecated]
fn count(&self) -> i64;

#[dbus(signal)]
fn greeted(&self, name: String);
}

async fn hello_tau(iface: &HelloWorld) -> Result<String, glib::Error> {
iface
.hello("Tau".to_owned())
.timeout(Duration::from_secs(5))
.flags(DBusCallFlags::ALLOW_INTERACTIVE_AUTHORIZATION)
.await
}
2 changes: 2 additions & 0 deletions examples/gio_dbus_register_object/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use gio::prelude::*;

mod hello_world;

glib::wrapper! {
pub struct SampleApplication(ObjectSubclass<imp::SampleApplication>)
@extends gio::Application,
Expand Down
1 change: 1 addition & 0 deletions gio-macros/COPYRIGHT
31 changes: 31 additions & 0 deletions gio-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "gio-macros"
documentation = "https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio_macros/"
description = "Rust bindings for the Gio library, proc macros crate"
keywords = ["glib", "gio", "gtk-rs", "gnome"]
authors.workspace = true
edition.workspace = true
exclude.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[features]
default = ["proc_macro_crate"]
proc_macro_crate = ["proc-macro-crate"]

[dependencies]
heck = "0.5"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0.113", features = ["full"] }
proc-macro-crate = { version = "3.5", optional = true }

[lib]
proc-macro = true

[dev-dependencies]
gio.workspace = true
trybuild2 = "1.3"
1 change: 1 addition & 0 deletions gio-macros/LICENSE
30 changes: 30 additions & 0 deletions gio-macros/src/dbus_interface/attributes/argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::dbus_interface::attributes::ATTRIBUTE_NAME;
use syn::{Attribute, LitStr};

pub(crate) struct DBusMethodArgumentAttribute {
pub(crate) name: Option<LitStr>,
}

impl DBusMethodArgumentAttribute {
pub(crate) fn parse(attrs: &[Attribute]) -> syn::Result<Self> {
let mut name: Option<LitStr> = None;
for attr in attrs {
if attr.path().is_ident(ATTRIBUTE_NAME) {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("name") {
name = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error(format!(
"unknown attribute `{}`. Possible attributes are `name`",
meta.path.get_ident().unwrap(),
)))
}
})?;
}
}
Ok(Self { name })
}
}
70 changes: 70 additions & 0 deletions gio-macros/src/dbus_interface/attributes/dbus_interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::dbus_interface::attributes::EmitsChangedSignal;
use proc_macro2::TokenStream;
use syn::spanned::Spanned;
use syn::{LitStr, Path};

/// The `#[dbus_interface(...)]` attribute.
pub(crate) struct DBusInterfaceAttribute {
pub(crate) name: LitStr,

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / build

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (glib-build-tools, false)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pango, true, --features v1_52)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pango, true, --features v1_52)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pangocairo, true)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pangocairo, true)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gdk-pixbuf, true, --features v2_42)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gdk-pixbuf, true, --features v2_42)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (examples, false, --bins --examples --all-features)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio, true, --features v2_80)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio, true, --features v2_80)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio-win32, true, --features v2_84)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read

Check warning on line 10 in gio-macros/src/dbus_interface/attributes/dbus_interface.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio-win32, true, --features v2_84)

fields `name`, `emits_changed_signal`, `generate_proxy`, and `generate_skeleton` are never read
pub(crate) type_name: Option<LitStr>,
pub(crate) crate_: Option<Path>,
pub(crate) emits_changed_signal: Option<EmitsChangedSignal>,
pub(crate) generate_proxy: bool,
pub(crate) generate_skeleton: bool,
}

impl DBusInterfaceAttribute {
pub(crate) fn parse(attr: TokenStream) -> syn::Result<Self> {
let span = attr.span();
let mut name: Option<LitStr> = None;
let mut type_name: Option<LitStr> = None;
let mut crate_: Option<Path> = None;
let mut emits_changed_signal: Option<EmitsChangedSignal> = None;
let mut generate_proxy = false;
let mut generate_skeleton = false;
let p = syn::meta::parser(|meta| {
if meta.path.is_ident("name") {
name = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("type_name") {
type_name = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("crate") {
crate_ = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("emits_changed_signal") {
emits_changed_signal = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("proxy") {
generate_proxy = true;
Ok(())
} else if meta.path.is_ident("skeleton") {
generate_skeleton = true;
Ok(())
} else {
Err(meta.error(format!(
"unknown attribute `{}`. Possible attributes are `name`, `type_name`, `crate`, `emits_changed_signal`",
meta.path.get_ident().unwrap(),
)))
}
});
syn::parse::Parser::parse2(p, attr)?;

let Some(name) = name else {
return Err(syn::Error::new(
span,
"attribute `#[dbus_interface(name = ...)]` must be specified",
));
};
Ok(Self {
name,
type_name,
crate_,
emits_changed_signal,
generate_proxy,
generate_skeleton,
})
}
}
45 changes: 45 additions & 0 deletions gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::Span;
use syn::LitStr;
use syn::parse::Parse;

#[derive(Clone)]
pub(crate) struct EmitsChangedSignal {
pub(crate) kind: EmitsChangedSignalKind,

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / build

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (glib-build-tools, false)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pango, true, --features v1_52)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pango, true, --features v1_52)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pangocairo, true)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (pangocairo, true)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gdk-pixbuf, true, --features v2_42)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gdk-pixbuf, true, --features v2_42)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (examples, false, --bins --examples --all-features)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio, true, --features v2_80)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio, true, --features v2_80)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio-win32, true, --features v2_84)

fields `kind` and `span` are never read

Check warning on line 9 in gio-macros/src/dbus_interface/attributes/emits_changed_signal.rs

View workflow job for this annotation

GitHub Actions / Build gtk-rs on Windows (gio-win32, true, --features v2_84)

fields `kind` and `span` are never read
pub(crate) span: Span,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum EmitsChangedSignalKind {
True,
Invalidates,
Const,
False,
}

impl Parse for EmitsChangedSignal {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
use EmitsChangedSignalKind::*;
let lit: LitStr = input.parse()?;
let value = lit.value();
let kind = match value.as_str() {
"true" => True,
"invalidates" => Invalidates,
"const" => Const,
"false" => False,
_ => {
return Err(syn::Error::new(
lit.span(),
format!(
"unknown value {value:?}. Possible values are \"true\", \"invalidates\", \"const\", \"false\""
),
));
}
};
Ok(EmitsChangedSignal {
kind,
span: lit.span(),
})
}
}
Loading
Loading