From 61fc19e948b123406628e5c4ae4ef3de8e536c7d Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Thu, 12 Apr 2018 22:15:52 -0700 Subject: [PATCH] Remove old middleware and VFS, since they'll be replaced --- server/src/lib.rs | 3 - server/src/middleware.rs | 83 ------------- server/src/rbx.rs | 34 ------ server/src/vfs/mod.rs | 5 - server/src/vfs/vfs_item.rs | 31 ----- server/src/vfs/vfs_session.rs | 214 ---------------------------------- 6 files changed, 370 deletions(-) delete mode 100644 server/src/middleware.rs delete mode 100644 server/src/rbx.rs delete mode 100644 server/src/vfs/mod.rs delete mode 100644 server/src/vfs/vfs_item.rs delete mode 100644 server/src/vfs/vfs_session.rs diff --git a/server/src/lib.rs b/server/src/lib.rs index 76e68573..e8c645d3 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -13,11 +13,8 @@ extern crate tempfile; pub mod commands; pub mod file_route; pub mod id; -pub mod middleware; pub mod pathext; pub mod project; -pub mod rbx; pub mod rbx_session; -pub mod vfs; pub mod web; pub mod web_util; diff --git a/server/src/middleware.rs b/server/src/middleware.rs deleted file mode 100644 index 8a33aafd..00000000 --- a/server/src/middleware.rs +++ /dev/null @@ -1,83 +0,0 @@ -use rbx::RbxInstance; -use vfs::VfsItem; - -type Route = Vec; - -pub enum TransformFileResult { - Value(Option), - Pass, - - // TODO: Error case -} - -pub enum RbxChangeResult { - Write(Option), - Pass, - - // TODO: Error case -} - -pub enum FileChangeResult { - MarkChanged(Option>), - Pass, -} - -pub trait Middleware { - /// Invoked when a file is read from the filesystem and needs to be turned - /// into a Roblox instance. - fn transform_file(&self, plugins: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult; - - /// Invoked when a Roblox Instance change is reported by the Roblox Studio - /// plugin and needs to be turned into a file to save. - fn handle_rbx_change(&self, route: &Route, rbx_item: &RbxInstance) -> RbxChangeResult; - - /// Invoked when a file changes on the filesystem. The result defines what - /// routes are marked as needing to be refreshed. - fn handle_file_change(&self, route: &Route) -> FileChangeResult; -} - -/// A set of plugins that are composed in order. -pub struct MiddlewareChain { - plugins: Vec>, -} - -impl MiddlewareChain { - pub fn new(plugins: Vec>) -> MiddlewareChain { - MiddlewareChain { - plugins, - } - } - - pub fn transform_file(&self, vfs_item: &VfsItem) -> Option { - for plugin in &self.plugins { - match plugin.transform_file(self, vfs_item) { - TransformFileResult::Value(rbx_item) => return rbx_item, - TransformFileResult::Pass => {}, - } - } - - None - } - - pub fn handle_rbx_change(&self, route: &Route, rbx_item: &RbxInstance) -> Option { - for plugin in &self.plugins { - match plugin.handle_rbx_change(route, rbx_item) { - RbxChangeResult::Write(vfs_item) => return vfs_item, - RbxChangeResult::Pass => {}, - } - } - - None - } - - pub fn handle_file_change(&self, route: &Route) -> Option> { - for plugin in &self.plugins { - match plugin.handle_file_change(route) { - FileChangeResult::MarkChanged(changes) => return changes, - FileChangeResult::Pass => {}, - } - } - - None - } -} diff --git a/server/src/rbx.rs b/server/src/rbx.rs deleted file mode 100644 index f411242f..00000000 --- a/server/src/rbx.rs +++ /dev/null @@ -1,34 +0,0 @@ -use std::collections::HashMap; - -/// Represents data about a Roblox instance -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "PascalCase")] -pub struct RbxInstance { - pub name: String, - pub class_name: String, - pub children: Vec, - pub properties: HashMap, - - /// The route that this instance was generated from, if there was one. - pub route: Option>, -} - -/// Any kind value that can be used by Roblox -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "PascalCase", tag = "Type")] -pub enum RbxValue { - #[serde(rename_all = "PascalCase")] - String { - value: String, - }, - #[serde(rename_all = "PascalCase")] - Bool { - value: bool, - }, - #[serde(rename_all = "PascalCase")] - Number { - value: f64, - }, - - // TODO: Compound types like Vector3 -} diff --git a/server/src/vfs/mod.rs b/server/src/vfs/mod.rs deleted file mode 100644 index 18c44886..00000000 --- a/server/src/vfs/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod vfs_session; -mod vfs_item; - -pub use self::vfs_session::*; -pub use self::vfs_item::*; diff --git a/server/src/vfs/vfs_item.rs b/server/src/vfs/vfs_item.rs deleted file mode 100644 index ae00c6dc..00000000 --- a/server/src/vfs/vfs_item.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::collections::HashMap; - -/// A VfsItem represents either a file or directory as it came from the filesystem. -/// -/// The interface here is intentionally simplified to make it easier to traverse -/// files that have been read into memory. -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase", tag = "type")] -pub enum VfsItem { - File { - route: Vec, - contents: String, - }, - Dir { - route: Vec, - children: HashMap, - }, -} - -impl VfsItem { - pub fn name(&self) -> &String { - self.route().last().unwrap() - } - - pub fn route(&self) -> &[String] { - match self { - &VfsItem::File { ref route, .. } => route, - &VfsItem::Dir { ref route, .. } => route, - } - } -} diff --git a/server/src/vfs/vfs_session.rs b/server/src/vfs/vfs_session.rs deleted file mode 100644 index c908795b..00000000 --- a/server/src/vfs/vfs_session.rs +++ /dev/null @@ -1,214 +0,0 @@ -use std::collections::HashMap; -use std::fs::{self, File}; -use std::io::Read; -use std::path::{Path, PathBuf}; -use std::time::Instant; - -use middleware::MiddlewareChain; -use vfs::VfsItem; - -/// Represents a virtual layer over multiple parts of the filesystem. -/// -/// Paths in this system are represented as slices of strings, and are always -/// relative to a partition, which is an absolute path into the real filesystem. -pub struct VfsSession { - /// Contains all of the partitions mounted by the Vfs. - /// - /// These must be absolute paths! - partitions: HashMap, - - /// A chronologically-sorted list of routes that changed since the Vfs was - /// created, along with a timestamp denoting when. - change_history: Vec, - - /// When the Vfs was initialized; used for change tracking. - start_time: Instant, - - middleware_chain: &'static MiddlewareChain, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct VfsChange { - timestamp: f64, - route: Vec, -} - -impl VfsSession { - pub fn new(middleware_chain: &'static MiddlewareChain) -> VfsSession { - VfsSession { - partitions: HashMap::new(), - start_time: Instant::now(), - change_history: Vec::new(), - middleware_chain, - } - } - - pub fn get_partitions(&self) -> &HashMap { - &self.partitions - } - - pub fn insert_partition>(&mut self, name: &str, path: P) { - let path = path.into(); - - assert!(path.is_absolute()); - - self.partitions.insert(name.to_string(), path.into()); - } - - fn route_to_path(&self, route: &[String]) -> Option { - let (partition_name, rest) = match route.split_first() { - Some((first, rest)) => (first, rest), - None => return None, - }; - - let partition = match self.partitions.get(partition_name) { - Some(v) => v, - None => return None, - }; - - // It's possible that the partition points to a file if `rest` is empty. - // Joining "" onto a path will put a trailing slash on, which causes - // file reads to fail. - let full_path = if rest.is_empty() { - partition.clone() - } else { - let joined = rest.join("/"); - let relative = Path::new(&joined); - - partition.join(relative) - }; - - Some(full_path) - } - - fn read_dir>(&self, route: &[String], path: P) -> Result { - let path = path.as_ref(); - let reader = match fs::read_dir(path) { - Ok(v) => v, - Err(_) => return Err(()), - }; - - let mut children = HashMap::new(); - - for entry in reader { - let entry = match entry { - Ok(v) => v, - Err(_) => return Err(()), - }; - - let path = entry.path(); - let name = path.file_name().unwrap().to_string_lossy().into_owned(); - - let mut child_route = route.iter().cloned().collect::>(); - child_route.push(name.clone()); - - match self.read_path(&child_route, &path) { - Ok(child_item) => { - children.insert(name, child_item); - }, - Err(_) => {}, - } - } - - Ok(VfsItem::Dir { - route: route.iter().cloned().collect::>(), - children, - }) - } - - fn read_file>(&self, route: &[String], path: P) -> Result { - let path = path.as_ref(); - let mut file = match File::open(path) { - Ok(v) => v, - Err(_) => return Err(()), - }; - - let mut contents = String::new(); - - match file.read_to_string(&mut contents) { - Ok(_) => {}, - Err(_) => return Err(()), - } - - Ok(VfsItem::File { - route: route.iter().cloned().collect::>(), - contents, - }) - } - - fn read_path>(&self, route: &[String], path: P) -> Result { - let path = path.as_ref(); - - let metadata = match fs::metadata(path) { - Ok(v) => v, - Err(_) => return Err(()), - }; - - if metadata.is_dir() { - self.read_dir(route, path) - } else if metadata.is_file() { - self.read_file(route, path) - } else { - Err(()) - } - } - - /// Get the current time, used for logging timestamps for file changes. - pub fn current_time(&self) -> f64 { - let elapsed = self.start_time.elapsed(); - - elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 * 1e-9 - } - - /// Register a new change to the filesystem at the given timestamp and VFS - /// route. - pub fn add_change(&mut self, timestamp: f64, route: Vec) { - match self.middleware_chain.handle_file_change(&route) { - Some(routes) => { - for route in routes { - self.change_history.push(VfsChange { - timestamp, - route, - }); - } - }, - None => {} - } - } - - /// Collect a list of changes that occured since the given timestamp. - pub fn changes_since(&self, timestamp: f64) -> &[VfsChange] { - let mut marker: Option = None; - - for (index, value) in self.change_history.iter().enumerate().rev() { - if value.timestamp >= timestamp { - marker = Some(index); - } else { - break; - } - } - - if let Some(index) = marker { - &self.change_history[index..] - } else { - &self.change_history[..0] - } - } - - /// Read an item from the filesystem using the given VFS route. - pub fn read(&self, route: &[String]) -> Result { - match self.route_to_path(route) { - Some(path) => self.read_path(route, &path), - None => Err(()), - } - } - - pub fn write(&self, _route: &[String], _item: VfsItem) -> Result<(), ()> { - unimplemented!() - } - - pub fn delete(&self, _route: &[String]) -> Result<(), ()> { - unimplemented!() - } -}