Skip to content

Commit

Permalink
feat: Added basic protocol types in separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 21, 2018
1 parent 18a77e2 commit 9bf5cbc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
16 changes: 11 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ authors = ["Sentry <[email protected]>"]
[dependencies]
failure = "0.1.1"
failure_derive = "0.1.1"
url = "1.6.0"
serde = "1.0.27"
serde_derive = "1.0.27"
serde_json = "1.0.9"
serde_plain = "0.2.0"
url = "1.7.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_plain = "0.3.0"
url_serde = "0.2.0"

[patch.crates-io]
serde = { git = "https://github.com/mitsuhiko/serde", branch = "feature/flatten" }
serde_derive = { git = "https://github.com/mitsuhiko/serde", branch = "feature/flatten" }
serde_derive_internals = { git = "https://github.com/mitsuhiko/serde", branch = "feature/flatten" }
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ extern crate serde_derive;
extern crate serde_json;
extern crate serde_plain;
extern crate url;
extern crate url_serde;

#[macro_use]
mod macros;

mod auth;
mod dsn;
mod project_id;
mod protocol;
pub mod protocol;

pub use auth::*;
pub use dsn::*;
pub use project_id::*;
pub use protocol::*;
3 changes: 3 additions & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! This module exposes the types for the Sentry protocol in different versions.
pub mod v1;
90 changes: 52 additions & 38 deletions src/protocol.rs → src/protocol/v1.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::collections::HashMap;
use std::net::IpAddr;

use url_serde;
use url::Url;
use serde_json::Value;

/// Represents a message.
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub struct Message {
pub message: String,
#[serde(skip_serializing_if="Vec::is_empty")]
pub params: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")] pub params: Vec<String>,
}

/// Represents a frame.
Expand Down Expand Up @@ -37,8 +39,7 @@ pub struct Exception {
/// Represents a single exception
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SingleException {
#[serde(rename="type")]
pub ty: String,
#[serde(rename = "type")] pub ty: String,
pub value: String,
pub stacktrace: Option<Stacktrace>,
}
Expand All @@ -47,10 +48,35 @@ pub struct SingleException {
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Breadcrumb {
pub timestamp: f64,
#[serde(rename="type")]
pub ty: String,
#[serde(rename = "type")] pub ty: String,
pub message: String,
pub category: String,
#[serde(flatten)]
pub data: HashMap<String, Value>,
}

/// Represents user info.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct User {
pub id: Option<String>,
pub email: Option<String>,
pub ip_address: Option<IpAddr>,
pub username: Option<String>,
#[serde(flatten)] pub data: HashMap<String, Value>,
}

/// Represents http request data.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Request {
#[serde(with = "url_serde")] pub url: Option<Url>,
pub method: Option<String>,
// XXX: this makes absolutely no sense because of unicode
pub data: Option<String>,
pub query_string: Option<String>,
pub cookies: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")] pub headers: HashMap<String, String>,
#[serde(skip_serializing_if = "HashMap::is_empty")] pub env: HashMap<String, String>,
#[serde(flatten)] pub other: HashMap<String, Value>,
}

/// Represents a full event for Sentry.
Expand All @@ -59,38 +85,35 @@ pub struct Event {
pub tags: HashMap<String, String>,
pub extra: HashMap<String, Value>,
pub level: String,
#[serde(skip_serializing_if="Option::is_none")]
pub fingerprint: Option<Vec<String>>,
#[serde(skip_serializing_if="Option::is_none", rename="sentry.interfaces.Message")]
#[serde(skip_serializing_if = "Option::is_none")] pub fingerprint: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none", rename = "sentry.interfaces.Message")]
pub message: Option<Message>,
pub platform: String,
pub timestamp: f64,
#[serde(skip_serializing_if="Option::is_none")]
pub server_name: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
pub release: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
pub dist: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
pub environment: Option<String>,
#[serde(skip_serializing_if="HashMap::is_empty")]
pub user: HashMap<String, String>,
#[serde(skip_serializing_if="HashMap::is_empty")]
#[serde(skip_serializing_if = "Option::is_none")] pub server_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub release: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub dist: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub environment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] pub user: Option<User>,
#[serde(skip_serializing_if = "Option::is_none")] pub request: Option<Request>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub contexts: HashMap<String, HashMap<String, String>>,
#[serde(skip_serializing_if="Vec::is_empty")]
pub breadcrumbs: Vec<Breadcrumb>,
#[serde(skip_serializing_if = "Vec::is_empty")] pub breadcrumbs: Vec<Breadcrumb>,
pub exception: Option<Exception>,
#[serde(flatten)] pub other: HashMap<String, Value>,
}

/// Holds a single contextual item.
#[derive(Debug, Default, Clone)]
pub struct Context {
data: ContextData,
rest: HashMap<String, Value>,
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct RawContext {
#[serde(rename="type")]
ty: Option<String>,
#[serde(flatten)]
data: HashMap<String, Value>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all="lowercase")]
#[serde(rename_all = "lowercase")]
pub enum Orientation {
Portrait,
Landscape,
Expand All @@ -105,7 +128,7 @@ pub enum ContextData {
model: Option<String>,
model_id: Option<String>,
arch: Option<String>,
battery_level: Option<f32>,
battery_level: Option<f32>,
orientation: Option<Orientation>,
},
Os {
Expand All @@ -118,7 +141,7 @@ pub enum ContextData {
Runtime {
name: Option<String>,
version: Option<String>,
}
},
}

impl Default for ContextData {
Expand All @@ -137,12 +160,3 @@ impl ContextData {
}
}
}

impl From<ContextData> for Context {
fn from(data: ContextData) -> Context {
Context {
data: data,
rest: HashMap::new(),
}
}
}

0 comments on commit 9bf5cbc

Please sign in to comment.