Wrangle lifetimes and implement empty read_all test

This commit is contained in:
Lucien Greathouse 2018-06-05 23:36:50 -07:00
parent 27d5bd3d6f
commit c85e5c8a78
3 changed files with 29 additions and 19 deletions

View file

@ -5,7 +5,7 @@ use id::Id;
// TODO: Switch to enum to represent more value types // TODO: Switch to enum to represent more value types
pub type RbxValue = String; pub type RbxValue = String;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct RbxInstance { pub struct RbxInstance {
/// Maps to the `Name` property on Instance. /// Maps to the `Name` property on Instance.

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{mpsc, RwLock, Arc}; use std::sync::{mpsc, RwLock, Arc};
@ -41,28 +42,28 @@ pub struct ServerInfoResponse<'a> {
pub partitions: HashMap<String, Vec<String>>, pub partitions: HashMap<String, Vec<String>>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ReadAllResponse<'a> { pub struct ReadAllResponse<'a> {
server_id: &'a str, pub server_id: &'a str,
message_cursor: i32, pub message_cursor: i32,
instances: &'a HashMap<Id, RbxInstance>, pub instances: Cow<'a, HashMap<Id, RbxInstance>>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ReadResponse<'a> { pub struct ReadResponse<'a> {
server_id: &'a str, pub server_id: &'a str,
message_cursor: i32, pub message_cursor: i32,
instances: HashMap<Id, &'a RbxInstance>, pub instances: HashMap<Id, &'a RbxInstance>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct SubscribeResponse<'a> { pub struct SubscribeResponse<'a> {
server_id: &'a str, pub server_id: &'a str,
message_cursor: i32, pub message_cursor: i32,
messages: &'a [Message], pub messages: Cow<'a, [Message]>,
} }
pub struct Server { pub struct Server {
@ -114,7 +115,7 @@ impl Server {
if cursor > messages.len() as i32 { if cursor > messages.len() as i32 {
return Response::json(&SubscribeResponse { return Response::json(&SubscribeResponse {
server_id: &self.server_id, server_id: &self.server_id,
messages: &[], messages: Cow::Borrowed(&[]),
message_cursor: messages.len() as i32 - 1, message_cursor: messages.len() as i32 - 1,
}); });
} }
@ -125,7 +126,7 @@ impl Server {
return Response::json(&SubscribeResponse { return Response::json(&SubscribeResponse {
server_id: &self.server_id, server_id: &self.server_id,
messages: new_messages, messages: Cow::Borrowed(new_messages),
message_cursor: new_cursor, message_cursor: new_cursor,
}); });
} }
@ -149,7 +150,7 @@ impl Server {
Response::json(&SubscribeResponse { Response::json(&SubscribeResponse {
server_id: &self.server_id, server_id: &self.server_id,
messages: new_messages, messages: Cow::Borrowed(new_messages),
message_cursor: new_cursor, message_cursor: new_cursor,
}) })
} }
@ -163,7 +164,7 @@ impl Server {
Response::json(&ReadAllResponse { Response::json(&ReadAllResponse {
server_id: &self.server_id, server_id: &self.server_id,
message_cursor, message_cursor,
instances: rbx_session.tree.get_all_instances(), instances: Cow::Borrowed(rbx_session.tree.get_all_instances()),
}) })
}, },

View file

@ -13,7 +13,7 @@ use std::path::PathBuf;
use librojo::{ use librojo::{
session::Session, session::Session,
project::Project, project::Project,
web::{Server, WebConfig, ServerInfoResponse}, web::{Server, WebConfig, ServerInfoResponse, ReadAllResponse},
}; };
#[test] #[test]
@ -39,4 +39,13 @@ fn empty() {
assert_eq!(response.protocol_version, 2); assert_eq!(response.protocol_version, 2);
assert_eq!(response.partitions, HashMap::new()); assert_eq!(response.partitions, HashMap::new());
} }
{
let body = server.get_string("/api/read_all");
let response = serde_json::from_str::<ReadAllResponse>(&body).unwrap();
assert_eq!(response.server_id, "0");
assert_eq!(response.message_cursor, -1);
assert_eq!(response.instances.as_ref(), &HashMap::new());
}
} }