Proc Macros for Nadi System Plugins
Note:
This crate is re-exported by the nadi_core library, do not use this directly.
Introduction
This crate contains the necessary macros for the nadi system plugin
development in rust.
The plugins can be developed without using the macros, but this makes
it concise, less error prone, and easier to upgrade to new versions.
Example Plugin:
Cargo.toml:
[package]
name = "example_plugin"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
anyhow = "1.0.86"
nadi_core = "0.7.0"
src/lib.rs:
use nadi_core::nadi_plugin::nadi_plugin;
#[nadi_plugin]
mod example {
use nadi_core::{Network, NodeInner};
use nadi_core::nadi_plugin::{network_func, node_func};
#[node_func(key=true)]
fn print_attr(
node: &NodeInner,
attr: String,
key: bool
) {
println!(
"{}{}",
if key { &attr } else { "" },
node.attr(&attr).map(|a| a.to_string()).unwrap_or_default()
);
}
#[node_func]
fn list_attr(node: &NodeInner) {
let attrs: Vec<&str> = node.attrs().iter().map(|kv| kv.0.as_str()).collect();
println!("{}: {}", node.name(), attrs.join(", "));
}
#[network_func]
fn print_net_attr(net: &Network, attr: String) {
for node in net.nodes() {
let node = node.lock();
println!(
"{}: {}",
node.name(),
node.attr(&attr).map(|a| a.to_string()).unwrap_or_default()
);
}
}
}