This repo contains the sources for the code in the "A Rust API Inspired by Python, Powered by Serde" article (link).
It shows how to use Serde's traits to turn this "raw" API:
let res = raw_api::query("SELECT * FROM Win32_Fan");
for obj in res {
if obj.get_attr("ActiveCooling") == Value::Bool(true) {
if let Value::String(name) = obj.get_attr("Name") {
if let Value::UI8(speed) = obj.get_attr("DesiredSpeed") {
println!("Fan `{name}` is running at {speed} RPM");
}
}
}
}
Into this API:
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Fan { .. }
let res: Vec<Fan> = query();
for fan in res {
if fan.active_cooling {
println!(
"Fan `{}` is running at {} RPM",
fan.name, fan.desired_speed
);
}
}
It is based on work done for the wmi-rs
crate.
raw_api.rs
: The underlying API which returnObject
s andValue
s, which require verbose and error prone handling.v1_api.rs
: A betterquery
function with a custom trait which needs to be manually implemented by the user'sstructs
.v2_api.rs
: The complete Serde-enabledquery
function.meta.rs
: A deserializer for getting the name (and fields) of a structure as&str
s.