Expand description
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:
INTERFACEandMATCH_RULEconstants.- A
Signalenum 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(), anExamplestruct with anasyncmethod per D-Bus method, a getter and setter per property, andall_properties(). - With
server(), anExampleServertrait with anasyncmethod per D-Bus method and per property accessor, and adispatch()function which routes an incoming call to it.dispatch()also answersorg.freedesktop.DBus.Propertiesfor the interface.
§Type mapping
| D-Bus | Rust |
|---|---|
y | u8 |
b | bool |
n / q | i16 / u16 |
i / u | i32 / u32 |
x / t | i64 / u64 |
d | f64 |
s | String |
o | ObjectPathBuf |
g | SignatureBuf |
v | Value |
aT | Vec<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
GetLayoutoricon-nameinto 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.