relay_event_schema/protocol/contexts/
flags.rs1use crate::processor::ProcessValue;
2use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};
3
4#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
10pub struct FlagsContext {
11 pub values: Annotated<Vec<Annotated<FlagsContextItem>>>,
13 #[metastructure(additional_properties, retain = true, pii = "maybe")]
15 pub other: Object<Value>,
16}
17
18#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
23pub struct FlagsContextItem {
24 #[metastructure(max_chars = 200, allow_chars = "a-zA-Z0-9_.:-")]
26 pub flag: Annotated<String>,
27 #[metastructure(max_chars = 200, deny_chars = "\n")]
30 pub result: Annotated<Value>,
31 #[metastructure(additional_properties, retain = true, pii = "maybe")]
33 pub other: Object<Value>,
34}
35
36impl super::DefaultContext for FlagsContext {
37 fn default_key() -> &'static str {
38 "flags"
39 }
40
41 fn from_context(context: super::Context) -> Option<Self> {
42 match context {
43 super::Context::Flags(c) => Some(*c),
44 _ => None,
45 }
46 }
47
48 fn cast(context: &super::Context) -> Option<&Self> {
49 match context {
50 super::Context::Flags(c) => Some(c),
51 _ => None,
52 }
53 }
54
55 fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
56 match context {
57 super::Context::Flags(c) => Some(c),
58 _ => None,
59 }
60 }
61
62 fn into_context(self) -> super::Context {
63 super::Context::Flags(Box::new(self))
64 }
65}
66
67#[cfg(test)]
68mod test {
69 use super::*;
70 use crate::protocol::Context;
71
72 #[test]
73 fn test_deserializing_flag_context() {
74 let json = r#"{
75 "values": [
76 {
77 "flag": "abc",
78 "result": true
79 },
80 {
81 "flag": "def",
82 "result": false
83 }
84 ],
85 "type": "flags"
86}"#;
87
88 let flags = vec![
89 Annotated::new(FlagsContextItem {
90 flag: Annotated::new("abc".to_owned()),
91 result: Annotated::new(Value::Bool(true)),
92 other: Object::default(),
93 }),
94 Annotated::new(FlagsContextItem {
95 flag: Annotated::new("def".to_owned()),
96 result: Annotated::new(Value::Bool(false)),
97 other: Object::default(),
98 }),
99 ];
100
101 let context = Annotated::new(Context::Flags(Box::new(FlagsContext {
102 values: Annotated::new(flags),
103 other: Object::default(),
104 })));
105
106 assert_eq!(context, Annotated::from_json(json).unwrap());
107 assert_eq!(json, context.to_json_pretty().unwrap());
108 }
109}