Skip to content

Commit

Permalink
refactor!: use tag = "type" and rename_all = "camelCase" for serde (
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Sep 25, 2024
1 parent 512e9fb commit bf5cec4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .changes/serde-tagged.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
"tray-icon": minor
---

**Breaking change** Changed `serde` derive implementation for `TrayIconEvent` to use `serde(tag = "type")` and `rename_all = "camelCase"` on variants so the expected JSON serialization would look like this

```json
{
"type": "Click",
"button": "Left",
"buttonState": "Down",
"id": "some id",
"position": {
"x": 0,
"y": 0
},
"rect": {
"size": {
"width": 0,
"height": 0
},
"position": {
"x": 0,
"y": 0
}
}
}
```
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ winit = "0.29"
tao = "0.30"
image = "0.25"
eframe = "0.27"
serde_json = "1"
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@ impl TrayIcon {
/// and will still show a context menu on right click.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[non_exhaustive]
pub enum TrayIconEvent {
/// A click happened on the tray icon.
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
Click {
/// Id of the tray icon which triggered this event.
id: TrayIconId,
Expand Down Expand Up @@ -566,3 +568,45 @@ impl TrayIconEvent {
}
}
}

#[cfg(test)]
mod tests {

#[cfg(feature = "serde")]
#[test]
fn it_serializes() {
use super::*;
let event = TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Down,
id: TrayIconId::new("id"),
position: dpi::PhysicalPosition::default(),
rect: Rect::default(),
};

let value = serde_json::to_value(&event).unwrap();
assert_eq!(
value,
serde_json::json!({
"type": "Click",
"button": "Left",
"buttonState": "Down",
"id": "id",
"position": {
"x": 0.0,
"y": 0.0,
},
"rect": {
"size": {
"width": 0,
"height": 0,
},
"position": {
"x": 0.0,
"y": 0.0,
},
}
})
)
}
}

0 comments on commit bf5cec4

Please sign in to comment.