diff --git a/server/src/bin.rs b/server/src/bin.rs index 82c568a7..86232c92 100644 --- a/server/src/bin.rs +++ b/server/src/bin.rs @@ -50,4 +50,4 @@ fn main() { process::exit(1); }, } -} +} \ No newline at end of file diff --git a/server/src/commands/init.rs b/server/src/commands/init.rs index 96cbbcb3..9050e072 100644 --- a/server/src/commands/init.rs +++ b/server/src/commands/init.rs @@ -13,4 +13,4 @@ pub fn init(project_path: &PathBuf) { process::exit(1); }, } -} +} \ No newline at end of file diff --git a/server/src/commands/mod.rs b/server/src/commands/mod.rs index 58023fa3..db228aa1 100644 --- a/server/src/commands/mod.rs +++ b/server/src/commands/mod.rs @@ -2,4 +2,4 @@ mod serve; mod init; pub use self::serve::*; -pub use self::init::*; +pub use self::init::*; \ No newline at end of file diff --git a/server/src/commands/serve.rs b/server/src/commands/serve.rs index 6b867ab7..ea210587 100644 --- a/server/src/commands/serve.rs +++ b/server/src/commands/serve.rs @@ -33,4 +33,4 @@ pub fn serve(fuzzy_project_location: &Path) { // let web_config = WebConfig::from_session(server_id, port, &session); // web::start(web_config); -} +} \ No newline at end of file diff --git a/server/src/id.rs b/server/src/id.rs index a27a9d8c..98144cfa 100644 --- a/server/src/id.rs +++ b/server/src/id.rs @@ -18,4 +18,4 @@ fn it_gives_unique_numbers() { let b = get_id(); assert!(a != b); -} +} \ No newline at end of file diff --git a/server/src/lib.rs b/server/src/lib.rs index f584328b..cbb70e9b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -16,6 +16,6 @@ pub mod message_session; pub mod pathext; pub mod project; pub mod rbx; -// pub mod web; +pub mod web; pub mod web_util; -pub mod roblox_studio; +pub mod roblox_studio; \ No newline at end of file diff --git a/server/src/message_session.rs b/server/src/message_session.rs index 85b2aa32..c2821377 100644 --- a/server/src/message_session.rs +++ b/server/src/message_session.rs @@ -61,4 +61,4 @@ impl MessageSession { pub fn get_message_cursor(&self) -> i32 { self.messages.read().unwrap().len() as i32 - 1 } -} +} \ No newline at end of file diff --git a/server/src/pathext.rs b/server/src/pathext.rs index 0b4c0f79..0b257cc8 100644 --- a/server/src/pathext.rs +++ b/server/src/pathext.rs @@ -24,4 +24,4 @@ where } else { root.join(value) } -} +} \ No newline at end of file diff --git a/server/src/rbx.rs b/server/src/rbx.rs index e3437f3c..fc759123 100644 --- a/server/src/rbx.rs +++ b/server/src/rbx.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::collections::HashMap; use id::Id; @@ -23,18 +22,44 @@ pub struct RbxInstance { /// Contains all other properties of an Instance. pub properties: HashMap, + /// The unique ID of the instance + id: Id, + /// All of the children of this instance. Order is relevant to preserve! - pub children: Vec, + children: Vec, /// The parent of the instance, if there is one. - pub parent: Option, + parent: Option, } -// This seems like a really bad idea? -// Why isn't there a blanket impl for this for all T? -impl<'a> From<&'a RbxInstance> for Cow<'a, RbxInstance> { - fn from(instance: &'a RbxInstance) -> Cow<'a, RbxInstance> { - Cow::Borrowed(instance) +pub struct Descendants<'a> { + tree: &'a RbxTree, + ids_to_visit: Vec, +} + +impl<'a> Iterator for Descendants<'a> { + type Item = &'a RbxInstance; + + fn next(&mut self) -> Option { + loop { + let id = match self.ids_to_visit.pop() { + Some(id) => id, + None => break, + }; + + match self.tree.get_instance(id) { + Some(instance) => { + for child_id in &instance.children { + self.ids_to_visit.push(*child_id); + } + + return Some(instance); + }, + None => continue, + } + } + + None } } @@ -49,20 +74,29 @@ impl RbxTree { } } + pub fn get_instance(&self, id: Id) -> Option<&RbxInstance> { + self.instances.get(&id) + } + pub fn get_all_instances(&self) -> &HashMap { &self.instances } - pub fn insert_instance(&mut self, id: Id, instance: RbxInstance) { + pub fn insert_instance(&mut self, instance: RbxInstance) { if let Some(parent_id) = instance.parent { - if let Some(mut parent) = self.instances.get_mut(&parent_id) { - if !parent.children.contains(&id) { - parent.children.push(id); + match self.instances.get_mut(&parent_id) { + Some(mut parent) => { + if !parent.children.contains(&instance.id) { + parent.children.push(instance.id); + } + }, + None => { + panic!("Tree consistency error, parent {} was not present in tree.", parent_id); } } } - self.instances.insert(id, instance); + self.instances.insert(instance.id, instance); } pub fn delete_instance(&mut self, id: Id) -> Vec { @@ -101,27 +135,10 @@ impl RbxTree { ids_deleted } - pub fn get_instance_and_descendants<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap) - where T: From<&'a RbxInstance> - { - let mut ids_to_visit = vec![id]; - - loop { - let id = match ids_to_visit.pop() { - Some(id) => id, - None => break, - }; - - match self.instances.get(&id) { - Some(instance) => { - output.insert(id, instance.into()); - - for child_id in &instance.children { - ids_to_visit.push(*child_id); - } - }, - None => continue, - } + pub fn iter_descendants<'a>(&'a self, id: Id) -> Descendants<'a> { + Descendants { + tree: self, + ids_to_visit: vec![id], } } -} +} \ No newline at end of file diff --git a/server/src/web.rs b/server/src/web.rs index 2a5fefd4..5a665c4d 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -8,37 +8,25 @@ use id::Id; use message_session::{MessageSession, Message}; use project::Project; use rbx::RbxInstance; -use rbx_session::RbxSession; -use session::Session; +// use rbx_session::RbxSession; +// use session::Session; /// The set of configuration the web server needs to start. pub struct WebConfig { pub port: u64, pub project: Project, pub server_id: u64, - pub rbx_session: Arc>, + // pub rbx_session: Arc>, pub message_session: MessageSession, } -impl WebConfig { - pub fn from_session(server_id: u64, port: u64, session: &Session) -> WebConfig { - WebConfig { - port, - server_id, - project: session.project.clone(), - rbx_session: session.get_rbx_session(), - message_session: session.get_message_session(), - } - } -} - #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ServerInfoResponse<'a> { pub server_id: &'a str, pub server_version: &'a str, pub protocol_version: u64, - pub partitions: HashMap>, + // pub partitions: HashMap>, } #[derive(Debug, Serialize, Deserialize)] @@ -46,8 +34,8 @@ pub struct ServerInfoResponse<'a> { pub struct ReadAllResponse<'a> { pub server_id: &'a str, pub message_cursor: i32, - pub instances: Cow<'a, HashMap>, - pub partition_instances: Cow<'a, HashMap>, + // pub instances: Cow<'a, HashMap>, + // pub partition_instances: Cow<'a, HashMap>, } #[derive(Debug, Serialize, Deserialize)] @@ -55,7 +43,7 @@ pub struct ReadAllResponse<'a> { pub struct ReadResponse<'a> { pub server_id: &'a str, pub message_cursor: i32, - pub instances: HashMap>, + // pub instances: HashMap>, } #[derive(Debug, Serialize, Deserialize)] @@ -90,17 +78,17 @@ impl Server { (GET) (/api/rojo) => { // Get a summary of information about the server. - let mut partitions = HashMap::new(); + // let mut partitions = HashMap::new(); - for partition in self.config.project.partitions.values() { - partitions.insert(partition.name.clone(), partition.target.clone()); - } + // for partition in self.config.project.partitions.values() { + // partitions.insert(partition.name.clone(), partition.target.clone()); + // } Response::json(&ServerInfoResponse { server_version: self.server_version, protocol_version: 2, server_id: &self.server_id, - partitions: partitions, + // partitions: partitions, }) }, @@ -157,15 +145,15 @@ impl Server { }, (GET) (/api/read_all) => { - let rbx_session = self.config.rbx_session.read().unwrap(); + // let rbx_session = self.config.rbx_session.read().unwrap(); let message_cursor = self.config.message_session.get_message_cursor(); Response::json(&ReadAllResponse { server_id: &self.server_id, message_cursor, - instances: Cow::Borrowed(rbx_session.tree.get_all_instances()), - partition_instances: Cow::Borrowed(&rbx_session.partition_instances), + // instances: Cow::Borrowed(rbx_session.tree.get_all_instances()), + // partition_instances: Cow::Borrowed(&rbx_session.partition_instances), }) }, @@ -180,20 +168,20 @@ impl Server { Err(_) => return rouille::Response::text("Malformed ID list").with_status_code(400), }; - let rbx_session = self.config.rbx_session.read().unwrap(); + // let rbx_session = self.config.rbx_session.read().unwrap(); let message_cursor = self.config.message_session.get_message_cursor(); - let mut instances = HashMap::new(); + // let mut instances = HashMap::new(); - for requested_id in &requested_ids { - rbx_session.tree.get_instance_and_descendants(*requested_id, &mut instances); - } + // for requested_id in &requested_ids { + // rbx_session.tree.get_instance_and_descendants(*requested_id, &mut instances); + // } Response::json(&ReadResponse { server_id: &self.server_id, message_cursor, - instances, + // instances, }) }, @@ -209,4 +197,4 @@ pub fn start(config: WebConfig) { let server = Server::new(config); rouille::start_server(address, move |request| server.handle_request(request)); -} +} \ No newline at end of file diff --git a/server/src/web_util.rs b/server/src/web_util.rs index 0c344e6c..a6aea450 100644 --- a/server/src/web_util.rs +++ b/server/src/web_util.rs @@ -40,4 +40,4 @@ where { let body = read_json_text(&request)?; serde_json::from_str(&body).ok()? -} +} \ No newline at end of file diff --git a/server/tests/read_projects.rs b/server/tests/read_projects.rs index 216ece8d..05a85759 100644 --- a/server/tests/read_projects.rs +++ b/server/tests/read_projects.rs @@ -4,7 +4,7 @@ extern crate librojo; use std::{ collections::HashMap, - path::PathBuf, + path::{Path, PathBuf}, }; use librojo::{ @@ -13,9 +13,7 @@ use librojo::{ lazy_static! { static ref TEST_PROJECTS_ROOT: PathBuf = { - let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - path.push("../test-projects"); - path + Path::new(env!("CARGO_MANIFEST_DIR")).join("../test-projects") }; }