diff --git a/server/src/rbx.rs b/server/src/rbx.rs index 74c84693..49822ea7 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -5,7 +5,7 @@ use id::Id; // TODO: Switch to enum to represent more value types pub type RbxValue = String; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct RbxInstance { /// Maps to the `Name` property on Instance. diff --git a/server/src/web.rs b/server/src/web.rs index f188e831..6ec6c572 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::HashMap; use std::sync::{mpsc, RwLock, Arc}; @@ -41,28 +42,28 @@ pub struct ServerInfoResponse<'a> { pub partitions: HashMap>, } -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -struct ReadAllResponse<'a> { - server_id: &'a str, - message_cursor: i32, - instances: &'a HashMap, +pub struct ReadAllResponse<'a> { + pub server_id: &'a str, + pub message_cursor: i32, + pub instances: Cow<'a, HashMap>, } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] -struct ReadResponse<'a> { - server_id: &'a str, - message_cursor: i32, - instances: HashMap, +pub struct ReadResponse<'a> { + pub server_id: &'a str, + pub message_cursor: i32, + pub instances: HashMap, } #[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] -struct SubscribeResponse<'a> { - server_id: &'a str, - message_cursor: i32, - messages: &'a [Message], +pub struct SubscribeResponse<'a> { + pub server_id: &'a str, + pub message_cursor: i32, + pub messages: Cow<'a, [Message]>, } pub struct Server { @@ -114,7 +115,7 @@ impl Server { if cursor > messages.len() as i32 { return Response::json(&SubscribeResponse { server_id: &self.server_id, - messages: &[], + messages: Cow::Borrowed(&[]), message_cursor: messages.len() as i32 - 1, }); } @@ -125,7 +126,7 @@ impl Server { return Response::json(&SubscribeResponse { server_id: &self.server_id, - messages: new_messages, + messages: Cow::Borrowed(new_messages), message_cursor: new_cursor, }); } @@ -149,7 +150,7 @@ impl Server { Response::json(&SubscribeResponse { server_id: &self.server_id, - messages: new_messages, + messages: Cow::Borrowed(new_messages), message_cursor: new_cursor, }) } @@ -163,7 +164,7 @@ impl Server { Response::json(&ReadAllResponse { server_id: &self.server_id, message_cursor, - instances: rbx_session.tree.get_all_instances(), + instances: Cow::Borrowed(rbx_session.tree.get_all_instances()), }) }, diff --git a/server/tests/web.rs b/server/tests/web.rs index b1e163a3..7d771c4b 100644 --- a/server/tests/web.rs +++ b/server/tests/web.rs @@ -13,7 +13,7 @@ use std::path::PathBuf; use librojo::{ session::Session, project::Project, - web::{Server, WebConfig, ServerInfoResponse}, + web::{Server, WebConfig, ServerInfoResponse, ReadAllResponse}, }; #[test] @@ -39,4 +39,13 @@ fn empty() { assert_eq!(response.protocol_version, 2); assert_eq!(response.partitions, HashMap::new()); } + + { + let body = server.get_string("/api/read_all"); + let response = serde_json::from_str::(&body).unwrap(); + + assert_eq!(response.server_id, "0"); + assert_eq!(response.message_cursor, -1); + assert_eq!(response.instances.as_ref(), &HashMap::new()); + } } \ No newline at end of file