diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e3063a0..e8323e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Breaking changes + +- Add custom variant to `AttachmentType` that holds an arbitrary String. ([#916](https://github.com/getsentry/sentry-rust/pull/916)) + ## 0.44.0 ### Breaking changes diff --git a/sentry-types/src/protocol/attachment.rs b/sentry-types/src/protocol/attachment.rs index 8b2ce6d7..e23a6478 100644 --- a/sentry-types/src/protocol/attachment.rs +++ b/sentry-types/src/protocol/attachment.rs @@ -3,7 +3,7 @@ use std::fmt; use serde::Deserialize; /// The different types an attachment can have. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)] +#[derive(Debug, Clone, Eq, PartialEq, Deserialize)] pub enum AttachmentType { #[serde(rename = "event.attachment")] /// (default) A standard attachment without special meaning. @@ -23,6 +23,9 @@ pub enum AttachmentType { /// the last logs are extracted into event breadcrumbs. #[serde(rename = "unreal.logs")] UnrealLogs, + /// A custom attachment type with an arbitrary string value. + #[serde(untagged)] + Custom(String), } impl Default for AttachmentType { @@ -33,13 +36,14 @@ impl Default for AttachmentType { impl AttachmentType { /// Gets the string value Sentry expects for the attachment type. - pub fn as_str(self) -> &'static str { + pub fn as_str(&self) -> &str { match self { Self::Attachment => "event.attachment", Self::Minidump => "event.minidump", Self::AppleCrashReport => "event.applecrashreport", Self::UnrealContext => "unreal.context", Self::UnrealLogs => "unreal.logs", + Self::Custom(s) => s, } } } @@ -68,7 +72,11 @@ impl Attachment { r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}","content_type":"{ct}"}}"#, filename = self.filename, length = self.buffer.len(), - at = self.ty.unwrap_or_default().as_str(), + at = self + .ty + .as_ref() + .unwrap_or(&AttachmentType::default()) + .as_str(), ct = self .content_type .as_ref() @@ -92,3 +100,18 @@ impl fmt::Debug for Attachment { .finish() } } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json; + + #[test] + fn test_attachment_type_deserialize() { + let result: AttachmentType = serde_json::from_str(r#""event.minidump""#).unwrap(); + assert_eq!(result, AttachmentType::Minidump); + + let result: AttachmentType = serde_json::from_str(r#""my.custom.type""#).unwrap(); + assert_eq!(result, AttachmentType::Custom("my.custom.type".to_string())); + } +}