Delete middleware, shuffle around session for future refactor

This commit is contained in:
Lucien Greathouse 2018-04-09 00:25:20 -07:00
parent c13ad3f34d
commit 15ffa86a66
6 changed files with 34 additions and 260 deletions

View file

@ -20,5 +20,4 @@ pub mod pathext;
pub mod vfs;
pub mod rbx;
pub mod middleware;
pub mod middlewares;
pub mod commands;

View file

@ -1,67 +0,0 @@
use std::collections::HashMap;
use core::Route;
use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem;
/// A middleware with simple transforms:
/// * Directories become Folder instances
/// * Files become StringValue objects with 'Value' as their contents
pub struct DefaultMiddleware;
impl DefaultMiddleware {
pub fn new() -> DefaultMiddleware {
DefaultMiddleware
}
}
impl Middleware for DefaultMiddleware {
fn transform_file(&self, middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let mut properties = HashMap::new();
properties.insert("Value".to_string(), RbxValue::String {
value: contents.clone(),
});
TransformFileResult::Value(Some(RbxInstance {
name: vfs_item.name().clone(),
class_name: "StringValue".to_string(),
children: Vec::new(),
properties,
route: Some(vfs_item.route().to_vec()),
}))
},
&VfsItem::Dir { ref children, .. } => {
let mut rbx_children = Vec::new();
for (_, child_item) in children {
match middlewares.transform_file(child_item) {
Some(rbx_item) => {
rbx_children.push(rbx_item);
},
_ => {},
}
}
TransformFileResult::Value(Some(RbxInstance {
name: vfs_item.name().clone(),
class_name: "*".to_string(),
children: rbx_children,
properties: HashMap::new(),
route: Some(vfs_item.route().to_vec()),
}))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
FileChangeResult::MarkChanged(Some(vec![route.clone()]))
}
fn handle_rbx_change(&self, _route: &Route, _rbx_item: &RbxInstance) -> RbxChangeResult {
RbxChangeResult::Pass
}
}

View file

@ -1,55 +0,0 @@
use regex::Regex;
use serde_json;
use core::Route;
use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::RbxInstance;
use vfs::VfsItem;
lazy_static! {
static ref JSON_MODEL_PATTERN: Regex = Regex::new(r"^(.*?)\.model\.json$").unwrap();
}
pub struct JsonModelMiddleware;
impl JsonModelMiddleware {
pub fn new() -> JsonModelMiddleware {
JsonModelMiddleware
}
}
impl Middleware for JsonModelMiddleware {
fn transform_file(&self, _middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let rbx_name = match JSON_MODEL_PATTERN.captures(vfs_item.name()) {
Some(captures) => captures.get(1).unwrap().as_str().to_string(),
None => return TransformFileResult::Pass,
};
let mut rbx_item: RbxInstance = match serde_json::from_str(contents) {
Ok(v) => v,
Err(e) => {
eprintln!("Unable to parse JSON Model File named {}: {}", vfs_item.name(), e);
return TransformFileResult::Pass; // This should be an error in the future!
},
};
rbx_item.route = Some(vfs_item.route().to_vec());
rbx_item.name = rbx_name;
TransformFileResult::Value(Some(rbx_item))
},
&VfsItem::Dir { .. } => TransformFileResult::Pass,
}
}
fn handle_file_change(&self, _route: &Route) -> FileChangeResult {
FileChangeResult::Pass
}
fn handle_rbx_change(&self, _route: &Route, _rbx_item: &RbxInstance) -> RbxChangeResult {
RbxChangeResult::Pass
}
}

View file

@ -1,7 +0,0 @@
mod default_middleware;
mod script_middleware;
mod json_model_middleware;
pub use self::default_middleware::*;
pub use self::script_middleware::*;
pub use self::json_model_middleware::*;

View file

@ -1,124 +0,0 @@
use std::collections::HashMap;
use regex::Regex;
use core::Route;
use middleware::{Middleware, MiddlewareChain, TransformFileResult, RbxChangeResult, FileChangeResult};
use rbx::{RbxInstance, RbxValue};
use vfs::VfsItem;
lazy_static! {
static ref SERVER_PATTERN: Regex = Regex::new(r"^(.*?)\.server\.lua$").unwrap();
static ref CLIENT_PATTERN: Regex = Regex::new(r"^(.*?)\.client\.lua$").unwrap();
static ref MODULE_PATTERN: Regex = Regex::new(r"^(.*?)\.lua$").unwrap();
}
static SERVER_INIT: &'static str = "init.server.lua";
static CLIENT_INIT: &'static str = "init.client.lua";
static MODULE_INIT: &'static str = "init.lua";
pub struct ScriptMiddleware;
impl ScriptMiddleware {
pub fn new() -> ScriptMiddleware {
ScriptMiddleware
}
}
impl Middleware for ScriptMiddleware {
fn transform_file(&self, middlewares: &MiddlewareChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item {
&VfsItem::File { ref contents, .. } => {
let name = vfs_item.name();
let (class_name, rbx_name) = {
if let Some(captures) = SERVER_PATTERN.captures(name) {
("Script".to_string(), captures.get(1).unwrap().as_str().to_string())
} else if let Some(captures) = CLIENT_PATTERN.captures(name) {
("LocalScript".to_string(), captures.get(1).unwrap().as_str().to_string())
} else if let Some(captures) = MODULE_PATTERN.captures(name) {
("ModuleScript".to_string(), captures.get(1).unwrap().as_str().to_string())
} else {
return TransformFileResult::Pass;
}
};
let mut properties = HashMap::new();
properties.insert("Source".to_string(), RbxValue::String {
value: contents.clone(),
});
TransformFileResult::Value(Some(RbxInstance {
name: rbx_name,
class_name: class_name,
children: Vec::new(),
properties,
route: Some(vfs_item.route().to_vec()),
}))
},
&VfsItem::Dir { ref children, .. } => {
let init_item = {
let maybe_item = children.get(SERVER_INIT)
.or(children.get(CLIENT_INIT))
.or(children.get(MODULE_INIT));
match maybe_item {
Some(v) => v,
None => return TransformFileResult::Pass,
}
};
let mut rbx_item = match self.transform_file(middlewares, init_item) {
TransformFileResult::Value(Some(item)) => item,
_ => {
eprintln!("Inconsistency detected in ScriptMiddleware!");
return TransformFileResult::Pass;
},
};
rbx_item.name.clear();
rbx_item.name.push_str(vfs_item.name());
for (child_name, child_item) in children {
if child_name == init_item.name() {
continue;
}
match middlewares.transform_file(child_item) {
Some(child_rbx_item) => {
rbx_item.children.push(child_rbx_item);
},
_ => {},
}
}
TransformFileResult::Value(Some(rbx_item))
},
}
}
fn handle_file_change(&self, route: &Route) -> FileChangeResult {
let leaf = match route.last() {
Some(v) => v,
None => return FileChangeResult::Pass,
};
let is_init = leaf == SERVER_INIT
|| leaf == CLIENT_INIT
|| leaf == MODULE_INIT;
if is_init {
let mut changed = route.clone();
changed.pop();
FileChangeResult::MarkChanged(Some(vec![changed]))
} else {
FileChangeResult::Pass
}
}
fn handle_rbx_change(&self, _route: &Route, _rbx_item: &RbxInstance) -> RbxChangeResult {
RbxChangeResult::Pass
}
}

View file

@ -1,13 +1,13 @@
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::io::Read;
use std::fs::{self, File};
use id::{Id};
// TODO: Add lifetime, switch to using Cow<'a, str> instead of String. It's
// TODO: Add lifetime, switch to using Cow<'a, str> instead of String? It's
// possible that it would be too cumbersome!
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Hash)]
struct FileRoute {
pub partition: String,
pub route: Vec<String>,
@ -54,13 +54,38 @@ enum FileItem {
}
struct RbxInstance {
pub class_name: String,
pub parent: Option<Id>,
pub properties: HashMap<String, String>,
}
struct RbxSession {
pub partition_paths: HashMap<String, PathBuf>,
pub partition_instances: HashMap<String, Id>,
pub partition_files: HashMap<String, FileItem>,
pub instances: HashMap<Id, RbxInstance>,
}
fn file_to_instance(file_item: &FileItem) -> RbxInstance {
match file_item {
&FileItem::File { ref contents } => {
let mut properties = HashMap::new();
properties.insert("Value".to_string(), contents.clone());
RbxInstance {
class_name: "StringValue".to_string(),
parent: None,
properties,
}
},
&FileItem::Directory { ref children } => {
RbxInstance {
class_name: "Folder".to_string(),
parent: None,
properties: HashMap::new(),
}
}
}
}
impl RbxSession {
@ -69,11 +94,12 @@ impl RbxSession {
partition_paths: HashMap::new(),
partition_instances: HashMap::new(),
partition_files: HashMap::new(),
instances: HashMap::new(),
}
}
fn load(&mut self) {
for (partition_name, partition_path) in &self.partition_paths {
fn load_files(&mut self) {
for partition_name in self.partition_paths.keys() {
let route = FileRoute {
partition: partition_name.clone(),
route: vec![],
@ -166,6 +192,8 @@ mod tests {
#[test]
fn file_items_correct() {
use std::io::Write;
let root_dir = tempfile::tempdir().unwrap();
let foo_path = root_dir.path().join("foo.txt");
@ -183,7 +211,7 @@ mod tests {
session.partition_paths.insert("agh".to_string(), root_dir.path().to_path_buf());
session.load();
session.load_files();
assert_eq!(session.partition_files.len(), 1);