Skip to content

Commit 18a77e2

Browse files
committed
feat: added initial protocol types
1 parent b6f3c1b commit 18a77e2

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ url = "1.6.0"
1010
serde = "1.0.27"
1111
serde_derive = "1.0.27"
1212
serde_json = "1.0.9"
13+
serde_plain = "0.2.0"

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate serde;
99
#[macro_use]
1010
extern crate serde_derive;
1111
extern crate serde_json;
12+
extern crate serde_plain;
1213
extern crate url;
1314

1415
#[macro_use]
@@ -17,7 +18,9 @@ mod macros;
1718
mod auth;
1819
mod dsn;
1920
mod project_id;
21+
mod protocol;
2022

2123
pub use auth::*;
2224
pub use dsn::*;
2325
pub use project_id::*;
26+
pub use protocol::*;

src/protocol.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
use std::collections::HashMap;
2+
3+
use serde_json::Value;
4+
5+
/// Represents a message.
6+
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
7+
pub struct Message {
8+
pub message: String,
9+
#[serde(skip_serializing_if="Vec::is_empty")]
10+
pub params: Vec<String>,
11+
}
12+
13+
/// Represents a frame.
14+
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
15+
pub struct Frame {
16+
pub filename: String,
17+
pub abs_path: Option<String>,
18+
pub function: String,
19+
pub lineno: Option<u32>,
20+
pub context_line: Option<String>,
21+
pub pre_context: Option<Vec<String>>,
22+
pub post_context: Option<Vec<String>>,
23+
}
24+
25+
/// Represents a stacktrace.
26+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
27+
pub struct Stacktrace {
28+
pub frames: Vec<Frame>,
29+
}
30+
31+
/// Represents a list of exceptions.
32+
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
33+
pub struct Exception {
34+
pub values: Vec<SingleException>,
35+
}
36+
37+
/// Represents a single exception
38+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
39+
pub struct SingleException {
40+
#[serde(rename="type")]
41+
pub ty: String,
42+
pub value: String,
43+
pub stacktrace: Option<Stacktrace>,
44+
}
45+
46+
/// Represents a single breadcrumb
47+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
48+
pub struct Breadcrumb {
49+
pub timestamp: f64,
50+
#[serde(rename="type")]
51+
pub ty: String,
52+
pub message: String,
53+
pub category: String,
54+
}
55+
56+
/// Represents a full event for Sentry.
57+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
58+
pub struct Event {
59+
pub tags: HashMap<String, String>,
60+
pub extra: HashMap<String, Value>,
61+
pub level: String,
62+
#[serde(skip_serializing_if="Option::is_none")]
63+
pub fingerprint: Option<Vec<String>>,
64+
#[serde(skip_serializing_if="Option::is_none", rename="sentry.interfaces.Message")]
65+
pub message: Option<Message>,
66+
pub platform: String,
67+
pub timestamp: f64,
68+
#[serde(skip_serializing_if="Option::is_none")]
69+
pub server_name: Option<String>,
70+
#[serde(skip_serializing_if="Option::is_none")]
71+
pub release: Option<String>,
72+
#[serde(skip_serializing_if="Option::is_none")]
73+
pub dist: Option<String>,
74+
#[serde(skip_serializing_if="Option::is_none")]
75+
pub environment: Option<String>,
76+
#[serde(skip_serializing_if="HashMap::is_empty")]
77+
pub user: HashMap<String, String>,
78+
#[serde(skip_serializing_if="HashMap::is_empty")]
79+
pub contexts: HashMap<String, HashMap<String, String>>,
80+
#[serde(skip_serializing_if="Vec::is_empty")]
81+
pub breadcrumbs: Vec<Breadcrumb>,
82+
pub exception: Option<Exception>,
83+
}
84+
85+
/// Holds a single contextual item.
86+
#[derive(Debug, Default, Clone)]
87+
pub struct Context {
88+
data: ContextData,
89+
rest: HashMap<String, Value>,
90+
}
91+
92+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
93+
#[serde(rename_all="lowercase")]
94+
pub enum Orientation {
95+
Portrait,
96+
Landscape,
97+
}
98+
99+
#[derive(Debug, Clone)]
100+
pub enum ContextData {
101+
Default,
102+
Device {
103+
name: Option<String>,
104+
family: Option<String>,
105+
model: Option<String>,
106+
model_id: Option<String>,
107+
arch: Option<String>,
108+
battery_level: Option<f32>,
109+
orientation: Option<Orientation>,
110+
},
111+
Os {
112+
name: Option<String>,
113+
version: Option<String>,
114+
build: Option<String>,
115+
kernel_version: Option<String>,
116+
rooted: Option<bool>,
117+
},
118+
Runtime {
119+
name: Option<String>,
120+
version: Option<String>,
121+
}
122+
}
123+
124+
impl Default for ContextData {
125+
fn default() -> ContextData {
126+
ContextData::Default
127+
}
128+
}
129+
130+
impl ContextData {
131+
pub fn get_type(&self) -> &str {
132+
match *self {
133+
ContextData::Default => "default",
134+
ContextData::Device { .. } => "device",
135+
ContextData::Os { .. } => "os",
136+
ContextData::Runtime { .. } => "runtime",
137+
}
138+
}
139+
}
140+
141+
impl From<ContextData> for Context {
142+
fn from(data: ContextData) -> Context {
143+
Context {
144+
data: data,
145+
rest: HashMap::new(),
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)