Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
fix: fix (de)serialization of EventFilter
Fintan Halpenny committed 1 year ago
commit d31347d1798028bd1a8d963774287beb31c4c912
parent d0c6a8c8c1e817ded633ef4f625f3da488df77a0
1 file changed +32 -12
modified src/filter.rs
@@ -37,7 +37,7 @@ impl From<&TriggerConfig> for Trigger {
}

/// A Boolean expression for filtering broker events.
-
#[derive(Debug, Clone, Deserialize, Serialize)]
+
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub enum EventFilter {
    /// Event for a specific repository.
@@ -74,13 +74,13 @@ pub enum EventFilter {
    Deny,

    /// Allow the opposite of the contained filter.
-
    Not(Box<Self>),
+
    Not(#[serde(with = "serde_yml::with::singleton_map")] Box<Self>),

    /// Allow if all contained filters allow.
-
    And(Vec<Box<Self>>),
+
    And(Vec<EventFilter>),

    /// Allow if any contained filter allow.
-
    Or(Vec<Box<Self>>),
+
    Or(Vec<EventFilter>),
}

impl EventFilter {
@@ -582,10 +582,7 @@ mod test {

    #[test]
    fn allows_if_all_allow() {
-
        let filter = EventFilter::And(vec![
-
            Box::new(EventFilter::Allow),
-
            Box::new(EventFilter::Allow),
-
        ]);
+
        let filter = EventFilter::And(vec![EventFilter::Allow, EventFilter::Allow]);

        eprintln!("filter: {filter:#?}");
        for e in all_events(did(), rid(), refstring("main"), patch_id(), oid(), oid()).iter() {
@@ -596,10 +593,7 @@ mod test {

    #[test]
    fn allows_if_any_allows() {
-
        let filter = EventFilter::Or(vec![
-
            Box::new(EventFilter::Deny),
-
            Box::new(EventFilter::Allow),
-
        ]);
+
        let filter = EventFilter::Or(vec![EventFilter::Deny, EventFilter::Allow]);

        eprintln!("filter: {filter:#?}");
        for e in all_events(did(), rid(), refstring("main"), patch_id(), oid(), oid()).iter() {
@@ -607,6 +601,32 @@ mod test {
            assert!(filter.allows(e));
        }
    }
+

+
    /// This test ensures that we can deserialize a nested enum, which is only
+
    /// possible using the `serde_yml::with::singleton_map` helper on the
+
    /// `EventFilter::Not` variant.
+
    #[test]
+
    fn deserialize_yaml_nested_not() {
+
        let expected = EventFilter::And(vec![
+
            EventFilter::Not(Box::new(EventFilter::Repository(
+
                "rad:z32iyJDyFLqvPFzwHm8YadK4HQ2EY"
+
                    .parse::<RepoId>()
+
                    .unwrap(),
+
            ))),
+
            EventFilter::BranchCreated,
+
            EventFilter::PatchCreated,
+
        ]);
+
        let filters = r#"
+
!And
+
- !Not
+
    Repository: rad:z32iyJDyFLqvPFzwHm8YadK4HQ2EY
+
- !BranchCreated
+
- !PatchCreated
+
"#;
+
        let evf = serde_yml::from_str::<EventFilter>(filters);
+
        assert!(evf.is_ok(), "Failed to deserialize filters: {evf:?}");
+
        assert_eq!(evf.unwrap(), expected);
+
    }
}

#[derive(Debug, thiserror::Error)]