diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index d60ee4b9..26dce30d 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -10,6 +10,7 @@ use crate::{ use super::{ dir::SnapshotDir, + meta_file::AdjacentMetadata, middleware::{SnapshotFileResult, SnapshotInstanceResult, SnapshotMiddleware}, }; @@ -79,6 +80,7 @@ fn snapshot_lua_file( let contents = entry.contents(imfs)?; let contents_str = str::from_utf8(contents) + // TODO: Turn into error type .expect("File content was not valid UTF-8") .to_string(); @@ -88,13 +90,30 @@ fn snapshot_lua_file( }, }; + let meta_path = entry + .path() + .with_file_name(format!("{}.meta.json", instance_name)); + + let mut metadata = InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf()], + ..Default::default() + }; + + if let Some(meta_entry) = imfs.get(meta_path).with_not_found()? { + let meta_contents = meta_entry.contents(imfs)?; + let parsed: AdjacentMetadata = serde_json::from_slice(meta_contents) + // TODO: Turn into error type + .expect(".meta.json file was malformed"); + + if let Some(ignore) = parsed.ignore_unknown_instances { + metadata.ignore_unknown_instances = ignore; + } + } + Ok(Some(InstanceSnapshot { snapshot_id: None, - metadata: InstanceMetadata { - instigating_source: Some(entry.path().to_path_buf().into()), - relevant_paths: vec![entry.path().to_path_buf()], - ..Default::default() - }, + metadata, name: Cow::Owned(instance_name.to_owned()), class_name: Cow::Borrowed(class_name), properties, @@ -185,4 +204,46 @@ mod test { assert_yaml_snapshot!(instance_snapshot); } + + #[test] + fn module_with_meta() { + let mut imfs = Imfs::new(NoopFetcher); + let file = ImfsSnapshot::file("Hello there!"); + let meta = ImfsSnapshot::file( + r#" + { + "ignoreUnknownInstances": true + } + "#, + ); + + imfs.debug_load_snapshot("/foo.lua", file); + imfs.debug_load_snapshot("/foo.meta.json", meta); + + let entry = imfs.get("/foo.lua").unwrap(); + let instance_snapshot = SnapshotLua::from_imfs(&mut imfs, &entry).unwrap().unwrap(); + + assert_yaml_snapshot!(instance_snapshot); + } + + #[test] + fn script_with_meta() { + let mut imfs = Imfs::new(NoopFetcher); + let file = ImfsSnapshot::file("Hello there!"); + let meta = ImfsSnapshot::file( + r#" + { + "ignoreUnknownInstances": true + } + "#, + ); + + imfs.debug_load_snapshot("/foo.server.lua", file); + imfs.debug_load_snapshot("/foo.meta.json", meta); + + let entry = imfs.get("/foo.server.lua").unwrap(); + let instance_snapshot = SnapshotLua::from_imfs(&mut imfs, &entry).unwrap().unwrap(); + + assert_yaml_snapshot!(instance_snapshot); + } } diff --git a/src/snapshot_middleware/meta_file.rs b/src/snapshot_middleware/meta_file.rs new file mode 100644 index 00000000..f20a5e41 --- /dev/null +++ b/src/snapshot_middleware/meta_file.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct AdjacentMetadata { + #[serde(skip_serializing_if = "Option::is_none")] + pub ignore_unknown_instances: Option, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct InitMetadata { + #[serde(skip_serializing_if = "Option::is_none")] + pub ignore_unknown_instances: Option, +} diff --git a/src/snapshot_middleware/mod.rs b/src/snapshot_middleware/mod.rs index 21f5b838..b16f3e23 100644 --- a/src/snapshot_middleware/mod.rs +++ b/src/snapshot_middleware/mod.rs @@ -9,6 +9,7 @@ mod dir; mod error; mod json_model; mod lua; +mod meta_file; mod middleware; mod project; mod rbxlx; diff --git a/src/snapshot_middleware/snapshots/test__module_with_meta.snap b/src/snapshot_middleware/snapshots/test__module_with_meta.snap new file mode 100644 index 00000000..d54f1133 --- /dev/null +++ b/src/snapshot_middleware/snapshots/test__module_with_meta.snap @@ -0,0 +1,18 @@ +--- +source: src/snapshot_middleware/lua.rs +expression: instance_snapshot +--- +snapshot_id: ~ +metadata: + ignore_unknown_instances: true + instigating_source: + Path: /foo.lua + relevant_paths: + - /foo.lua +name: foo +class_name: ModuleScript +properties: + Source: + Type: String + Value: Hello there! +children: [] diff --git a/src/snapshot_middleware/snapshots/test__script_with_meta.snap b/src/snapshot_middleware/snapshots/test__script_with_meta.snap new file mode 100644 index 00000000..ce267c6f --- /dev/null +++ b/src/snapshot_middleware/snapshots/test__script_with_meta.snap @@ -0,0 +1,18 @@ +--- +source: src/snapshot_middleware/lua.rs +expression: instance_snapshot +--- +snapshot_id: ~ +metadata: + ignore_unknown_instances: true + instigating_source: + Path: /foo.server.lua + relevant_paths: + - /foo.server.lua +name: foo +class_name: Script +properties: + Source: + Type: String + Value: Hello there! +children: []