relay_base_schema/
events.rs1use std::fmt;
7use std::str::FromStr;
8
9use relay_protocol::{Annotated, Empty, ErrorKind, FromValue, IntoValue, SkipSerialization, Value};
10use serde::{Deserialize, Serialize};
11
12#[derive(
25 Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize, Default,
26)]
27#[serde(rename_all = "lowercase")]
28pub enum EventType {
29 Error,
31 Csp,
33 Hpkp,
35 ExpectCt,
37 ExpectStaple,
39 Transaction,
41 UserReportV2,
45 #[serde(other)]
47 #[default]
48 Default,
49}
50
51impl EventType {
52 pub fn as_str(&self) -> &'static str {
54 match self {
55 EventType::Default => "default",
56 EventType::Error => "error",
57 EventType::Csp => "csp",
58 EventType::Hpkp => "hpkp",
59 EventType::ExpectCt => "expectct",
60 EventType::ExpectStaple => "expectstaple",
61 EventType::Transaction => "transaction",
62 EventType::UserReportV2 => "feedback",
63 }
64 }
65}
66
67#[derive(Clone, Copy, Debug)]
69pub struct ParseEventTypeError;
70
71impl fmt::Display for ParseEventTypeError {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 write!(f, "invalid event type")
74 }
75}
76
77impl std::error::Error for ParseEventTypeError {}
78
79impl FromStr for EventType {
80 type Err = ParseEventTypeError;
81
82 fn from_str(string: &str) -> Result<Self, Self::Err> {
83 Ok(match string {
84 "default" => EventType::Default,
85 "error" => EventType::Error,
86 "csp" => EventType::Csp,
87 "hpkp" => EventType::Hpkp,
88 "expectct" => EventType::ExpectCt,
89 "expectstaple" => EventType::ExpectStaple,
90 "transaction" => EventType::Transaction,
91 "feedback" => EventType::UserReportV2,
92 _ => return Err(ParseEventTypeError),
93 })
94 }
95}
96
97impl fmt::Display for EventType {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 write!(f, "{}", self.as_str())
100 }
101}
102
103impl Empty for EventType {
104 #[inline]
105 fn is_empty(&self) -> bool {
106 false
107 }
108}
109
110impl FromValue for EventType {
111 fn from_value(value: Annotated<Value>) -> Annotated<Self> {
112 match String::from_value(value) {
113 Annotated(Some(value), mut meta) => match value.parse() {
114 Ok(eventtype) => Annotated(Some(eventtype), meta),
115 Err(_) => {
116 meta.add_error(ErrorKind::InvalidData);
117 meta.set_original_value(Some(value));
118 Annotated(None, meta)
119 }
120 },
121 Annotated(None, meta) => Annotated(None, meta),
122 }
123 }
124}
125
126impl IntoValue for EventType {
127 fn into_value(self) -> Value
128 where
129 Self: Sized,
130 {
131 Value::String(self.to_string())
132 }
133
134 fn serialize_payload<S>(&self, s: S, _behavior: SkipSerialization) -> Result<S::Ok, S::Error>
135 where
136 Self: Sized,
137 S: serde::Serializer,
138 {
139 Serialize::serialize(self.as_str(), s)
140 }
141}