Tuesday, September 5, 2023

BlueChi project and rust

 

# Rust
Rust developers can take advance of **dbus-codegen-rust** to generate a Rust module
and use,tag,release in any pace it's appropriate to their project.
## Generating bluechi module
Make sure a BlueChi node is running and execute the below commands.
``` bash
# dnf install rust cargo
# cargo install dbus-codegen
# dbus-codegen-rust -s -g -m None -d org.eclipse.bluechi -p /org/eclipse/bluechi > bluechi.rs
# mv bluechi.rs src/
```
### What's the functions names to be called?
In Sept 2023, the generator output was:
``` bash
pub trait OrgEclipseBluechiManager {
fn ping(&self, arg0: &str) -> Result<String, dbus::Error>;
fn list_units(&self) -> Result<Vec<(String, String, String, String, String, String, String, dbus::Path<'static>, u32, String, dbus::Path<'static>)>, dbus::Error>;
fn list_nodes(&self) -> Result<Vec<(String, dbus::Path<'static>, String)>, dbus::Error>;
fn get_node(&self, arg0: &str) -> Result<dbus::Path<'static>, dbus::Error>;
fn create_monitor(&self) -> Result<dbus::Path<'static>, dbus::Error>;
fn set_log_level(&self, arg0: &str) -> Result<(), dbus::Error>;
fn enable_metrics(&self) -> Result<(), dbus::Error>;
fn disable_metrics(&self) -> Result<(), dbus::Error>;
}
```
### How to use it?
As every project is different, here a tradicional example of importing the generated module in a **main.rs**:
**Dir structure**:
```
.
├── Cargo.toml
└── src
├── bluechi.rs
└── main.rs
```
Cargo.toml
```
[package]
name = "bluechi"
version = "0.1.0"
authors = [
"Douglas Schilling Landgraf <dougsland@redhat.com>",
]
description = "List all BlueChi nodes"
repository = "https://github.com/containers/bluechi"
readme = "README.md"
license = "LGPLv2"
categories = ["os::unix-apis"]
keywords = ["linux", "systemd", "dbus"]
edition = "2021"
[dependencies]
dbus = { version = "0.9.7", features = [] }
```
As soon the files **bluechi.rs** and [main.rs](./src/main.rs) were copies to `src/` it's possible to compile a new binary.
**Generating the binary**:
``` bash
# dnf install rust cargo
# pwd
$HOME/bluechi/src/bindings/rust
# cargo run
Updating crates.io index
Compiling pkg-config v0.3.27
Compiling libc v0.2.147
Compiling libdbus-sys v0.2.5
Compiling dbus v0.9.7
Compiling bluechi v0.1.0 (/root/rust/src/bluechi/src/bindings/rust)
Finished dev [unoptimized + debuginfo] target(s) in 7.48s
Running `target/debug/bluechi`
Node: "control"
Status: "online"
Path("/org/eclipse/bluechi/node/control\0")
Node: "node1"
Status: "offline"
Path("/org/eclipse/bluechi/node/node1\0")
```

No comments:

Post a Comment