From 3620a9d25690c9c4b18aa345585ce7ab23607c09 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 16 Jan 2019 16:36:22 -0800 Subject: [PATCH] Thread Cow<'str> through for naming nodes --- server/src/rbx_session.rs | 5 ++- server/src/rbx_snapshot.rs | 67 +++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/server/src/rbx_session.rs b/server/src/rbx_session.rs index 1820424e..052d2c1e 100644 --- a/server/src/rbx_session.rs +++ b/server/src/rbx_session.rs @@ -1,4 +1,5 @@ use std::{ + borrow::Cow, collections::HashMap, path::{Path, PathBuf}, str, @@ -80,10 +81,12 @@ impl RbxSession { trace!("Snapshotting path {}", path_to_snapshot.display()); + let instance_name = self.sync_point_names.get(&path_to_snapshot) + .map(|value| Cow::Owned(value.to_owned())); let mut snapshot_meta = SnapshotMetadata { sync_point_names: &mut self.sync_point_names, }; - let maybe_snapshot = snapshot_imfs_path(&imfs, &mut snapshot_meta, &path_to_snapshot) + let maybe_snapshot = snapshot_imfs_path(&imfs, &mut snapshot_meta, &path_to_snapshot, instance_name) .unwrap_or_else(|_| panic!("Could not generate instance snapshot for path {}", path_to_snapshot.display())); let snapshot = match maybe_snapshot { diff --git a/server/src/rbx_snapshot.rs b/server/src/rbx_snapshot.rs index 73ee3ea5..2a10de3b 100644 --- a/server/src/rbx_snapshot.rs +++ b/server/src/rbx_snapshot.rs @@ -83,14 +83,14 @@ pub fn snapshot_project_tree<'source>( metadata: &mut SnapshotMetadata, project: &'source Project, ) -> SnapshotResult<'source> { - snapshot_project_node(imfs, metadata, &project.tree, &project.name) + snapshot_project_node(imfs, metadata, &project.tree, Cow::Borrowed(&project.name)) } fn snapshot_project_node<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, node: &'source ProjectNode, - instance_name: &'source str, + instance_name: Cow<'source, str>, ) -> SnapshotResult<'source> { match node { ProjectNode::Instance(instance_node) => snapshot_instance_node(imfs, metadata, instance_node, instance_name), @@ -102,19 +102,19 @@ fn snapshot_instance_node<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, node: &'source InstanceProjectNode, - instance_name: &'source str, + instance_name: Cow<'source, str>, ) -> SnapshotResult<'source> { let mut children = Vec::new(); for (child_name, child_project_node) in &node.children { - if let Some(child) = snapshot_project_node(imfs, metadata, child_project_node, child_name)? { + if let Some(child) = snapshot_project_node(imfs, metadata, child_project_node, Cow::Borrowed(child_name))? { children.push(child); } } Ok(Some(RbxSnapshotInstance { class_name: Cow::Borrowed(&node.class_name), - name: Cow::Borrowed(instance_name), + name: instance_name, properties: node.properties.clone(), children, source_path: None, @@ -126,19 +126,19 @@ fn snapshot_sync_point_node<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, node: &'source SyncPointProjectNode, - instance_name: &'source str, + instance_name: Cow<'source, str>, ) -> SnapshotResult<'source> { + let maybe_snapshot = snapshot_imfs_path(imfs, metadata, &node.path, Some(instance_name))?; + // If the snapshot resulted in no instances, like if it targets an unknown // file or an empty model file, we can early-return. - let mut snapshot = match snapshot_imfs_path(imfs, metadata, &node.path)? { + let snapshot = match maybe_snapshot { Some(snapshot) => snapshot, None => return Ok(None), }; - // Otherwise, we can mutate the snapshot we got back and track some extra - // metadata. - snapshot.name = Cow::Borrowed(instance_name); - metadata.sync_point_names.insert(node.path.to_owned(), instance_name.to_owned()); + // Otherwise, we can log the name of the sync point we just snapshotted. + metadata.sync_point_names.insert(node.path.to_owned(), snapshot.name.clone().into_owned()); Ok(Some(snapshot)) } @@ -146,12 +146,13 @@ fn snapshot_sync_point_node<'source>( pub fn snapshot_imfs_path<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, - path: &Path + path: &Path, + instance_name: Option>, ) -> SnapshotResult<'source> { // If the given path doesn't exist in the in-memory filesystem, we consider // that an error. match imfs.get(path) { - Some(imfs_item) => snapshot_imfs_item(imfs, metadata, imfs_item), + Some(imfs_item) => snapshot_imfs_item(imfs, metadata, imfs_item, instance_name), None => return Err(SnapshotError::DidNotExist(path.to_owned())), } } @@ -160,10 +161,11 @@ fn snapshot_imfs_item<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, item: &'source ImfsItem, + instance_name: Option>, ) -> SnapshotResult<'source> { match item { - ImfsItem::File(file) => snapshot_imfs_file(metadata, file), - ImfsItem::Directory(directory) => snapshot_imfs_directory(imfs, metadata, directory), + ImfsItem::File(file) => snapshot_imfs_file(file, instance_name), + ImfsItem::Directory(directory) => snapshot_imfs_directory(imfs, metadata, directory, instance_name), } } @@ -171,39 +173,36 @@ fn snapshot_imfs_directory<'source>( imfs: &'source Imfs, metadata: &mut SnapshotMetadata, directory: &'source ImfsDirectory, + instance_name: Option>, ) -> SnapshotResult<'source> { let init_path = directory.path.join(INIT_MODULE_NAME); let init_server_path = directory.path.join(INIT_SERVER_NAME); let init_client_path = directory.path.join(INIT_CLIENT_NAME); + let snapshot_name = instance_name + .unwrap_or_else(|| { + Cow::Borrowed(directory.path + .file_name().expect("Could not extract file name") + .to_str().expect("Could not convert path to UTF-8")) + }); + let mut snapshot = if directory.children.contains(&init_path) { - snapshot_imfs_path(imfs, metadata, &init_path)?.unwrap() + snapshot_imfs_path(imfs, metadata, &init_path, Some(snapshot_name.clone()))?.unwrap() } else if directory.children.contains(&init_server_path) { - snapshot_imfs_path(imfs, metadata, &init_server_path)?.unwrap() + snapshot_imfs_path(imfs, metadata, &init_server_path, Some(snapshot_name.clone()))?.unwrap() } else if directory.children.contains(&init_client_path) { - snapshot_imfs_path(imfs, metadata, &init_client_path)?.unwrap() + snapshot_imfs_path(imfs, metadata, &init_client_path, Some(snapshot_name.clone()))?.unwrap() } else { RbxSnapshotInstance { class_name: Cow::Borrowed("Folder"), name: Cow::Borrowed(""), properties: HashMap::new(), children: Vec::new(), - source_path: Some(directory.path.clone()), + source_path: Some(directory.path.to_owned()), metadata: None, } }; - // We have to be careful not to lose instance names that are specified in - // the project manifest. We store them in sync_point_names when the original - // tree is constructed. - snapshot.name = if let Some(actual_name) = metadata.sync_point_names.get(&directory.path) { - Cow::Owned(actual_name.clone()) - } else { - Cow::Borrowed(directory.path - .file_name().expect("Could not extract file name") - .to_str().expect("Could not convert path to UTF-8")) - }; - for child_path in &directory.children { let child_name = child_path .file_name().expect("Couldn't extract file name") @@ -216,7 +215,7 @@ fn snapshot_imfs_directory<'source>( // them here. }, _ => { - if let Some(child) = snapshot_imfs_path(imfs, metadata, child_path)? { + if let Some(child) = snapshot_imfs_path(imfs, metadata, child_path, None)? { snapshot.children.push(child); } }, @@ -227,8 +226,8 @@ fn snapshot_imfs_directory<'source>( } fn snapshot_imfs_file<'source>( - metadata: &mut SnapshotMetadata, file: &'source ImfsFile, + instance_name: Option>, ) -> SnapshotResult<'source> { let extension = file.path.extension() .map(|v| v.to_str().expect("Could not convert extension to UTF-8")); @@ -244,8 +243,8 @@ fn snapshot_imfs_file<'source>( if let Some(snapshot) = maybe_snapshot.as_mut() { // Carefully preserve name from project manifest if present. - if let Some(actual_name) = metadata.sync_point_names.get(&file.path) { - snapshot.name = Cow::Owned(actual_name.clone()); + if let Some(snapshot_name) = instance_name { + snapshot.name = snapshot_name; } }