diff --git a/src/plugins/default_plugin.rs b/src/plugins/default_plugin.rs index a503fbe8..a870aa18 100644 --- a/src/plugins/default_plugin.rs +++ b/src/plugins/default_plugin.rs @@ -19,7 +19,7 @@ impl DefaultPlugin { impl Plugin for DefaultPlugin { fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { match vfs_item { - &VfsItem::File { ref contents, ref name } => { + &VfsItem::File { ref contents, .. } => { let mut properties = HashMap::new(); properties.insert("Value".to_string(), RbxValue::String { @@ -27,13 +27,13 @@ impl Plugin for DefaultPlugin { }); TransformFileResult::Value(Some(RbxItem { - name: name.clone(), + name: vfs_item.name().clone(), class_name: "StringValue".to_string(), children: Vec::new(), properties, })) }, - &VfsItem::Dir { ref children, ref name } => { + &VfsItem::Dir { ref children, .. } => { let mut rbx_children = Vec::new(); for (_, child_item) in children { @@ -46,7 +46,7 @@ impl Plugin for DefaultPlugin { } TransformFileResult::Value(Some(RbxItem { - name: name.clone(), + name: vfs_item.name().clone(), class_name: "Folder".to_string(), children: rbx_children, properties: HashMap::new(), diff --git a/src/plugins/json_model_plugin.rs b/src/plugins/json_model_plugin.rs index c4d6eb74..29be5b29 100644 --- a/src/plugins/json_model_plugin.rs +++ b/src/plugins/json_model_plugin.rs @@ -21,8 +21,8 @@ impl JsonModelPlugin { impl Plugin for JsonModelPlugin { fn transform_file(&self, _plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { match vfs_item { - &VfsItem::File { ref contents, ref name } => { - let rbx_name = match JSON_MODEL_PATTERN.captures(name) { + &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, }; @@ -30,7 +30,7 @@ impl Plugin for JsonModelPlugin { let mut rbx_item: RbxItem = match serde_json::from_str(contents) { Ok(v) => v, Err(_) => { - eprintln!("Unable to parse JSON Model File named {}", name); + eprintln!("Unable to parse JSON Model File named {}", vfs_item.name()); return TransformFileResult::Pass; // This should be an error in the future! }, diff --git a/src/plugins/script_plugin.rs b/src/plugins/script_plugin.rs index 38bd746a..183d5df8 100644 --- a/src/plugins/script_plugin.rs +++ b/src/plugins/script_plugin.rs @@ -28,7 +28,9 @@ impl ScriptPlugin { impl Plugin for ScriptPlugin { fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { match vfs_item { - &VfsItem::File { ref contents, ref name } => { + &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()) @@ -54,7 +56,7 @@ impl Plugin for ScriptPlugin { properties, })) }, - &VfsItem::Dir { ref children, ref name } => { + &VfsItem::Dir { ref children, .. } => { let init_item = { let maybe_item = children.get(SERVER_INIT) .or(children.get(CLIENT_INIT)) @@ -75,7 +77,7 @@ impl Plugin for ScriptPlugin { }; rbx_item.name.clear(); - rbx_item.name.push_str(name); + rbx_item.name.push_str(vfs_item.name()); for (child_name, child_item) in children { if child_name == init_item.name() { diff --git a/src/vfs.rs b/src/vfs.rs index b6fd57cc..22753533 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -1,4 +1,3 @@ -use std::borrow::Borrow; use std::collections::HashMap; use std::fs::{self, File}; use std::io::Read; @@ -40,15 +39,25 @@ pub struct VfsChange { #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase", tag = "type")] pub enum VfsItem { - File { name: String, contents: String }, - Dir { name: String, children: HashMap }, + 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 name, .. } => name, - &VfsItem::Dir { ref name, .. } => name, + &VfsItem::File { ref route, .. } => route, + &VfsItem::Dir { ref route, .. } => route, } } } @@ -64,9 +73,9 @@ impl Vfs { } } - fn route_to_path>(&self, route: &[R]) -> Option { + fn route_to_path(&self, route: &[String]) -> Option { let (partition_name, rest) = match route.split_first() { - Some((first, rest)) => (first.borrow(), rest), + Some((first, rest)) => (first, rest), None => return None, }; @@ -90,7 +99,7 @@ impl Vfs { Some(full_path) } - fn read_dir>(&self, path: P) -> Result { + fn read_dir>(&self, route: &[String], path: P) -> Result { let path = path.as_ref(); let reader = match fs::read_dir(path) { Ok(v) => v, @@ -106,11 +115,13 @@ impl Vfs { }; let path = entry.path(); + let name = path.file_name().unwrap().to_string_lossy().into_owned(); - match self.read_path(&path) { + let mut child_route = route.iter().cloned().collect::>(); + child_route.push(name.clone()); + + match self.read_path(&child_route, &path) { Ok(child_item) => { - let name = path.file_name().unwrap().to_string_lossy().into_owned(); - children.insert(name, child_item); }, Err(_) => {}, @@ -118,12 +129,12 @@ impl Vfs { } Ok(VfsItem::Dir { - name: path.file_name().unwrap().to_string_lossy().into_owned(), + route: route.iter().cloned().collect::>(), children, }) } - fn read_file>(&self, path: P) -> Result { + fn read_file>(&self, route: &[String], path: P) -> Result { let path = path.as_ref(); let mut file = match File::open(path) { Ok(v) => v, @@ -138,12 +149,12 @@ impl Vfs { } Ok(VfsItem::File { - name: path.file_name().unwrap().to_string_lossy().into_owned(), + route: route.iter().cloned().collect::>(), contents, }) } - fn read_path>(&self, path: P) -> Result { + fn read_path>(&self, route: &[String], path: P) -> Result { let path = path.as_ref(); let metadata = match fs::metadata(path) { @@ -152,9 +163,9 @@ impl Vfs { }; if metadata.is_dir() { - self.read_dir(path) + self.read_dir(route, path) } else if metadata.is_file() { - self.read_file(path) + self.read_file(route, path) } else { Err(()) } @@ -206,18 +217,18 @@ impl Vfs { } } - pub fn read>(&self, route: &[R]) -> Result { + pub fn read(&self, route: &[String]) -> Result { match self.route_to_path(route) { - Some(path) => self.read_path(&path), + Some(path) => self.read_path(route, &path), None => Err(()), } } - pub fn write>(&self, _route: &[R], _item: VfsItem) -> Result<(), ()> { + pub fn write(&self, _route: &[String], _item: VfsItem) -> Result<(), ()> { unimplemented!() } - pub fn delete>(&self, _route: &[R]) -> Result<(), ()> { + pub fn delete(&self, _route: &[String]) -> Result<(), ()> { unimplemented!() } }