diff --git a/src/snapshot/instance_snapshot.rs b/src/snapshot/instance_snapshot.rs index 6adeb5b2..8182ad5f 100644 --- a/src/snapshot/instance_snapshot.rs +++ b/src/snapshot/instance_snapshot.rs @@ -1,25 +1,32 @@ //! Defines the structure of an instance snapshot. -use std::{borrow::Cow, collections::HashMap}; +use std::{borrow::Cow, collections::HashMap, path::PathBuf}; use rbx_dom_weak::{RbxId, RbxTree, RbxValue}; +use crate::project::ProjectNode; + /// A lightweight description of what an instance should look like. Attempts to /// be somewhat memory efficient by borrowing from its source data, indicated by -/// the lifetime parameter, `'source`. +/// the lifetime parameter `'source`. /// // Possible future improvements: // - Use refcounted/interned strings // - Replace use of RbxValue with a sum of RbxValue + borrowed value #[derive(Debug, Clone, PartialEq)] pub struct InstanceSnapshot<'source> { + /// A temporary ID applied to the snapshot that's used for Ref properties. pub snapshot_id: Option, + /// A complete view of where this snapshot came from. It should contain + /// enough information, if not None, to recreate this snapshot + /// deterministically assuming the source has not changed state. + pub source: Option, + pub name: Cow<'source, str>, pub class_name: Cow<'source, str>, pub properties: HashMap, pub children: Vec>, - // TODO: Snapshot source, like a file or a project node? } impl<'source> InstanceSnapshot<'source> { @@ -32,6 +39,7 @@ impl<'source> InstanceSnapshot<'source> { InstanceSnapshot { snapshot_id: None, + source: self.source.clone(), name: Cow::Owned(self.name.clone().into_owned()), class_name: Cow::Owned(self.class_name.clone().into_owned()), properties: self.properties.clone(), @@ -53,6 +61,7 @@ impl<'source> InstanceSnapshot<'source> { InstanceSnapshot { snapshot_id: Some(id), + source: None, name: Cow::Owned(instance.name.clone()), class_name: Cow::Owned(instance.class_name.clone()), properties: instance.properties.clone(), @@ -60,3 +69,15 @@ impl<'source> InstanceSnapshot<'source> { } } } + +#[derive(Debug, Clone, PartialEq)] +pub enum SnapshotSource { + File { + path: PathBuf, + }, + ProjectFile { + path: PathBuf, + name: String, + node: ProjectNode, + }, +} diff --git a/src/snapshot/patch_apply.rs b/src/snapshot/patch_apply.rs index 246fdd02..dfd5c523 100644 --- a/src/snapshot/patch_apply.rs +++ b/src/snapshot/patch_apply.rs @@ -159,6 +159,7 @@ mod test { let snapshot = InstanceSnapshot { snapshot_id: None, + source: None, name: Cow::Borrowed("Foo"), class_name: Cow::Borrowed("Bar"), properties: hashmap! { diff --git a/src/snapshot/patch_compute.rs b/src/snapshot/patch_compute.rs index 451736ae..83acbed3 100644 --- a/src/snapshot/patch_compute.rs +++ b/src/snapshot/patch_compute.rs @@ -244,6 +244,7 @@ mod test { } }, + source: None, name: Cow::Borrowed("foo"), class_name: Cow::Borrowed("foo"), children: Vec::new(), @@ -294,11 +295,13 @@ mod test { }, snapshot_id: None, + source: None, name: Cow::Borrowed("child"), class_name: Cow::Borrowed("child"), children: Vec::new(), }], + source: None, properties: HashMap::new(), name: Cow::Borrowed("foo"), class_name: Cow::Borrowed("foo"), @@ -311,6 +314,7 @@ mod test { parent_id: root_id, instance: InstanceSnapshot { snapshot_id: None, + source: None, properties: hashmap! { "Self".to_owned() => RbxValue::Ref { value: Some(root_id), diff --git a/src/snapshot_middleware/csv.rs b/src/snapshot_middleware/csv.rs index fdf532af..4be88d2b 100644 --- a/src/snapshot_middleware/csv.rs +++ b/src/snapshot_middleware/csv.rs @@ -39,6 +39,7 @@ impl SnapshotMiddleware for SnapshotCsv { Ok(Some(InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Owned(instance_name), class_name: Cow::Borrowed("LocalizationTable"), properties: hashmap! { diff --git a/src/snapshot_middleware/dir.rs b/src/snapshot_middleware/dir.rs index 1d7de937..2d6b60ef 100644 --- a/src/snapshot_middleware/dir.rs +++ b/src/snapshot_middleware/dir.rs @@ -43,6 +43,7 @@ impl SnapshotMiddleware for SnapshotDir { Ok(Some(InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Owned(instance_name), class_name: Cow::Borrowed("Folder"), properties: HashMap::new(), diff --git a/src/snapshot_middleware/json_model.rs b/src/snapshot_middleware/json_model.rs index 4d4834f7..ec5148a8 100644 --- a/src/snapshot_middleware/json_model.rs +++ b/src/snapshot_middleware/json_model.rs @@ -120,6 +120,7 @@ impl JsonModelCore { InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Owned(name), class_name: Cow::Owned(class_name), properties, @@ -169,6 +170,7 @@ mod test { instance_snapshot, InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Borrowed("foo"), class_name: Cow::Borrowed("IntValue"), properties: hashmap! { @@ -178,6 +180,7 @@ mod test { }, children: vec![InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Borrowed("The Child"), class_name: Cow::Borrowed("StringValue"), properties: HashMap::new(), diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index 8ae12323..0aa61080 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -90,6 +90,7 @@ fn snapshot_lua_file( Ok(Some(InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Owned(instance_name.to_owned()), class_name: Cow::Borrowed(class_name), properties, diff --git a/src/snapshot_middleware/project.rs b/src/snapshot_middleware/project.rs index ceaa4d7b..2d431978 100644 --- a/src/snapshot_middleware/project.rs +++ b/src/snapshot_middleware/project.rs @@ -131,6 +131,7 @@ fn snapshot_project_node( Ok(Some(InstanceSnapshot { snapshot_id: None, + source: None, // TODO name, class_name, properties, diff --git a/src/snapshot_middleware/rbxm.rs b/src/snapshot_middleware/rbxm.rs index 156aa052..47e8ab29 100644 --- a/src/snapshot_middleware/rbxm.rs +++ b/src/snapshot_middleware/rbxm.rs @@ -49,6 +49,7 @@ impl SnapshotMiddleware for SnapshotRbxm { if children.len() == 1 { let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); snapshot.name = Cow::Owned(instance_name); + // TODO: Assign snapshot.source Ok(Some(snapshot)) } else { diff --git a/src/snapshot_middleware/rbxmx.rs b/src/snapshot_middleware/rbxmx.rs index 6cfc9b1a..6d0aa78b 100644 --- a/src/snapshot_middleware/rbxmx.rs +++ b/src/snapshot_middleware/rbxmx.rs @@ -45,6 +45,7 @@ impl SnapshotMiddleware for SnapshotRbxmx { if children.len() == 1 { let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); snapshot.name = Cow::Owned(instance_name); + // TODO: Assign snapshot.source Ok(Some(snapshot)) } else { diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index 22d7cd83..058ae408 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -51,6 +51,7 @@ impl SnapshotMiddleware for SnapshotTxt { Ok(Some(InstanceSnapshot { snapshot_id: None, + source: None, // TODO name: Cow::Owned(instance_name), class_name: Cow::Borrowed("StringValue"), properties,