Change each VfsItem to keep a full route instead of just its name

This commit is contained in:
Lucien Greathouse
2018-01-03 15:56:19 -08:00
parent 13ce04abb2
commit 9720c56765
4 changed files with 44 additions and 31 deletions

View File

@@ -19,7 +19,7 @@ impl DefaultPlugin {
impl Plugin for DefaultPlugin { impl Plugin for DefaultPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, ref name } => { &VfsItem::File { ref contents, .. } => {
let mut properties = HashMap::new(); let mut properties = HashMap::new();
properties.insert("Value".to_string(), RbxValue::String { properties.insert("Value".to_string(), RbxValue::String {
@@ -27,13 +27,13 @@ impl Plugin for DefaultPlugin {
}); });
TransformFileResult::Value(Some(RbxItem { TransformFileResult::Value(Some(RbxItem {
name: name.clone(), name: vfs_item.name().clone(),
class_name: "StringValue".to_string(), class_name: "StringValue".to_string(),
children: Vec::new(), children: Vec::new(),
properties, properties,
})) }))
}, },
&VfsItem::Dir { ref children, ref name } => { &VfsItem::Dir { ref children, .. } => {
let mut rbx_children = Vec::new(); let mut rbx_children = Vec::new();
for (_, child_item) in children { for (_, child_item) in children {
@@ -46,7 +46,7 @@ impl Plugin for DefaultPlugin {
} }
TransformFileResult::Value(Some(RbxItem { TransformFileResult::Value(Some(RbxItem {
name: name.clone(), name: vfs_item.name().clone(),
class_name: "Folder".to_string(), class_name: "Folder".to_string(),
children: rbx_children, children: rbx_children,
properties: HashMap::new(), properties: HashMap::new(),

View File

@@ -21,8 +21,8 @@ impl JsonModelPlugin {
impl Plugin for JsonModelPlugin { impl Plugin for JsonModelPlugin {
fn transform_file(&self, _plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, _plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, ref name } => { &VfsItem::File { ref contents, .. } => {
let rbx_name = match JSON_MODEL_PATTERN.captures(name) { let rbx_name = match JSON_MODEL_PATTERN.captures(vfs_item.name()) {
Some(captures) => captures.get(1).unwrap().as_str().to_string(), Some(captures) => captures.get(1).unwrap().as_str().to_string(),
None => return TransformFileResult::Pass, None => return TransformFileResult::Pass,
}; };
@@ -30,7 +30,7 @@ impl Plugin for JsonModelPlugin {
let mut rbx_item: RbxItem = match serde_json::from_str(contents) { let mut rbx_item: RbxItem = match serde_json::from_str(contents) {
Ok(v) => v, Ok(v) => v,
Err(_) => { 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! return TransformFileResult::Pass; // This should be an error in the future!
}, },

View File

@@ -28,7 +28,9 @@ impl ScriptPlugin {
impl Plugin for ScriptPlugin { impl Plugin for ScriptPlugin {
fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult { fn transform_file(&self, plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
match vfs_item { match vfs_item {
&VfsItem::File { ref contents, ref name } => { &VfsItem::File { ref contents, .. } => {
let name = vfs_item.name();
let (class_name, rbx_name) = { let (class_name, rbx_name) = {
if let Some(captures) = SERVER_PATTERN.captures(name) { if let Some(captures) = SERVER_PATTERN.captures(name) {
("Script".to_string(), captures.get(1).unwrap().as_str().to_string()) ("Script".to_string(), captures.get(1).unwrap().as_str().to_string())
@@ -54,7 +56,7 @@ impl Plugin for ScriptPlugin {
properties, properties,
})) }))
}, },
&VfsItem::Dir { ref children, ref name } => { &VfsItem::Dir { ref children, .. } => {
let init_item = { let init_item = {
let maybe_item = children.get(SERVER_INIT) let maybe_item = children.get(SERVER_INIT)
.or(children.get(CLIENT_INIT)) .or(children.get(CLIENT_INIT))
@@ -75,7 +77,7 @@ impl Plugin for ScriptPlugin {
}; };
rbx_item.name.clear(); 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 { for (child_name, child_item) in children {
if child_name == init_item.name() { if child_name == init_item.name() {

View File

@@ -1,4 +1,3 @@
use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::io::Read;
@@ -40,15 +39,25 @@ pub struct VfsChange {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")] #[serde(rename_all = "camelCase", tag = "type")]
pub enum VfsItem { pub enum VfsItem {
File { name: String, contents: String }, File {
Dir { name: String, children: HashMap<String, VfsItem> }, route: Vec<String>,
contents: String,
},
Dir {
route: Vec<String>,
children: HashMap<String, VfsItem>,
},
} }
impl VfsItem { impl VfsItem {
pub fn name(&self) -> &String { pub fn name(&self) -> &String {
self.route().last().unwrap()
}
pub fn route(&self) -> &[String] {
match self { match self {
&VfsItem::File { ref name, .. } => name, &VfsItem::File { ref route, .. } => route,
&VfsItem::Dir { ref name, .. } => name, &VfsItem::Dir { ref route, .. } => route,
} }
} }
} }
@@ -64,9 +73,9 @@ impl Vfs {
} }
} }
fn route_to_path<R: Borrow<str>>(&self, route: &[R]) -> Option<PathBuf> { fn route_to_path(&self, route: &[String]) -> Option<PathBuf> {
let (partition_name, rest) = match route.split_first() { let (partition_name, rest) = match route.split_first() {
Some((first, rest)) => (first.borrow(), rest), Some((first, rest)) => (first, rest),
None => return None, None => return None,
}; };
@@ -90,7 +99,7 @@ impl Vfs {
Some(full_path) Some(full_path)
} }
fn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<VfsItem, ()> { fn read_dir<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref(); let path = path.as_ref();
let reader = match fs::read_dir(path) { let reader = match fs::read_dir(path) {
Ok(v) => v, Ok(v) => v,
@@ -106,11 +115,13 @@ impl Vfs {
}; };
let path = entry.path(); 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::<Vec<_>>();
child_route.push(name.clone());
match self.read_path(&child_route, &path) {
Ok(child_item) => { Ok(child_item) => {
let name = path.file_name().unwrap().to_string_lossy().into_owned();
children.insert(name, child_item); children.insert(name, child_item);
}, },
Err(_) => {}, Err(_) => {},
@@ -118,12 +129,12 @@ impl Vfs {
} }
Ok(VfsItem::Dir { Ok(VfsItem::Dir {
name: path.file_name().unwrap().to_string_lossy().into_owned(), route: route.iter().cloned().collect::<Vec<_>>(),
children, children,
}) })
} }
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<VfsItem, ()> { fn read_file<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref(); let path = path.as_ref();
let mut file = match File::open(path) { let mut file = match File::open(path) {
Ok(v) => v, Ok(v) => v,
@@ -138,12 +149,12 @@ impl Vfs {
} }
Ok(VfsItem::File { Ok(VfsItem::File {
name: path.file_name().unwrap().to_string_lossy().into_owned(), route: route.iter().cloned().collect::<Vec<_>>(),
contents, contents,
}) })
} }
fn read_path<P: AsRef<Path>>(&self, path: P) -> Result<VfsItem, ()> { fn read_path<P: AsRef<Path>>(&self, route: &[String], path: P) -> Result<VfsItem, ()> {
let path = path.as_ref(); let path = path.as_ref();
let metadata = match fs::metadata(path) { let metadata = match fs::metadata(path) {
@@ -152,9 +163,9 @@ impl Vfs {
}; };
if metadata.is_dir() { if metadata.is_dir() {
self.read_dir(path) self.read_dir(route, path)
} else if metadata.is_file() { } else if metadata.is_file() {
self.read_file(path) self.read_file(route, path)
} else { } else {
Err(()) Err(())
} }
@@ -206,18 +217,18 @@ impl Vfs {
} }
} }
pub fn read<R: Borrow<str>>(&self, route: &[R]) -> Result<VfsItem, ()> { pub fn read(&self, route: &[String]) -> Result<VfsItem, ()> {
match self.route_to_path(route) { match self.route_to_path(route) {
Some(path) => self.read_path(&path), Some(path) => self.read_path(route, &path),
None => Err(()), None => Err(()),
} }
} }
pub fn write<R: Borrow<str>>(&self, _route: &[R], _item: VfsItem) -> Result<(), ()> { pub fn write(&self, _route: &[String], _item: VfsItem) -> Result<(), ()> {
unimplemented!() unimplemented!()
} }
pub fn delete<R: Borrow<str>>(&self, _route: &[R]) -> Result<(), ()> { pub fn delete(&self, _route: &[String]) -> Result<(), ()> {
unimplemented!() unimplemented!()
} }
} }