8000 GitHub - ohadravid/serde-reflect: Code for an article about using Serde for reflection
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ohadravid/serde-reflect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

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.

Code Structure

  • raw_api.rs: The underlying API which return Objects and Values, which require verbose and error prone handling.
  • v1_api.rs: A better query function with a custom trait which needs to be manually implemented by the user's structs.
  • v2_api.rs: The complete Serde-enabled query function.
  • meta.rs: A deserializer for getting the name (and fields) of a structure as &strs.

About

Code for an article about using Serde for reflection

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0