From 9bf5cbc7669e838ef7ca13f7ff37818c7937932c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 21 Mar 2018 15:39:36 +0100 Subject: [PATCH] feat: Added basic protocol types in separate module --- Cargo.toml | 16 +++-- src/lib.rs | 4 +- src/protocol/mod.rs | 3 + src/{protocol.rs => protocol/v1.rs} | 90 +++++++++++++++++------------ 4 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 src/protocol/mod.rs rename src/{protocol.rs => protocol/v1.rs} (54%) diff --git a/Cargo.toml b/Cargo.toml index 920ee81e..2829a651 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,14 @@ authors = ["Sentry "] [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" } diff --git a/src/lib.rs b/src/lib.rs index 4a3f4938..296e3776 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ extern crate serde_derive; extern crate serde_json; extern crate serde_plain; extern crate url; +extern crate url_serde; #[macro_use] mod macros; @@ -18,9 +19,8 @@ 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::*; diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs new file mode 100644 index 00000000..4a21b1d7 --- /dev/null +++ b/src/protocol/mod.rs @@ -0,0 +1,3 @@ +//! This module exposes the types for the Sentry protocol in different versions. + +pub mod v1; diff --git a/src/protocol.rs b/src/protocol/v1.rs similarity index 54% rename from src/protocol.rs rename to src/protocol/v1.rs index 4219b9ae..a6504edd 100644 --- a/src/protocol.rs +++ b/src/protocol/v1.rs @@ -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, + #[serde(skip_serializing_if = "Vec::is_empty")] pub params: Vec, } /// Represents a frame. @@ -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, } @@ -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, +} + +/// Represents user info. +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct User { + pub id: Option, + pub email: Option, + pub ip_address: Option, + pub username: Option, + #[serde(flatten)] pub data: HashMap, +} + +/// Represents http request data. +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct Request { + #[serde(with = "url_serde")] pub url: Option, + pub method: Option, + // XXX: this makes absolutely no sense because of unicode + pub data: Option, + pub query_string: Option, + pub cookies: Option, + #[serde(skip_serializing_if = "HashMap::is_empty")] pub headers: HashMap, + #[serde(skip_serializing_if = "HashMap::is_empty")] pub env: HashMap, + #[serde(flatten)] pub other: HashMap, } /// Represents a full event for Sentry. @@ -59,38 +85,35 @@ pub struct Event { pub tags: HashMap, pub extra: HashMap, pub level: String, - #[serde(skip_serializing_if="Option::is_none")] - pub fingerprint: Option>, - #[serde(skip_serializing_if="Option::is_none", rename="sentry.interfaces.Message")] + #[serde(skip_serializing_if = "Option::is_none")] pub fingerprint: Option>, + #[serde(skip_serializing_if = "Option::is_none", rename = "sentry.interfaces.Message")] pub message: Option, pub platform: String, pub timestamp: f64, - #[serde(skip_serializing_if="Option::is_none")] - pub server_name: Option, - #[serde(skip_serializing_if="Option::is_none")] - pub release: Option, - #[serde(skip_serializing_if="Option::is_none")] - pub dist: Option, - #[serde(skip_serializing_if="Option::is_none")] - pub environment: Option, - #[serde(skip_serializing_if="HashMap::is_empty")] - pub user: HashMap, - #[serde(skip_serializing_if="HashMap::is_empty")] + #[serde(skip_serializing_if = "Option::is_none")] pub server_name: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub release: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub dist: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub environment: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub user: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub request: Option, + #[serde(skip_serializing_if = "HashMap::is_empty")] pub contexts: HashMap>, - #[serde(skip_serializing_if="Vec::is_empty")] - pub breadcrumbs: Vec, + #[serde(skip_serializing_if = "Vec::is_empty")] pub breadcrumbs: Vec, pub exception: Option, + #[serde(flatten)] pub other: HashMap, } /// Holds a single contextual item. -#[derive(Debug, Default, Clone)] -pub struct Context { - data: ContextData, - rest: HashMap, +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct RawContext { + #[serde(rename="type")] + ty: Option, + #[serde(flatten)] + data: HashMap, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] -#[serde(rename_all="lowercase")] +#[serde(rename_all = "lowercase")] pub enum Orientation { Portrait, Landscape, @@ -105,7 +128,7 @@ pub enum ContextData { model: Option, model_id: Option, arch: Option, - battery_level: Option, + battery_level: Option, orientation: Option, }, Os { @@ -118,7 +141,7 @@ pub enum ContextData { Runtime { name: Option, version: Option, - } + }, } impl Default for ContextData { @@ -137,12 +160,3 @@ impl ContextData { } } } - -impl From for Context { - fn from(data: ContextData) -> Context { - Context { - data: data, - rest: HashMap::new(), - } - } -}