relay_event_schema/protocol/contexts/
attachments.rs1use relay_protocol::{Annotated, Array, Empty, FromValue, IntoValue, Object, Value};
2
3use crate::processor::ProcessValue;
4
5#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
7pub struct AttachmentsContext {
8 pub rate_limited: Annotated<Array<RateLimitedAttachment>>,
10
11 #[metastructure(additional_properties, retain = true)]
13 pub other: Object<Value>,
14}
15
16#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
18pub struct RateLimitedAttachment {
19 #[metastructure(field = "type")]
21 pub ty: Annotated<String>,
22
23 pub reason_code: Annotated<String>,
25}
26
27impl super::DefaultContext for AttachmentsContext {
28 fn default_key() -> &'static str {
29 "attachments"
30 }
31
32 fn from_context(context: super::Context) -> Option<Self> {
33 match context {
34 super::Context::Attachments(c) => Some(*c),
35 _ => None,
36 }
37 }
38
39 fn cast(context: &super::Context) -> Option<&Self> {
40 match context {
41 super::Context::Attachments(c) => Some(c),
42 _ => None,
43 }
44 }
45
46 fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
47 match context {
48 super::Context::Attachments(c) => Some(c),
49 _ => None,
50 }
51 }
52
53 fn into_context(self) -> super::Context {
54 super::Context::Attachments(Box::new(self))
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use crate::protocol::Context;
62
63 #[test]
64 fn test_attachments_context_roundtrip() {
65 let json = r#"{
66 "rate_limited": [
67 {
68 "type": "event.minidump",
69 "reason_code": "attachment_usage_exceeded"
70 }
71 ],
72 "type": "attachments"
73}"#;
74 let context = Annotated::new(Context::Attachments(Box::new(AttachmentsContext {
75 rate_limited: Annotated::new(vec![Annotated::new(RateLimitedAttachment {
76 ty: Annotated::new("event.minidump".to_owned()),
77 reason_code: Annotated::new("attachment_usage_exceeded".to_owned()),
78 })]),
79 other: Object::new(),
80 })));
81
82 assert_eq!(context, Annotated::from_json(json).unwrap());
83 assert_eq!(json, context.to_json_pretty().unwrap());
84 }
85}