relay_event_schema/protocol/contexts/
profile.rs

1use relay_protocol::{Annotated, Empty, FromValue, IntoValue};
2
3use crate::processor::ProcessValue;
4use crate::protocol::EventId;
5
6/// Profile context
7#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
8pub struct ProfileContext {
9    /// The profile ID.
10    pub profile_id: Annotated<EventId>,
11
12    /// The profiler ID.
13    pub profiler_id: Annotated<EventId>,
14}
15
16impl super::DefaultContext for ProfileContext {
17    fn default_key() -> &'static str {
18        "profile"
19    }
20
21    fn from_context(context: super::Context) -> Option<Self> {
22        match context {
23            super::Context::Profile(c) => Some(*c),
24            _ => None,
25        }
26    }
27
28    fn cast(context: &super::Context) -> Option<&Self> {
29        match context {
30            super::Context::Profile(c) => Some(c),
31            _ => None,
32        }
33    }
34
35    fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
36        match context {
37            super::Context::Profile(c) => Some(c),
38            _ => None,
39        }
40    }
41
42    fn into_context(self) -> super::Context {
43        super::Context::Profile(Box::new(self))
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::protocol::Context;
51
52    #[test]
53    fn roundtrip() {
54        let json = r#"{
55  "profile_id": "4c79f60c11214eb38604f4ae0781bfb2",
56  "type": "profile"
57}"#;
58        let context = Annotated::new(Context::Profile(Box::new(ProfileContext {
59            profile_id: Annotated::new(EventId(
60                "4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
61            )),
62            ..ProfileContext::default()
63        })));
64
65        assert_eq!(context, Annotated::from_json(json).unwrap());
66        assert_eq!(json, context.to_json_pretty().unwrap());
67    }
68
69    #[test]
70    fn normalization() {
71        let json = r#"{
72  "profile_id": "4C79F60C11214EB38604F4AE0781BFB2",
73  "type": "profile"
74}"#;
75        let context = Annotated::new(Context::Profile(Box::new(ProfileContext {
76            profile_id: Annotated::new(EventId(
77                "4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
78            )),
79            ..ProfileContext::default()
80        })));
81
82        assert_eq!(context, Annotated::from_json(json).unwrap());
83    }
84
85    #[test]
86    fn context_with_profiler_id() {
87        let json = r#"{"profiler_id": "4C79F60C11214EB38604F4AE0781BFB2", "type": "profile"}"#;
88        let context = Annotated::new(Context::Profile(Box::new(ProfileContext {
89            profiler_id: Annotated::new(EventId(
90                "4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap(),
91            )),
92            ..ProfileContext::default()
93        })));
94
95        assert_eq!(context, Annotated::from_json(json).unwrap());
96    }
97}