Skip to content

Commit cc7b53c

Browse files
feat: Add custom attachment type
1 parent 34b27b5 commit cc7b53c

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

sentry-types/src/protocol/attachment.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use 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)]
77
pub 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

2831
impl Default for AttachmentType {
@@ -33,13 +36,14 @@ impl Default for AttachmentType {
3336

3437
impl 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,7 @@ 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.ty.clone().unwrap_or_default().as_str(),
7276
ct = self
7377
.content_type
7478
.as_ref()
@@ -92,3 +96,18 @@ impl fmt::Debug for Attachment {
9296
.finish()
9397
}
9498
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
use serde_json;
104+
105+
#[test]
106+
fn test_attachment_type_deserialize() {
107+
let result: AttachmentType = serde_json::from_str(r#""event.minidump""#).unwrap();
108+
assert_eq!(result, AttachmentType::Minidump);
109+
110+
let result: AttachmentType = serde_json::from_str(r#""my.custom.type""#).unwrap();
111+
assert_eq!(result, AttachmentType::Custom("my.custom.type".to_string()));
112+
}
113+
}

0 commit comments

Comments
 (0)