diff --git a/src/snapshot/instance_snapshot.rs b/src/snapshot/instance_snapshot.rs index dcd50fe9..aae249a8 100644 --- a/src/snapshot/instance_snapshot.rs +++ b/src/snapshot/instance_snapshot.rs @@ -36,7 +36,53 @@ pub struct InstanceSnapshot { } impl InstanceSnapshot { - pub fn from_tree(tree: &RbxTree, id: RbxId) -> InstanceSnapshot { + pub fn new() -> Self { + Self { + snapshot_id: None, + metadata: InstanceMetadata::default(), + name: Cow::Borrowed("DEFAULT"), + class_name: Cow::Borrowed("DEFAULT"), + properties: HashMap::new(), + children: Vec::new(), + } + } + + pub fn name(self, name: impl Into) -> Self { + Self { + name: Cow::Owned(name.into()), + ..self + } + } + + pub fn class_name(self, class_name: impl Into) -> Self { + Self { + class_name: Cow::Owned(class_name.into()), + ..self + } + } + + pub fn properties(self, properties: impl Into>) -> Self { + Self { + properties: properties.into(), + ..self + } + } + + pub fn children(self, children: impl Into>) -> Self { + Self { + children: children.into(), + ..self + } + } + + pub fn metadata(self, metadata: impl Into) -> Self { + Self { + metadata: metadata.into(), + ..self + } + } + + pub fn from_tree(tree: &RbxTree, id: RbxId) -> Self { let instance = tree .get_instance(id) .expect("instance did not exist in tree"); @@ -45,10 +91,10 @@ impl InstanceSnapshot { .get_children_ids() .iter() .cloned() - .map(|id| InstanceSnapshot::from_tree(tree, id)) + .map(|id| Self::from_tree(tree, id)) .collect(); - InstanceSnapshot { + Self { snapshot_id: Some(id), metadata: InstanceMetadata::default(), name: Cow::Owned(instance.name.clone()), @@ -60,14 +106,7 @@ impl InstanceSnapshot { } impl Default for InstanceSnapshot { - fn default() -> InstanceSnapshot { - InstanceSnapshot { - snapshot_id: None, - metadata: InstanceMetadata::default(), - name: Cow::Borrowed("DEFAULT"), - class_name: Cow::Borrowed("DEFAULT"), - properties: HashMap::new(), - children: Vec::new(), - } + fn default() -> Self { + Self::new() } } diff --git a/src/snapshot_middleware/csv.rs b/src/snapshot_middleware/csv.rs index e74a30ad..5838602f 100644 --- a/src/snapshot_middleware/csv.rs +++ b/src/snapshot_middleware/csv.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, collections::BTreeMap}; +use std::collections::BTreeMap; use maplit::hashmap; use rbx_dom_weak::RbxValue; @@ -39,22 +39,19 @@ impl SnapshotMiddleware for SnapshotCsv { let table_contents = convert_localization_csv(&entry.contents(vfs)?); - let mut snapshot = InstanceSnapshot { - snapshot_id: None, - metadata: InstanceMetadata { - instigating_source: Some(entry.path().to_path_buf().into()), - relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], - ..Default::default() - }, - name: Cow::Owned(instance_name.to_owned()), - class_name: Cow::Borrowed("LocalizationTable"), - properties: hashmap! { + let mut snapshot = InstanceSnapshot::new() + .name(instance_name) + .class_name("LocalizationTable") + .properties(hashmap! { "Contents".to_owned() => RbxValue::String { value: table_contents, }, - }, - children: Vec::new(), - }; + }) + .metadata(InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], + ..Default::default() + }); if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; diff --git a/src/snapshot_middleware/dir.rs b/src/snapshot_middleware/dir.rs index eefff70c..9a93affc 100644 --- a/src/snapshot_middleware/dir.rs +++ b/src/snapshot_middleware/dir.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, collections::HashMap}; +use std::collections::HashMap; use rbx_dom_weak::{RbxId, RbxTree}; @@ -58,18 +58,15 @@ impl SnapshotMiddleware for SnapshotDir { entry.path().join("init.client.lua"), ]; - let mut snapshot = InstanceSnapshot { - snapshot_id: None, - metadata: InstanceMetadata { + let mut snapshot = InstanceSnapshot::new() + .name(instance_name) + .class_name("Folder") + .children(snapshot_children) + .metadata(InstanceMetadata { instigating_source: Some(entry.path().to_path_buf().into()), relevant_paths, ..Default::default() - }, - name: Cow::Owned(instance_name), - class_name: Cow::Borrowed("Folder"), - properties: HashMap::new(), - children: snapshot_children, - }; + }); if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index d632c941..0b096018 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, str}; +use std::str; use maplit::hashmap; use rbx_dom_weak::RbxValue; @@ -75,30 +75,23 @@ fn snapshot_lua_file(vfs: &Vfs, entry: &VfsEntry) -> SnapshotI .expect("File content was not valid UTF-8") .to_string(); - let properties = hashmap! { - "Source".to_owned() => RbxValue::String { - value: contents_str, - }, - }; - let meta_path = entry .path() .with_file_name(format!("{}.meta.json", instance_name)); - let metadata = InstanceMetadata { - instigating_source: Some(entry.path().to_path_buf().into()), - relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], - ..Default::default() - }; - - let mut snapshot = InstanceSnapshot { - snapshot_id: None, - metadata, - name: Cow::Owned(instance_name.to_owned()), - class_name: Cow::Borrowed(class_name), - properties, - children: Vec::new(), - }; + let mut snapshot = InstanceSnapshot::new() + .name(instance_name) + .class_name(class_name) + .properties(hashmap! { + "Source".to_owned() => RbxValue::String { + value: contents_str, + }, + }) + .metadata(InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], + ..Default::default() + }); if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?; diff --git a/src/snapshot_middleware/rbxlx.rs b/src/snapshot_middleware/rbxlx.rs index ed4d578c..59753ccf 100644 --- a/src/snapshot_middleware/rbxlx.rs +++ b/src/snapshot_middleware/rbxlx.rs @@ -1,7 +1,5 @@ -use std::borrow::Cow; - use crate::{ - snapshot::InstanceSnapshot, + snapshot::{InstanceMetadata, InstanceSnapshot}, vfs::{Vfs, VfsEntry, VfsFetcher}, }; @@ -36,10 +34,13 @@ impl SnapshotMiddleware for SnapshotRbxlx { let root_id = temp_tree.get_root_id(); - let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id); - snapshot.name = Cow::Owned(instance_name.to_owned()); - snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); - snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; + let snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id) + .name(instance_name) + .metadata(InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf()], + ..Default::default() + }); Ok(Some(snapshot)) } diff --git a/src/snapshot_middleware/rbxm.rs b/src/snapshot_middleware/rbxm.rs index 825e9036..41d120b3 100644 --- a/src/snapshot_middleware/rbxm.rs +++ b/src/snapshot_middleware/rbxm.rs @@ -1,9 +1,9 @@ -use std::{borrow::Cow, collections::HashMap}; +use std::collections::HashMap; use rbx_dom_weak::{RbxInstanceProperties, RbxTree}; use crate::{ - snapshot::InstanceSnapshot, + snapshot::{InstanceMetadata, InstanceSnapshot}, vfs::{Vfs, VfsEntry, VfsFetcher}, }; @@ -44,10 +44,13 @@ impl SnapshotMiddleware for SnapshotRbxm { let children = root_instance.get_children_ids(); if children.len() == 1 { - let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); - snapshot.name = Cow::Owned(instance_name.to_owned()); - snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); - snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; + let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]) + .name(instance_name) + .metadata(InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf()], + ..Default::default() + }); Ok(Some(snapshot)) } else { diff --git a/src/snapshot_middleware/rbxmx.rs b/src/snapshot_middleware/rbxmx.rs index 8d0faef3..5ff41ce0 100644 --- a/src/snapshot_middleware/rbxmx.rs +++ b/src/snapshot_middleware/rbxmx.rs @@ -1,7 +1,5 @@ -use std::borrow::Cow; - use crate::{ - snapshot::InstanceSnapshot, + snapshot::{InstanceMetadata, InstanceSnapshot}, vfs::{Vfs, VfsEntry, VfsFetcher}, }; @@ -38,10 +36,13 @@ impl SnapshotMiddleware for SnapshotRbxmx { let children = root_instance.get_children_ids(); if children.len() == 1 { - let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); - snapshot.name = Cow::Owned(instance_name.to_owned()); - snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); - snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; + let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]) + .name(instance_name) + .metadata(InstanceMetadata { + instigating_source: Some(entry.path().to_path_buf().into()), + relevant_paths: vec![entry.path().to_path_buf()], + ..Default::default() + }); Ok(Some(snapshot)) } else { diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index bf1b55d0..77208168 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, str}; +use std::str; use maplit::hashmap; use rbx_dom_weak::{RbxId, RbxTree, RbxValue}; @@ -48,18 +48,15 @@ impl SnapshotMiddleware for SnapshotTxt { .path() .with_file_name(format!("{}.meta.json", instance_name)); - let mut snapshot = InstanceSnapshot { - snapshot_id: None, - metadata: InstanceMetadata { + let mut snapshot = InstanceSnapshot::new() + .name(instance_name) + .class_name("StringValue") + .properties(properties) + .metadata(InstanceMetadata { instigating_source: Some(entry.path().to_path_buf().into()), relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], ..Default::default() - }, - name: Cow::Owned(instance_name.to_owned()), - class_name: Cow::Borrowed("StringValue"), - properties, - children: Vec::new(), - }; + }); if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { let meta_contents = meta_entry.contents(vfs)?;