Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 26 additions & 3 deletions sentry-types/src/protocol/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Copy link

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing Copy trait from the enum will impact performance for small operations. Consider keeping Copy and use Cow<'static, str> for the Custom variant to maintain zero-cost abstractions for predefined types while supporting custom strings.

Copilot uses AI. Check for mistakes.

pub enum AttachmentType {
#[serde(rename = "event.attachment")]
/// (default) A standard attachment without special meaning.
Expand All @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()));
}
}