@@ -3,7 +3,7 @@ use std::fmt;
33use serde:: Deserialize ;
44
55/// The different types an attachment can have.
6- #[ derive( Debug , Copy , Clone , Eq , PartialEq , Deserialize ) ]
6+ #[ derive( Debug , Clone , Eq , PartialEq , Deserialize ) ]
77pub enum AttachmentType {
88 #[ serde( rename = "event.attachment" ) ]
99 /// (default) A standard attachment without special meaning.
@@ -23,6 +23,9 @@ pub enum AttachmentType {
2323 /// the last logs are extracted into event breadcrumbs.
2424 #[ serde( rename = "unreal.logs" ) ]
2525 UnrealLogs ,
26+ /// A custom attachment type with an arbitrary string value.
27+ #[ serde( untagged) ]
28+ Custom ( String ) ,
2629}
2730
2831impl Default for AttachmentType {
@@ -33,13 +36,14 @@ impl Default for AttachmentType {
3336
3437impl AttachmentType {
3538 /// Gets the string value Sentry expects for the attachment type.
36- pub fn as_str ( self ) -> & ' static str {
39+ pub fn as_str ( & self ) -> & str {
3740 match self {
3841 Self :: Attachment => "event.attachment" ,
3942 Self :: Minidump => "event.minidump" ,
4043 Self :: AppleCrashReport => "event.applecrashreport" ,
4144 Self :: UnrealContext => "unreal.context" ,
4245 Self :: UnrealLogs => "unreal.logs" ,
46+ Self :: Custom ( s) => s,
4347 }
4448 }
4549}
@@ -68,7 +72,11 @@ impl Attachment {
6872 r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}","content_type":"{ct}"}}"# ,
6973 filename = self . filename,
7074 length = self . buffer. len( ) ,
71- at = self . ty. unwrap_or_default( ) . as_str( ) ,
75+ at = self
76+ . ty
77+ . as_ref( )
78+ . unwrap_or( & AttachmentType :: default ( ) )
79+ . as_str( ) ,
7280 ct = self
7381 . content_type
7482 . as_ref( )
@@ -92,3 +100,18 @@ impl fmt::Debug for Attachment {
92100 . finish ( )
93101 }
94102}
103+
104+ #[ cfg( test) ]
105+ mod tests {
106+ use super :: * ;
107+ use serde_json;
108+
109+ #[ test]
110+ fn test_attachment_type_deserialize ( ) {
111+ let result: AttachmentType = serde_json:: from_str ( r#""event.minidump""# ) . unwrap ( ) ;
112+ assert_eq ! ( result, AttachmentType :: Minidump ) ;
113+
114+ let result: AttachmentType = serde_json:: from_str ( r#""my.custom.type""# ) . unwrap ( ) ;
115+ assert_eq ! ( result, AttachmentType :: Custom ( "my.custom.type" . to_string( ) ) ) ;
116+ }
117+ }
0 commit comments