Skip to main content

Crate tokio_dbus_codegen

Crate tokio_dbus_codegen 

Source
Expand description

github crates.io docs.rs

Generate asynchronous D-Bus clients and servers from interface files at build time.

The generated code speaks in owned Rust types String, Vec, and HashMap and tuples and is driven by a tokio_dbus_runtime::Connection, which both crates a consumer needs to depend on.


§Using it from a build script

Add the generator as a build dependency and the runtime as a regular one:

[dependencies]
tokio-dbus-runtime = "0.1.1"

[build-dependencies]
tokio-dbus-codegen = "0.1.1"

Then write a build.rs which names the interface files to read and what to generate for each interface in them:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tokio_dbus_codegen::Builder::new()
        .file("interfaces/org.freedesktop.Notifications.xml")
        .client("org.freedesktop.Notifications")
        .generate("notifications.rs")?;

    Ok(())
}

Finally include the generated file:

include!(concat!(env!("OUT_DIR"), "/notifications.rs"));

§What is generated

For an interface com.example.Example, a module example is generated holding:

  • INTERFACE and MATCH_RULE constants.
  • A Signal enum with a variant per signal, which can be decoded from an incoming message and emitted from an outgoing one. Present whenever the interface has signals.
  • With client(), an Example struct with an async method per D-Bus method, a getter and setter per property, and all_properties().
  • With server(), an ExampleServer trait with an async method per D-Bus method and per property accessor, and a dispatch() function which routes an incoming call to it. dispatch() also answers org.freedesktop.DBus.Properties for the interface.

§Type mapping

D-BusRust
yu8
bbool
n / qi16 / u16
i / ui32 / u32
x / ti64 / u64
df64
sString
oObjectPathBuf
gSignatureBuf
vValue
aTVec<T>
a{KV}HashMap<K, V>
(T1 T2)(T1, T2)

Client methods take the borrowed form of each argument where borrowing is free, so an s is passed as a &str and an as as a &[String].

Passing a file descriptor (h) is not supported.

Structs§

Builder
Reads interface files and generates bindings for the interfaces in them.
Error
An error raised while generating code.
Mode
What to generate for an interface.

Functions§

argument_name
The name of an argument, falling back to a positional name when the interface file does not give one.
module_name
The name of the module generated for an interface, derived from its last segment.
owned_type
The owned Rust type a value of this signature is decoded into.
parameter_type
The Rust type a client takes for an argument of this signature, which borrows where borrowing is free.
pascal_case
Convert a D-Bus name into a Pascal case Rust identifier, which is what enum variants and generated types are named after.
snake_case
Convert a D-Bus name such as GetLayout or icon-name into a snake case Rust identifier, escaping it if it collides with a keyword.

Type Aliases§

Result
Result alias defaulting to the error type of this crate.