Skip to content

Commit

Permalink
feat: add serde feature implementation to able to serialize/deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed May 22, 2022
1 parent b5c6762 commit bdb7f09
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1 1,3 @@
{}
{
"rust-analyzer.cargo.features": "all"
}
102 changes: 73 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@ repository = "https://github.com/meskill/mystic-light-sdk"
license = "Apache-2.0"
keywords = ["mystic-light", "sdk", "rgb", "rgb-led"]
categories = ["api-bindings"]
include = ["/src/**", "README.md", "CHANGELOG.md"]
include = ["/src/**", "/examples/**", "README.md", "CHANGELOG.md"]

[badges]
maintenance = { status = "passively-maintained" }
Expand All @@ -18,8 18,19 @@ maintenance = { status = "passively-maintained" }
custom_error = "1.9.2"
libloading = "0.7.3"
oaidl = "0.2.1"
serde = { version="1.0.137", optional=true, features=["derive"] }
widestring = "0.4.3" # version compatible with `oaidl`
winapi = { version = "0.3.9" }

[dev-dependencies]
serde_json = { version="1.0.81" }

[build-dependencies]
copy_dir = "0.1.2"

[features]
serde = ["dep:serde"]

[[example]]
name = "serde_serialization"
required-features = ["serde"]
4 changes: 2 additions & 2 deletions examples/disable_light_for_5_sec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 15,10 @@ fn main() -> Result<(), CommonError> {

println!("{:#?}", devices);

let mut keyboard_leds = devices[2].leds()?;

println!("Second Device name is {}", devices[2].name());

let mut keyboard_leds = devices[2].leds()?;

println!("{:#?}", keyboard_leds);

println!(
Expand Down
31 changes: 31 additions & 0 deletions examples/serde_serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,31 @@
use mystic_light_sdk::MysticLightSDK;

const LIB_PATH: &str = if cfg!(target_arch = "x86_64") {
"../sdk/MysticLight_SDK_x64.dll"
} else {
"../sdk/MysticLight_SDK.dll"
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let sdk = MysticLightSDK::new(LIB_PATH)?;

let devices = sdk.get_devices()?;

println!("devices json: {}", serde_json::to_string_pretty(&devices)?);

let keyboard_leds = devices[2].leds()?;

println!(
"keyboard_leds json: {}",
serde_json::to_string_pretty(&keyboard_leds)?
);

let state = keyboard_leds[0].get_state()?;

println!(
"Current device state json: {}",
serde_json::to_string_pretty(&state)?
);

Ok(())
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 114,12 @@
//!
//! - in case of any problems with conversion from and into WinApi types
//!
//! # Features
//!
//! ## serde
//!
//! Enables [serde](https://crates.io/crates/serde) serialization/deserialization for some of the sdk structs
//!
//! # Troubleshooting
//!
//! ## Timeout error on initialization
Expand Down
26 changes: 26 additions & 0 deletions src/sdk/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 2,34 @@ pub type SingleColor = u32;

/// Represent RGB color
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
pub red: SingleColor,
pub green: SingleColor,
pub blue: SingleColor,
}

#[cfg(test)]
mod tests {

#[test]
#[cfg(feature = "serde")]
fn serde_serialization_deserialization() {
use super::Color;

let color = Color {
red: 25,
green: 220,
blue: 100,
};

let serialized_string = serde_json::to_string(&color).unwrap();

assert_eq!(serialized_string, "{\"red\":25,\"green\":220,\"blue\":100}");

assert_eq!(
serde_json::from_str::<Color>(&serialized_string).unwrap(),
color
);
}
}
6 changes: 5 additions & 1 deletion src/sdk/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 6,13 @@ use super::types::Result;
use libloading::Library;

/// Represents single hardware MysticLight Device
#[cfg_attr(feature="serde", derive(serde::Serialize))]
pub struct Device {
library: Rc<Library>,
name: String,

#[cfg_attr(feature="serde", serde(skip))]
library: Rc<Library>,
#[cfg_attr(feature="serde", serde(skip))]
led_count: u32,
}

Expand Down
Loading

0 comments on commit bdb7f09

Please sign in to comment.