falco_event/events/
event.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::event_derive::{EventMetadata, Format, PayloadToBytes};
use crate::events::to_bytes::EventToBytes;
use crate::types::format::format_type;
use std::fmt::{Debug, Formatter};
use std::io::Write;

#[derive(Clone)]
pub struct Event<T> {
    pub metadata: EventMetadata,
    pub params: T,
}

impl<T: Debug + Format<format_type::PF_NA>> Debug for Event<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            f.debug_struct("Event")
                .field("metadata", &self.metadata)
                .field("params", &self.params)
                .finish()
        } else {
            self.format(f)
        }
    }
}

impl<T: PayloadToBytes> EventToBytes for Event<T> {
    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
        self.params.write(&self.metadata, writer)
    }
}

impl<T, F> Format<F> for Event<T>
where
    EventMetadata: Format<F>,
    T: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        self.metadata.format(fmt)?;
        fmt.write_str(" ")?;
        self.params.format(fmt)
    }
}

#[cfg(feature = "serde")]
mod serde_event {
    use super::*;
    use crate::events::types::AnyEvent;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    #[derive(Serialize)]
    struct BorrowedSerializableEvent<'a> {
        #[serde(flatten)]
        pub metadata: &'a EventMetadata,
        #[serde(flatten)]
        pub params: &'a AnyEvent<'a>,
    }

    impl Serialize for Event<AnyEvent<'_>> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let se = BorrowedSerializableEvent {
                metadata: &self.metadata,
                params: &self.params,
            };

            se.serialize(serializer)
        }
    }

    #[derive(Serialize)]
    struct OwnedSerializableEvent<'a> {
        #[serde(flatten)]
        pub metadata: &'a EventMetadata,
        #[serde(flatten)]
        pub params: &'a crate::events::types::owned::AnyEvent,
    }

    impl Serialize for Event<crate::events::types::owned::AnyEvent> {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let se = OwnedSerializableEvent {
                metadata: &self.metadata,
                params: &self.params,
            };

            se.serialize(serializer)
        }
    }

    #[derive(Deserialize)]
    struct OwnedDeserializableEvent {
        #[serde(flatten)]
        pub metadata: EventMetadata,
        #[serde(flatten)]
        pub params: crate::events::types::owned::AnyEvent,
    }

    impl<'de> Deserialize<'de> for Event<crate::events::types::owned::AnyEvent> {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            let de: OwnedDeserializableEvent = Deserialize::deserialize(deserializer)?;

            Ok(Self {
                metadata: de.metadata,
                params: de.params,
            })
        }
    }
}