diff --git a/Cargo.toml b/Cargo.toml index 74ea7e09..920ee81e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ url = "1.6.0" serde = "1.0.27" serde_derive = "1.0.27" serde_json = "1.0.9" +serde_plain = "0.2.0" diff --git a/src/lib.rs b/src/lib.rs index 25ff5bae..4a3f4938 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ extern crate serde; #[macro_use] extern crate serde_derive; extern crate serde_json; +extern crate serde_plain; extern crate url; #[macro_use] @@ -17,7 +18,9 @@ mod macros; mod auth; mod dsn; mod project_id; +mod protocol; pub use auth::*; pub use dsn::*; pub use project_id::*; +pub use protocol::*; diff --git a/src/protocol.rs b/src/protocol.rs new file mode 100644 index 00000000..4219b9ae --- /dev/null +++ b/src/protocol.rs @@ -0,0 +1,148 @@ +use std::collections::HashMap; + +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, +} + +/// Represents a frame. +#[derive(Serialize, Deserialize, Default, Clone, Debug)] +pub struct Frame { + pub filename: String, + pub abs_path: Option, + pub function: String, + pub lineno: Option, + pub context_line: Option, + pub pre_context: Option>, + pub post_context: Option>, +} + +/// Represents a stacktrace. +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct Stacktrace { + pub frames: Vec, +} + +/// Represents a list of exceptions. +#[derive(Serialize, Deserialize, Default, Clone, Debug)] +pub struct Exception { + pub values: Vec, +} + +/// Represents a single exception +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct SingleException { + #[serde(rename="type")] + pub ty: String, + pub value: String, + pub stacktrace: Option, +} + +/// Represents a single breadcrumb +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +pub struct Breadcrumb { + pub timestamp: f64, + #[serde(rename="type")] + pub ty: String, + pub message: String, + pub category: String, +} + +/// Represents a full event for Sentry. +#[derive(Serialize, Deserialize, Debug, Default, Clone)] +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")] + 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")] + pub contexts: HashMap>, + #[serde(skip_serializing_if="Vec::is_empty")] + pub breadcrumbs: Vec, + pub exception: Option, +} + +/// Holds a single contextual item. +#[derive(Debug, Default, Clone)] +pub struct Context { + data: ContextData, + rest: HashMap, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] +#[serde(rename_all="lowercase")] +pub enum Orientation { + Portrait, + Landscape, +} + +#[derive(Debug, Clone)] +pub enum ContextData { + Default, + Device { + name: Option, + family: Option, + model: Option, + model_id: Option, + arch: Option, + battery_level: Option, + orientation: Option, + }, + Os { + name: Option, + version: Option, + build: Option, + kernel_version: Option, + rooted: Option, + }, + Runtime { + name: Option, + version: Option, + } +} + +impl Default for ContextData { + fn default() -> ContextData { + ContextData::Default + } +} + +impl ContextData { + pub fn get_type(&self) -> &str { + match *self { + ContextData::Default => "default", + ContextData::Device { .. } => "device", + ContextData::Os { .. } => "os", + ContextData::Runtime { .. } => "runtime", + } + } +} + +impl From for Context { + fn from(data: ContextData) -> Context { + Context { + data: data, + rest: HashMap::new(), + } + } +}