ridiculous refactor to allow enums to be dbus strings for better TS generation

This commit is contained in:
Luke D. Jones
2023-06-27 21:16:13 +12:00
parent fca7d23a31
commit 7b17a13ce7
43 changed files with 1516 additions and 267 deletions

View File

@@ -31,6 +31,7 @@ serde.workspace = true
serde_derive.workspace = true
glam.workspace = true
typeshare.workspace = true
zbus = { workspace = true, optional = true }

View File

@@ -5,6 +5,7 @@ use std::time::{Duration, Instant};
use log::info;
use serde_derive::{Deserialize, Serialize};
use typeshare::typeshare;
#[cfg(feature = "dbus")]
use zbus::zvariant::Type;
@@ -27,7 +28,9 @@ pub const USB_PREFIX2: [u8; 7] = [0x5e, 0xc0, 0x02, 0x74, 0x02, 0x73, 0x02];
/// Third packet is for GA402 matrix
pub const USB_PREFIX3: [u8; 7] = [0x5e, 0xc0, 0x02, 0xe7, 0x04, 0x73, 0x02];
#[typeshare]
#[cfg_attr(feature = "dbus", derive(Type))]
#[typeshare]
#[derive(Default, Deserialize, PartialEq, Eq, Clone, Copy, Serialize, Debug)]
pub struct Animations {
pub boot: AnimBooting,
@@ -36,7 +39,9 @@ pub struct Animations {
pub shutdown: AnimShutdown,
}
#[typeshare]
#[cfg_attr(feature = "dbus", derive(Type))]
#[typeshare]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
pub struct DeviceState {
pub display_enabled: bool,
@@ -45,8 +50,9 @@ pub struct DeviceState {
pub builtin_anims: Animations,
}
#[cfg_attr(feature = "dbus", derive(Type))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[typeshare]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum AnimeType {
GA401,
GA402,

View File

@@ -33,3 +33,27 @@ pub mod error;
pub mod usb;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
// #[cfg(feature = "typescript")]
// #[cfg(test)]
// mod tests {
// use crate::usb::{AnimShutdown, Brightness, AnimSleeping, AnimAwake,
// AnimBooting}; use crate::{AnimeType, Animations, DeviceState};
// use ts_rs::SingleFileExporter;
// #[test]
// fn test_singlefile() {
// let out = SingleFileExporter::new(false)
// .and::<AnimeType>()
// .and::<Animations>()
// .and::<DeviceState>()
// .and::<Brightness>()
// .and::<AnimShutdown>()
// .and::<AnimSleeping>()
// .and::<AnimAwake>()
// .and::<AnimBooting>()
// .finish()
// .unwrap();
// println!("{out}");
// assert_eq!(out, "");
// }
// }

View File

@@ -11,6 +11,7 @@
use std::str::FromStr;
use serde_derive::{Deserialize, Serialize};
use typeshare::typeshare;
#[cfg(feature = "dbus")]
use zbus::zvariant::Type;
@@ -22,7 +23,8 @@ const DEV_PAGE: u8 = 0x5e;
pub const VENDOR_ID: u16 = 0x0b05;
pub const PROD_ID: u16 = 0x193b;
#[cfg_attr(feature = "dbus", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[typeshare]
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
/// Base LED brightness of the display
pub enum Brightness {
@@ -58,7 +60,8 @@ impl From<u8> for Brightness {
}
}
#[cfg_attr(feature = "dbus", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[typeshare]
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
pub enum AnimBooting {
#[default]
@@ -78,7 +81,8 @@ impl FromStr for AnimBooting {
}
}
#[cfg_attr(feature = "dbus", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[typeshare]
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
pub enum AnimAwake {
#[default]
@@ -98,7 +102,8 @@ impl FromStr for AnimAwake {
}
}
#[cfg_attr(feature = "dbus", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[typeshare]
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
pub enum AnimSleeping {
#[default]
@@ -118,7 +123,8 @@ impl FromStr for AnimSleeping {
}
}
#[cfg_attr(feature = "dbus", derive(Type))]
#[cfg_attr(feature = "dbus", derive(Type), zvariant(signature = "s"))]
#[typeshare]
#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Deserialize, Serialize)]
pub enum AnimShutdown {
#[default]