valpackett / systemstat

Rust library for getting system information | also on https://codeberg.org/valpackett/systemstat
https://crates.io/crates/systemstat
The Unlicense
616 stars 71 forks source link

Added Serialize and Deserialize traits #85

Closed Techno-Fox closed 3 years ago

Techno-Fox commented 3 years ago

serde is the most popular crate for serializing and de-serializing structs, enums, etc. I added them to the public facing data types so users can serialize and de-serialize the data returned from this crate.

Techno-Fox commented 3 years ago

I'm gonna need some help with this.

Here's how the lib.rs is looking like

//! This library provides a way to access system information such as CPU load, mounted filesystems,
//! network interfaces, etc.

#[cfg(windows)]
extern crate winapi;
extern crate libc;
extern crate time;
extern crate chrono;
extern crate bytesize;
#[cfg_attr(any(target_os = "freebsd", target_os = "openbsd", target_os = "macos"), macro_use)]
extern crate lazy_static;
#[cfg(any(target_os = "linux", target_os = "android"))]
extern crate nom;
#[cfg(feature = "serde")]
extern crate real_serde as serde;

pub mod platform;
pub mod data;

pub use self::platform::Platform;
pub use self::platform::PlatformImpl as System;
pub use self::data::*;

Cargo.toml :

[package]
name = "systemstat"
version = "0.1.7"
authors = [ "Greg V <greg@unrelenting.technology>" ]
keywords = [ "System", "Info" ]
description = "systemstat"
license = "Unlicense"
readme = "README.md"
homepage = "https://github.com/myfreeweb/systemstat"
repository = "https://github.com/myfreeweb/systemstat"

[dependencies]
time = "0.1"
chrono = "0.4"
lazy_static = "1.0"
bytesize = "1.0"
libc = "0.2"
real_serde = { version = "1.0", package = "serde", features = ["derive"], optional = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
nom = "6.0"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["fileapi", "sysinfoapi", "minwindef", "winbase", "winerror", "ws2def", "ws2ipdef", "pdh"]

[package.metadata.docs.rs]
targets = [
    "x86_64-unknown-freebsd",
    "x86_64-unknown-openbsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-apple-darwin",
    "x86_64-pc-windows-msvc"
]

[features]
serde = ["real_serde", "bytesize/serde", "chrono/serde"]

I added #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] to the data types.

Now I'm getting a compilation error

[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

                                                                            ^^^^^^^^^^^ can't find crate

Screenshot_20210531_145156

valpackett commented 3 years ago

Hm, maybe https://crates.io/crates/serde-feature-hack actually is necessary.

Techno-Fox commented 3 years ago

Same error happens (Sorry it took so long to comment back)

extern crate serde_feature_hack as serde;
[package]
name = "systemstat"
version = "0.1.7"
authors = [ "Greg V <greg@unrelenting.technology>" ]
keywords = [ "System", "Info" ]
description = "systemstat"
license = "Unlicense"
readme = "README.md"
homepage = "https://github.com/myfreeweb/systemstat"
repository = "https://github.com/myfreeweb/systemstat"

[dependencies]
time = "0.1"
chrono = "0.4"
lazy_static = "1.0"
bytesize = "1.0"
libc = "0.2"
serde-feature-hack = { version = "0.1.0", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
nom = "6.0"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["fileapi", "sysinfoapi", "minwindef", "winbase", "winerror", "ws2def", "ws2ipdef", "pdh"]

[package.metadata.docs.rs]
targets = [
    "x86_64-unknown-freebsd",
    "x86_64-unknown-openbsd",
    "x86_64-unknown-linux-gnu",
    "x86_64-apple-darwin",
    "x86_64-pc-windows-msvc"
]

[features]
serde = ["serde-feature-hack", "bytesize/serde", "chrono/serde"]
valpackett commented 3 years ago

Looks like the documentation there is outdated, you need version 0.2, and then it would be just extern crate serde (no as!) and macros work fine.

Techno-Fox commented 3 years ago

it works. I'll commit changes

valpackett commented 3 years ago

Thanks!

willhughes11 commented 1 year ago

New to Rust, trying to get the gated serde serializes to work, so I can serialize the platform objects but they don't. Here is what my cargo.toml looks like.

` [package] name = "events" version = "0.1.0" edition = "2021"

[dependencies] systemstat = "0.2.2" rusqlite = { version = "0.28.0", features = ["vtab", "csvtab", "modern_sqlite", "bundled"] } json = "0.12.4" log = "0.4.17" env_logger = "0.9.3" nanoid = "0.4.0" serde = { version = "1.0.148", features = ["derive"], optional = true } serde_json = "1.0.64" regex = "1.7.0"

[features] default = ["serde"] `

Error: the trait boundNetwork: Serializeis not satisfied the following other types implement traitSerialize:

valpackett commented 1 year ago

@willhughes11 you haven't enabled the serde feature in systemstat, it should be

default = ["serde", "systemstat/serde"]