Skip to content

Commit b99eb46

Browse files
feat(types): Add custom variant to AttachmentType (#916)
Co-authored-by: Arpad Borsos <[email protected]>
1 parent 34b27b5 commit b99eb46

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Breaking changes
6+
7+
- Add custom variant to `AttachmentType` that holds an arbitrary String. ([#916](https://github.com/getsentry/sentry-rust/pull/916))
8+
39
## 0.44.0
410

511
### Breaking changes

sentry-types/src/protocol/attachment.rs

Lines changed: 26 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,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

Comments
 (0)