Skip to content

Commit e1131d6

Browse files
aw-models: Add schemars support
1 parent 5bdd6f3 commit e1131d6

File tree

11 files changed

+92
-23
lines changed

11 files changed

+92
-23
lines changed

Diff for: Cargo.lock

+36-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: aw-models/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ path = "src/lib.rs"
1515
[dependencies]
1616
chrono = { version = "0.4", features = ["serde"] }
1717
log = "0.4"
18-
serde = "1.0"
18+
serde = { version = "1.0", features = ["derive"] }
1919
serde_json = "1.0"
20-
serde_derive = "1.0"
20+
schemars = { version = "0.7", features = ["chrono"] }

Diff for: aw-models/src/bucket.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use chrono::DateTime;
22
use chrono::Utc;
3+
use schemars::JsonSchema;
4+
use serde::{Deserialize, Serialize};
35
use serde_json::map::Map;
46
use serde_json::value::Value;
57
use std::collections::HashMap;
68

79
use crate::Event;
810

9-
#[derive(Serialize, Deserialize, Clone, Debug)]
11+
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
1012
pub struct Bucket {
1113
#[serde(skip)]
1214
pub bid: Option<i64>,
@@ -24,7 +26,7 @@ pub struct Bucket {
2426
pub last_updated: Option<DateTime<Utc>>, // TODO: Should probably be moved into metadata field
2527
}
2628

27-
#[derive(Serialize, Deserialize, Clone, Debug)]
29+
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
2830
pub struct BucketMetadata {
2931
#[serde(default)]
3032
pub start: Option<DateTime<Utc>>,
@@ -40,7 +42,7 @@ impl Default for BucketMetadata {
4042
}
4143
}
4244

43-
#[derive(Clone, Serialize, Deserialize)]
45+
#[derive(Serialize, Deserialize, JsonSchema, Clone)]
4446
pub struct BucketsExport {
4547
pub buckets: HashMap<String, Bucket>,
4648
}

Diff for: aw-models/src/duration.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use serde::{Deserialize, Serialize};
2+
13
// Max duration of a i64 nanosecond is 2562047.7880152157 hours
24
// ((2**64)/2)/1000000000/60/60
35

Diff for: aw-models/src/event.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
use chrono::DateTime;
22
use chrono::Duration;
33
use chrono::Utc;
4+
use schemars::JsonSchema;
5+
use serde::{Deserialize, Serialize};
46
use serde_json::Map;
57
use serde_json::Value;
68

79
use crate::duration::DurationSerialization;
810

9-
#[derive(Serialize, Deserialize, Clone, Debug)]
11+
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)]
1012
pub struct Event {
13+
/// An unique id for this event.
14+
/// Will be assigned once the event has reached the servers datastore.
15+
///
16+
/// **WARNING:** If you set the ID and insert the event to the server it will replace the previous
17+
/// event with that ID. Only do this if you are completely sure what you are doing.
1118
pub id: Option<i64>,
19+
/// An rfc3339 timestamp which represents the start of the event
1220
pub timestamp: DateTime<Utc>,
21+
/// Duration of the event as a floating point number in seconds.
22+
/// Appended to the timestamp it can represent the end of the event
23+
/// Maximum precision is nanoseconds.
1324
#[serde(with = "DurationSerialization", default = "default_duration")]
25+
#[schemars(with = "f64")]
1426
pub duration: Duration,
27+
/// Can contain any arbitrary JSON data that represents the value of the event.
28+
/// All events in a bucket should follow the format of it's respective bucket-type.
1529
pub data: Map<String, Value>,
1630
}
1731

@@ -47,6 +61,8 @@ fn default_duration() -> Duration {
4761

4862
#[test]
4963
fn test_event() {
64+
use serde_json::json;
65+
5066
let e = Event {
5167
id: None,
5268
timestamp: Utc::now(),

Diff for: aw-models/src/info.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use schemars::JsonSchema;
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Serialize, Deserialize, JsonSchema)]
5+
pub struct Info {
6+
pub hostname: String,
7+
pub version: String,
8+
pub testing: bool,
9+
pub device_id: String,
10+
}

Diff for: aw-models/src/key_value.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use chrono::{DateTime, Utc};
2+
use schemars::JsonSchema;
3+
use serde::{Deserialize, Serialize};
24

3-
#[derive(Serialize, Deserialize)]
5+
#[derive(Serialize, Deserialize, JsonSchema)]
46
pub struct Key {
57
pub key: String,
68
}
79

8-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
10+
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq)]
911
pub struct KeyValue {
1012
pub key: String,
1113
pub value: String,

Diff for: aw-models/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
extern crate serde;
2-
#[cfg_attr(test, macro_use)] // Only macro use for tests
3-
extern crate serde_json;
4-
#[macro_use]
5-
extern crate serde_derive;
6-
extern crate chrono;
71
#[macro_use]
82
extern crate log;
93

@@ -12,7 +6,7 @@ extern crate log;
126
#[macro_use]
137
macro_rules! json_map {
148
{ $( $key:literal : $value:expr),* } => {{
15-
use serde_json::Value;
9+
use serde_json::{Value};
1610
use serde_json::map::Map;
1711
#[allow(unused_mut)]
1812
let mut map : Map<String, Value> = Map::new();
@@ -26,6 +20,7 @@ macro_rules! json_map {
2620
mod bucket;
2721
mod duration;
2822
mod event;
23+
mod info;
2924
mod key_value;
3025
mod query;
3126
mod timeinterval;
@@ -34,6 +29,7 @@ pub use self::bucket::Bucket;
3429
pub use self::bucket::BucketMetadata;
3530
pub use self::bucket::BucketsExport;
3631
pub use self::event::Event;
32+
pub use self::info::Info;
3733
pub use self::key_value::Key;
3834
pub use self::key_value::KeyValue;
3935
pub use self::query::Query;

Diff for: aw-models/src/query.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use serde::Deserialize;
2+
13
use crate::TimeInterval;
24

5+
// TODO Implement serialize once TimeInterval has implemented it
36
#[derive(Deserialize, Clone, Debug)]
47
pub struct Query {
58
//#[serde(with = "DurationSerialization")]

Diff for: aw-models/src/timeinterval.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use chrono::DateTime;
66
use chrono::Duration;
77
use chrono::Utc;
88

9+
// TODO: Implement serialize
10+
911
#[derive(Clone, Debug)]
1012
pub struct TimeInterval {
1113
start: DateTime<Utc>,

Diff for: aw-server/src/endpoints/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use std::sync::Mutex;
44
use gethostname::gethostname;
55
use rocket::response::NamedFile;
66
use rocket::State;
7-
use rocket_contrib::json::JsonValue;
7+
use rocket_contrib::json::Json;
88

99
use crate::config::AWConfig;
1010

1111
use aw_datastore::Datastore;
12+
use aw_models::Info;
1213

1314
pub struct ServerState {
1415
pub datastore: Mutex<Datastore>,
@@ -58,16 +59,16 @@ fn root_favicon(state: State<ServerState>) -> Option<NamedFile> {
5859
}
5960

6061
#[get("/")]
61-
fn server_info(config: State<AWConfig>, state: State<ServerState>) -> JsonValue {
62+
fn server_info(config: State<AWConfig>, state: State<ServerState>) -> Json<Info> {
6263
#[allow(clippy::or_fun_call)]
6364
let hostname = gethostname().into_string().unwrap_or("unknown".to_string());
6465
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
6566

66-
json!({
67-
"hostname": hostname,
68-
"version": format!("v{} (rust)", VERSION.unwrap_or("(unknown)")),
69-
"testing": config.testing,
70-
"device_id": state.device_id,
67+
Json(Info {
68+
hostname,
69+
version: format!("v{} (rust)", VERSION.unwrap_or("(unknown)")),
70+
testing: config.testing,
71+
device_id: state.device_id.clone(),
7172
})
7273
}
7374

0 commit comments

Comments
 (0)