Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jun 17, 2022
1 parent ed013ae commit a598c37
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
@@ -1,10 +1,14 @@
# Changelog

## 0.27.0
## Unreleased

**Breaking Changes**:

- The minium supported Rust version was bumped to **1.57.0** due to requirements from dependencies.
-
**Features**:

- feat: Implement `Envelope::from_path` and `Envelope::from_slice`. ([#456](https://github.com/getsentry/sentry-rust/pull/456))

## 0.26.0

Expand Down
24 changes: 23 additions & 1 deletion sentry-types/src/protocol/attachment.rs
@@ -1,6 +1,13 @@
use std::fmt;
use std::str;

use serde::Deserialize;
use thiserror::Error;

/// An error used when parsing `AttachmentType`.
#[derive(Debug, Error)]
#[error("invalid attachment type")]
pub struct AttachmentTypeError;

/// The different types an attachment can have.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deserialize)]
Expand All @@ -26,6 +33,21 @@ impl Default for AttachmentType {
}
}

impl str::FromStr for AttachmentType {
type Err = AttachmentTypeError;

fn from_str(string: &str) -> Result<AttachmentType, Self::Err> {
Ok(match string {
"event.attachment" => AttachmentType::Attachment,
"event.minidump" => AttachmentType::Minidump,
"event.applecrashreport" => AttachmentType::AppleCrashReport,
"unreal.context" => AttachmentType::UnrealContext,
"unreal.logs" => AttachmentType::UnrealLogs,
_ => return Err(AttachmentTypeError),
})
}
}

impl AttachmentType {
/// Gets the string value Sentry expects for the attachment type.
pub fn as_str(self) -> &'static str {
Expand All @@ -39,7 +61,7 @@ impl AttachmentType {
}
}

#[derive(Clone, PartialEq, Deserialize, Default)]
#[derive(Clone, PartialEq, Default)]
/// Represents an attachment item.
pub struct Attachment {
/// The actual attachment data.
Expand Down
60 changes: 29 additions & 31 deletions sentry-types/src/protocol/envelope.rs
Expand Up @@ -4,7 +4,10 @@ use serde::Deserialize;
use thiserror::Error;
use uuid::Uuid;

use super::v7::{Attachment, Event, SessionAggregates, SessionUpdate, Transaction};
use super::{
attachment::AttachmentType,
v7::{Attachment, Event, SessionAggregates, SessionUpdate, Transaction},
};

/// Raised if a envelope cannot be parsed from a given input.
#[derive(Debug, Error)]
Expand Down Expand Up @@ -58,25 +61,23 @@ enum EnvelopeItemType {
}

/// An Envelope Item Header.
#[allow(dead_code)]
#[derive(Clone, Debug, Deserialize)]
struct EnvelopeItemHeader {
r#type: EnvelopeItemType,
length: Option<usize>,
// Fields below apply only to Attachment Item type
filename: Option<String>,
attachment_type: Option<String>,
attachment_type: Option<AttachmentType>,
content_type: Option<String>,
}

/// An Envelope Item.
///
/// See the [documentation on Items](https://develop.sentry.dev/sdk/envelopes/#items)
/// for more details.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
#[serde(untagged)]
pub enum EnvelopeItem {
/// An Event Item.
///
Expand Down Expand Up @@ -383,34 +384,36 @@ impl Envelope {
Self::require_termination(slice, payload_end)?;
payload_end
}
None => match slice[payload_start..].iter().position(|b| *b == b'\n') {
Some(relative_end) => payload_start + relative_end,
None => match slice.get(payload_start..) {
Some(range) => match range.iter().position(|&b| b == b'\n') {
Some(relative_end) => payload_start + relative_end,
None => slice.len(),
},
None => slice.len(),
},
};

let payload = slice.get(payload_start..payload_end).unwrap();

let item = match header.r#type {
EnvelopeItemType::Event => EnvelopeItem::Event(
serde_json::from_slice(payload).map_err(EnvelopeError::InvalidItemPayload)?,
),
EnvelopeItemType::Transaction => EnvelopeItem::Transaction(
serde_json::from_slice(payload).map_err(EnvelopeError::InvalidItemPayload)?,
),
EnvelopeItemType::SessionUpdate => EnvelopeItem::SessionUpdate(
serde_json::from_slice(payload).map_err(EnvelopeError::InvalidItemPayload)?,
),
EnvelopeItemType::SessionAggregates => EnvelopeItem::SessionAggregates(
serde_json::from_slice(payload).map_err(EnvelopeError::InvalidItemPayload)?,
),
EnvelopeItemType::Attachment => EnvelopeItem::Attachment(Attachment {
EnvelopeItemType::Event => serde_json::from_slice(payload).map(EnvelopeItem::Event),
EnvelopeItemType::Transaction => {
serde_json::from_slice(payload).map(EnvelopeItem::Transaction)
}
EnvelopeItemType::SessionUpdate => {
serde_json::from_slice(payload).map(EnvelopeItem::SessionUpdate)
}
EnvelopeItemType::SessionAggregates => {
serde_json::from_slice(payload).map(EnvelopeItem::SessionAggregates)
}
EnvelopeItemType::Attachment => Ok(EnvelopeItem::Attachment(Attachment {
buffer: payload.to_owned(),
filename: header.filename.unwrap_or_default(),
content_type: header.content_type,
..Default::default()
}),
};
ty: header.attachment_type,
})),
}
.map_err(EnvelopeError::InvalidItemPayload)?;

Ok((item, payload_end + 1))
}
Expand Down Expand Up @@ -441,19 +444,14 @@ impl From<Transaction<'static>> for Envelope {

#[cfg(test)]
mod test {
use std::{
str::FromStr,
time::{Duration, SystemTime},
};
use std::str::FromStr;
use std::time::{Duration, SystemTime};

use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

use super::*;
use crate::protocol::{
latest::Level,
v7::{SessionAttributes, SessionStatus, Span},
};
use crate::protocol::v7::{Level, SessionAttributes, SessionStatus, Span};

fn to_str(envelope: Envelope) -> String {
let mut vec = Vec::new();
Expand Down

0 comments on commit a598c37

Please sign in to comment.