diff --git a/src/imfs/imfs.rs b/src/imfs/imfs.rs index 97eb3806..915f9f84 100644 --- a/src/imfs/imfs.rs +++ b/src/imfs/imfs.rs @@ -40,18 +40,6 @@ impl Imfs { self.fetcher.receiver() } - pub fn commit_pending_changes(&mut self) -> FsResult> { - let receiver = self.fetcher.receiver(); - let mut changes = Vec::new(); - - while let Ok(event) = receiver.try_recv() { - self.commit_change(&event)?; - changes.push(event); - } - - Ok(changes) - } - pub fn commit_change(&mut self, event: &ImfsEvent) -> FsResult<()> { use notify::DebouncedEvent::*; @@ -84,35 +72,6 @@ impl Imfs { Ok(()) } - pub fn load_from_snapshot(&mut self, path: impl AsRef, snapshot: ImfsSnapshot) { - let path = path.as_ref(); - - match snapshot { - ImfsSnapshot::File(file) => { - self.inner.insert( - path.to_path_buf(), - ImfsItem::File(ImfsFile { - path: path.to_path_buf(), - contents: Some(file.contents), - }), - ); - } - ImfsSnapshot::Directory(directory) => { - self.inner.insert( - path.to_path_buf(), - ImfsItem::Directory(ImfsDirectory { - path: path.to_path_buf(), - children_enumerated: true, - }), - ); - - for (child_name, child) in directory.children.into_iter() { - self.load_from_snapshot(path.join(child_name), child); - } - } - } - } - fn raise_file_changed(&mut self, path: impl AsRef) -> FsResult<()> { let path = path.as_ref(); @@ -306,6 +265,7 @@ impl Imfs { /// Contains extra methods that should only be used for debugging. They're /// broken out into a separate trait to make it more explicit to depend on them. pub trait ImfsDebug { + fn debug_load_snapshot>(&mut self, path: P, snapshot: ImfsSnapshot); fn debug_is_file(&self, path: &Path) -> bool; fn debug_contents<'a>(&'a self, path: &Path) -> Option<&'a [u8]>; fn debug_children<'a>(&'a self, path: &Path) -> Option<(bool, Vec<&'a Path>)>; @@ -313,6 +273,35 @@ pub trait ImfsDebug { } impl ImfsDebug for Imfs { + fn debug_load_snapshot>(&mut self, path: P, snapshot: ImfsSnapshot) { + let path = path.as_ref(); + + match snapshot { + ImfsSnapshot::File(file) => { + self.inner.insert( + path.to_path_buf(), + ImfsItem::File(ImfsFile { + path: path.to_path_buf(), + contents: Some(file.contents), + }), + ); + } + ImfsSnapshot::Directory(directory) => { + self.inner.insert( + path.to_path_buf(), + ImfsItem::Directory(ImfsDirectory { + path: path.to_path_buf(), + children_enumerated: true, + }), + ); + + for (child_name, child) in directory.children.into_iter() { + self.debug_load_snapshot(path.join(child_name), child); + } + } + } + } + fn debug_is_file(&self, path: &Path) -> bool { match self.inner.get(path) { Some(ImfsItem::File(_)) => true, @@ -432,7 +421,7 @@ mod test { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("hello, world!"); - imfs.load_from_snapshot("/hello.txt", file); + imfs.debug_load_snapshot("/hello.txt", file); let entry = imfs.get_contents("/hello.txt").unwrap(); assert_eq!(entry, b"hello, world!"); @@ -446,7 +435,7 @@ mod test { "b.lua" => ImfsSnapshot::file("contents of b.lua"), }); - imfs.load_from_snapshot("/dir", dir); + imfs.debug_load_snapshot("/dir", dir); let children = imfs.get_children("/dir").unwrap(); @@ -560,7 +549,7 @@ mod test { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("hello, world!"); - imfs.load_from_snapshot("/hello.txt", file); + imfs.debug_load_snapshot("/hello.txt", file); let hello = imfs.get("/hello.txt").expect("couldn't get hello.txt"); diff --git a/src/snapshot_middleware/csv.rs b/src/snapshot_middleware/csv.rs index 9c87f200..804a03e5 100644 --- a/src/snapshot_middleware/csv.rs +++ b/src/snapshot_middleware/csv.rs @@ -136,7 +136,7 @@ fn convert_localization_csv(contents: &[u8]) -> String { mod test { use super::*; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn csv_from_imfs() { @@ -147,7 +147,7 @@ Key,Source,Context,Example,es Ack,Ack!,,An exclamation of despair,¡Ay!"#, ); - imfs.load_from_snapshot("/foo.csv", file); + imfs.debug_load_snapshot("/foo.csv", file); let entry = imfs.get("/foo.csv").unwrap(); let instance_snapshot = SnapshotCsv::from_imfs(&mut imfs, &entry).unwrap().unwrap(); diff --git a/src/snapshot_middleware/dir.rs b/src/snapshot_middleware/dir.rs index 1e8d50d3..a826c43f 100644 --- a/src/snapshot_middleware/dir.rs +++ b/src/snapshot_middleware/dir.rs @@ -78,14 +78,14 @@ mod test { use maplit::hashmap; - use crate::imfs::NoopFetcher; + use crate::imfs::{ImfsDebug, NoopFetcher}; #[test] fn empty_folder() { let mut imfs = Imfs::new(NoopFetcher); let dir = ImfsSnapshot::dir::(HashMap::new()); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotDir::from_imfs(&mut imfs, &entry).unwrap().unwrap(); @@ -103,7 +103,7 @@ mod test { "Child" => ImfsSnapshot::dir::(HashMap::new()), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotDir::from_imfs(&mut imfs, &entry).unwrap().unwrap(); diff --git a/src/snapshot_middleware/json_model.rs b/src/snapshot_middleware/json_model.rs index 6b026557..3cbcc81b 100644 --- a/src/snapshot_middleware/json_model.rs +++ b/src/snapshot_middleware/json_model.rs @@ -136,7 +136,7 @@ mod test { use maplit::hashmap; use rbx_dom_weak::RbxValue; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn model_from_imfs() { @@ -159,7 +159,7 @@ mod test { "#, ); - imfs.load_from_snapshot("/foo.model.json", file); + imfs.debug_load_snapshot("/foo.model.json", file); let entry = imfs.get("/foo.model.json").unwrap(); let instance_snapshot = SnapshotJsonModel::from_imfs(&mut imfs, &entry) diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index 779fc740..f7d5b4ee 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -141,14 +141,14 @@ mod test { use maplit::hashmap; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn module_from_imfs() { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("Hello there!"); - imfs.load_from_snapshot("/foo.lua", file); + imfs.debug_load_snapshot("/foo.lua", file); let entry = imfs.get("/foo.lua").unwrap(); let instance_snapshot = SnapshotLua::from_imfs(&mut imfs, &entry).unwrap().unwrap(); @@ -170,7 +170,7 @@ mod test { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("Hello there!"); - imfs.load_from_snapshot("/foo.server.lua", file); + imfs.debug_load_snapshot("/foo.server.lua", file); let entry = imfs.get("/foo.server.lua").unwrap(); let instance_snapshot = SnapshotLua::from_imfs(&mut imfs, &entry).unwrap().unwrap(); @@ -192,7 +192,7 @@ mod test { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("Hello there!"); - imfs.load_from_snapshot("/foo.client.lua", file); + imfs.debug_load_snapshot("/foo.client.lua", file); let entry = imfs.get("/foo.client.lua").unwrap(); let instance_snapshot = SnapshotLua::from_imfs(&mut imfs, &entry).unwrap().unwrap(); diff --git a/src/snapshot_middleware/project.rs b/src/snapshot_middleware/project.rs index 4f68fe1c..6d06e332 100644 --- a/src/snapshot_middleware/project.rs +++ b/src/snapshot_middleware/project.rs @@ -143,7 +143,7 @@ mod test { use maplit::hashmap; use rbx_dom_weak::RbxValue; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn project_from_folder() { @@ -161,7 +161,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -190,7 +190,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo/hello.project.json").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -225,7 +225,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -264,7 +264,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -304,7 +304,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -340,7 +340,7 @@ mod test { "other.txt" => ImfsSnapshot::file("Hello, world!"), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -384,7 +384,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -425,7 +425,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) @@ -477,7 +477,7 @@ mod test { "#), }); - imfs.load_from_snapshot("/foo", dir); + imfs.debug_load_snapshot("/foo", dir); let entry = imfs.get("/foo").unwrap(); let instance_snapshot = SnapshotProject::from_imfs(&mut imfs, &entry) diff --git a/src/snapshot_middleware/rbxm.rs b/src/snapshot_middleware/rbxm.rs index 210d64ee..d14f7279 100644 --- a/src/snapshot_middleware/rbxm.rs +++ b/src/snapshot_middleware/rbxm.rs @@ -66,14 +66,14 @@ impl SnapshotMiddleware for SnapshotRbxm { mod test { use super::*; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn model_from_imfs() { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file(include_bytes!("../../assets/test-folder.rbxm").to_vec()); - imfs.load_from_snapshot("/foo.rbxm", file); + imfs.debug_load_snapshot("/foo.rbxm", file); let entry = imfs.get("/foo.rbxm").unwrap(); let instance_snapshot = SnapshotRbxm::from_imfs(&mut imfs, &entry).unwrap().unwrap(); diff --git a/src/snapshot_middleware/rbxmx.rs b/src/snapshot_middleware/rbxmx.rs index 3f0c2814..fde908bd 100644 --- a/src/snapshot_middleware/rbxmx.rs +++ b/src/snapshot_middleware/rbxmx.rs @@ -64,7 +64,7 @@ mod test { use std::collections::HashMap; - use crate::imfs::{ImfsSnapshot, NoopFetcher}; + use crate::imfs::{ImfsDebug, ImfsSnapshot, NoopFetcher}; #[test] fn model_from_imfs() { @@ -81,7 +81,7 @@ mod test { "#, ); - imfs.load_from_snapshot("/foo.rbxmx", file); + imfs.debug_load_snapshot("/foo.rbxmx", file); let entry = imfs.get("/foo.rbxmx").unwrap(); let instance_snapshot = SnapshotRbxmx::from_imfs(&mut imfs, &entry) diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index ea750b42..f35495ae 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -94,14 +94,14 @@ mod test { use maplit::hashmap; use rbx_dom_weak::RbxInstanceProperties; - use crate::imfs::NoopFetcher; + use crate::imfs::{ImfsDebug, NoopFetcher}; #[test] fn instance_from_imfs() { let mut imfs = Imfs::new(NoopFetcher); let file = ImfsSnapshot::file("Hello there!"); - imfs.load_from_snapshot("/foo.txt", file); + imfs.debug_load_snapshot("/foo.txt", file); let entry = imfs.get("/foo.txt").unwrap(); let instance_snapshot = SnapshotTxt::from_imfs(&mut imfs, &entry).unwrap().unwrap();