use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum AttachmentType {
Attachment,
Minidump,
AppleCrashReport,
UnrealContext,
UnrealLogs,
}
impl Default for AttachmentType {
fn default() -> Self {
Self::Attachment
}
}
impl AttachmentType {
pub fn as_str(self) -> &'static str {
match self {
Self::Attachment => "event.attachment",
Self::Minidump => "event.minidump",
Self::AppleCrashReport => "event.applecrashreport",
Self::UnrealContext => "unreal.context",
Self::UnrealLogs => "unreal.logs",
}
}
}
#[derive(Clone, PartialEq)]
pub struct Attachment {
pub buffer: Vec<u8>,
pub filename: String,
pub ty: Option<AttachmentType>,
}
impl Attachment {
pub fn to_writer<W>(&self, writer: &mut W) -> std::io::Result<()>
where
W: std::io::Write,
{
writeln!(
writer,
r#"{{"type":"attachment","length":{length},"filename":"{filename}","attachment_type":"{at}"}}"#,
filename = self.filename,
length = self.buffer.len(),
at = self.ty.unwrap_or_default().as_str()
)?;
writer.write_all(&self.buffer)?;
Ok(())
}
}
impl fmt::Debug for Attachment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Attachment")
.field("buffer", &self.buffer.len())
.field("filename", &self.filename)
.field("type", &self.ty)
.finish()
}
}