Skip to content

Commit

Permalink
Improve serialization (notify-rs#558)
Browse files Browse the repository at this point in the history
* Add event kind serialization tests

* Serialize events

* Flatten event kind during serialization

* Serialize event with attributes

* Serialize flags as camelCase
  • Loading branch information
dfaust authored Jan 23, 2024
1 parent 80aaee2 commit 43efec9
Show file tree
Hide file tree
Showing 44 changed files with 287 additions and 1 deletion.
2 changes: 2 additions & 0 deletions notify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mio = { version = "0.8", features = ["os-ext"] }
serde_json = "1.0.39"
tempfile = "3.2.0"
nix = "0.23.1"
insta = "1.34.0"
rstest = "0.17.0"

[features]
default = ["macos_fsevent","crossbeam-channel"]
Expand Down
75 changes: 74 additions & 1 deletion notify/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub enum RemoveKind {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
pub enum EventKind {
/// The catch-all event kind, for unsupported/unknown events.
///
Expand Down Expand Up @@ -304,7 +305,7 @@ pub struct Event {
/// The `EventKind::Any` variant should be used as the "else" case when mapping native kernel
/// bitmasks or bitmaps, such that if the mask is ever extended with new event types the
/// backend will not gain bugs due to not matching new unknown event types.
#[cfg_attr(feature = "serde", serde(rename = "type"))]
#[cfg_attr(feature = "serde", serde(flatten))]
pub kind: EventKind,

/// Paths the event is about, if known.
Expand Down Expand Up @@ -483,6 +484,7 @@ impl EventAttributes {
/// particular ways.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum Flag {
/// Rescan notices are emitted by some platforms (and may also be emitted by Notify itself).
/// They indicate either a lapse in the events or a change in the filesystem such that events
Expand Down Expand Up @@ -621,3 +623,74 @@ impl Hash for Event {
self.source().hash(state);
}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;

use insta::assert_snapshot;
use rstest::rstest;

#[rustfmt::skip]
#[rstest]
#[case("any", EventKind::Any)]
#[case("access-any", EventKind::Access(AccessKind::Any))]
#[case("access-read", EventKind::Access(AccessKind::Read))]
#[case("access-open-any", EventKind::Access(AccessKind::Open(AccessMode::Any)))]
#[case("access-open-execute", EventKind::Access(AccessKind::Open(AccessMode::Execute)))]
#[case("access-open-read", EventKind::Access(AccessKind::Open(AccessMode::Read)))]
#[case("access-open-write", EventKind::Access(AccessKind::Open(AccessMode::Write)))]
#[case("access-open-other", EventKind::Access(AccessKind::Open(AccessMode::Other)))]
#[case("access-close-any", EventKind::Access(AccessKind::Close(AccessMode::Any)))]
#[case("access-close-execute", EventKind::Access(AccessKind::Close(AccessMode::Execute)))]
#[case("access-close-read", EventKind::Access(AccessKind::Close(AccessMode::Read)))]
#[case("access-close-write", EventKind::Access(AccessKind::Close(AccessMode::Write)))]
#[case("access-close-other", EventKind::Access(AccessKind::Close(AccessMode::Other)))]
#[case("access-other", EventKind::Access(AccessKind::Other))]
#[case("create-any", EventKind::Create(CreateKind::Any))]
#[case("create-file", EventKind::Create(CreateKind::File))]
#[case("create-folder", EventKind::Create(CreateKind::Folder))]
#[case("create-other", EventKind::Create(CreateKind::Other))]
#[case("modify-any", EventKind::Modify(ModifyKind::Any))]
#[case("modify-data-any", EventKind::Modify(ModifyKind::Data(DataChange::Any)))]
#[case("modify-data-size", EventKind::Modify(ModifyKind::Data(DataChange::Size)))]
#[case("modify-data-content", EventKind::Modify(ModifyKind::Data(DataChange::Content)))]
#[case("modify-data-other", EventKind::Modify(ModifyKind::Data(DataChange::Other)))]
#[case("modify-metadata-any", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)))]
#[case("modify-metadata-accesstime", EventKind::Modify(ModifyKind::Metadata(MetadataKind::AccessTime)))]
#[case("modify-metadata-writetime", EventKind::Modify(ModifyKind::Metadata(MetadataKind::WriteTime)))]
#[case("modify-metadata-permissions", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Permissions)))]
#[case("modify-metadata-ownership", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Ownership)))]
#[case("modify-metadata-extended", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Extended)))]
#[case("modify-metadata-other", EventKind::Modify(ModifyKind::Metadata(MetadataKind::Other)))]
#[case("modify-name-any", EventKind::Modify(ModifyKind::Name(RenameMode::Any)))]
#[case("modify-name-to", EventKind::Modify(ModifyKind::Name(RenameMode::To)))]
#[case("modify-name-from", EventKind::Modify(ModifyKind::Name(RenameMode::From)))]
#[case("modify-name-both", EventKind::Modify(ModifyKind::Name(RenameMode::Both)))]
#[case("modify-name-other", EventKind::Modify(ModifyKind::Name(RenameMode::Other)))]
#[case("modify-other", EventKind::Modify(ModifyKind::Other))]
#[case("remove-any", EventKind::Remove(RemoveKind::Any))]
#[case("remove-file", EventKind::Remove(RemoveKind::File))]
#[case("remove-folder", EventKind::Remove(RemoveKind::Folder))]
#[case("remove-other", EventKind::Remove(RemoveKind::Other))]
#[case("other", EventKind::Other)]
fn serialize_event_kind(
#[case] name: &str,
#[case] event_kind: EventKind,
) {
let event = Event::new(event_kind);
let json = serde_json::to_string(&event).unwrap();
assert_snapshot!(name, json);
}

#[test]
fn serialize_event_with_attrs() {
let event = Event::new(EventKind::Any)
.set_tracker(123)
.set_flag(Flag::Rescan)
.set_info("test event")
.set_process_id(0);
let json = serde_json::to_string(&event).unwrap();
assert_snapshot!(json);
}
}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__access-any.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"close","mode":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"close","mode":"execute","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"close","mode":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"close","mode":"read","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"close","mode":"write","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"open","mode":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"open","mode":"execute","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"open","mode":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"open","mode":"read","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"open","mode":"write","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__access-other.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"other","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__access-read.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"access","kind":"read","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__any.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"any","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__create-any.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"create","kind":"any","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__create-file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"create","kind":"file","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__create-folder.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"create","kind":"folder","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__create-other.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"create","kind":"other","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__modify-any.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"data","mode":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"data","mode":"content","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"data","mode":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"data","mode":"size","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"access-time","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"extended","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"ownership","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"permissions","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"metadata","mode":"write-time","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"rename","mode":"any","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"rename","mode":"both","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"rename","mode":"from","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"rename","mode":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"rename","mode":"to","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__modify-other.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"modify","kind":"other","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__other.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"other","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__remove-any.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"remove","kind":"any","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__remove-file.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"remove","kind":"file","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__remove-folder.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"remove","kind":"folder","paths":[],"attrs":{}}
5 changes: 5 additions & 0 deletions notify/src/snapshots/notify__event__tests__remove-other.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: notify/src/event.rs
expression: json
---
{"type":"remove","kind":"other","paths":[],"attrs":{}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: notify/src/event.rs
assertion_line: 694
expression: json
---
{"type":"any","paths":[],"attrs":{"tracker":123,"flag":"rescan","info":"test event"}}

0 comments on commit 43efec9

Please sign in to comment.