Add builder-ish methods to InstanceSnapshot to make middleware code more readable

This commit is contained in:
Lucien Greathouse
2019-11-19 14:02:57 -08:00
parent 0fbeb70865
commit c3f6a17bae
8 changed files with 115 additions and 87 deletions

View File

@@ -36,7 +36,53 @@ pub struct InstanceSnapshot {
} }
impl 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<String>) -> Self {
Self {
name: Cow::Owned(name.into()),
..self
}
}
pub fn class_name(self, class_name: impl Into<String>) -> Self {
Self {
class_name: Cow::Owned(class_name.into()),
..self
}
}
pub fn properties(self, properties: impl Into<HashMap<String, RbxValue>>) -> Self {
Self {
properties: properties.into(),
..self
}
}
pub fn children(self, children: impl Into<Vec<Self>>) -> Self {
Self {
children: children.into(),
..self
}
}
pub fn metadata(self, metadata: impl Into<InstanceMetadata>) -> Self {
Self {
metadata: metadata.into(),
..self
}
}
pub fn from_tree(tree: &RbxTree, id: RbxId) -> Self {
let instance = tree let instance = tree
.get_instance(id) .get_instance(id)
.expect("instance did not exist in tree"); .expect("instance did not exist in tree");
@@ -45,10 +91,10 @@ impl InstanceSnapshot {
.get_children_ids() .get_children_ids()
.iter() .iter()
.cloned() .cloned()
.map(|id| InstanceSnapshot::from_tree(tree, id)) .map(|id| Self::from_tree(tree, id))
.collect(); .collect();
InstanceSnapshot { Self {
snapshot_id: Some(id), snapshot_id: Some(id),
metadata: InstanceMetadata::default(), metadata: InstanceMetadata::default(),
name: Cow::Owned(instance.name.clone()), name: Cow::Owned(instance.name.clone()),
@@ -60,14 +106,7 @@ impl InstanceSnapshot {
} }
impl Default for InstanceSnapshot { impl Default for InstanceSnapshot {
fn default() -> InstanceSnapshot { fn default() -> Self {
InstanceSnapshot { Self::new()
snapshot_id: None,
metadata: InstanceMetadata::default(),
name: Cow::Borrowed("DEFAULT"),
class_name: Cow::Borrowed("DEFAULT"),
properties: HashMap::new(),
children: Vec::new(),
}
} }
} }

View File

@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::BTreeMap}; use std::collections::BTreeMap;
use maplit::hashmap; use maplit::hashmap;
use rbx_dom_weak::RbxValue; use rbx_dom_weak::RbxValue;
@@ -39,22 +39,19 @@ impl SnapshotMiddleware for SnapshotCsv {
let table_contents = convert_localization_csv(&entry.contents(vfs)?); let table_contents = convert_localization_csv(&entry.contents(vfs)?);
let mut snapshot = InstanceSnapshot { let mut snapshot = InstanceSnapshot::new()
snapshot_id: None, .name(instance_name)
metadata: InstanceMetadata { .class_name("LocalizationTable")
instigating_source: Some(entry.path().to_path_buf().into()), .properties(hashmap! {
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! {
"Contents".to_owned() => RbxValue::String { "Contents".to_owned() => RbxValue::String {
value: table_contents, 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()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap}; use std::collections::HashMap;
use rbx_dom_weak::{RbxId, RbxTree}; use rbx_dom_weak::{RbxId, RbxTree};
@@ -58,18 +58,15 @@ impl SnapshotMiddleware for SnapshotDir {
entry.path().join("init.client.lua"), entry.path().join("init.client.lua"),
]; ];
let mut snapshot = InstanceSnapshot { let mut snapshot = InstanceSnapshot::new()
snapshot_id: None, .name(instance_name)
metadata: InstanceMetadata { .class_name("Folder")
.children(snapshot_children)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()), instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths, relevant_paths,
..Default::default() ..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()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -1,4 +1,4 @@
use std::{borrow::Cow, str}; use std::str;
use maplit::hashmap; use maplit::hashmap;
use rbx_dom_weak::RbxValue; use rbx_dom_weak::RbxValue;
@@ -75,30 +75,23 @@ fn snapshot_lua_file<F: VfsFetcher>(vfs: &Vfs<F>, entry: &VfsEntry) -> SnapshotI
.expect("File content was not valid UTF-8") .expect("File content was not valid UTF-8")
.to_string(); .to_string();
let properties = hashmap! {
"Source".to_owned() => RbxValue::String {
value: contents_str,
},
};
let meta_path = entry let meta_path = entry
.path() .path()
.with_file_name(format!("{}.meta.json", instance_name)); .with_file_name(format!("{}.meta.json", instance_name));
let metadata = InstanceMetadata { let mut snapshot = InstanceSnapshot::new()
instigating_source: Some(entry.path().to_path_buf().into()), .name(instance_name)
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], .class_name(class_name)
..Default::default() .properties(hashmap! {
}; "Source".to_owned() => RbxValue::String {
value: contents_str,
let mut snapshot = InstanceSnapshot { },
snapshot_id: None, })
metadata, .metadata(InstanceMetadata {
name: Cow::Owned(instance_name.to_owned()), instigating_source: Some(entry.path().to_path_buf().into()),
class_name: Cow::Borrowed(class_name), relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
properties, ..Default::default()
children: Vec::new(), });
};
if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; let meta_contents = meta_entry.contents(vfs)?;

View File

@@ -1,7 +1,5 @@
use std::borrow::Cow;
use crate::{ use crate::{
snapshot::InstanceSnapshot, snapshot::{InstanceMetadata, InstanceSnapshot},
vfs::{Vfs, VfsEntry, VfsFetcher}, vfs::{Vfs, VfsEntry, VfsFetcher},
}; };
@@ -36,10 +34,13 @@ impl SnapshotMiddleware for SnapshotRbxlx {
let root_id = temp_tree.get_root_id(); let root_id = temp_tree.get_root_id();
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id); let snapshot = InstanceSnapshot::from_tree(&temp_tree, root_id)
snapshot.name = Cow::Owned(instance_name.to_owned()); .name(instance_name)
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); .metadata(InstanceMetadata {
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
Ok(Some(snapshot)) Ok(Some(snapshot))
} }

View File

@@ -1,9 +1,9 @@
use std::{borrow::Cow, collections::HashMap}; use std::collections::HashMap;
use rbx_dom_weak::{RbxInstanceProperties, RbxTree}; use rbx_dom_weak::{RbxInstanceProperties, RbxTree};
use crate::{ use crate::{
snapshot::InstanceSnapshot, snapshot::{InstanceMetadata, InstanceSnapshot},
vfs::{Vfs, VfsEntry, VfsFetcher}, vfs::{Vfs, VfsEntry, VfsFetcher},
}; };
@@ -44,10 +44,13 @@ impl SnapshotMiddleware for SnapshotRbxm {
let children = root_instance.get_children_ids(); let children = root_instance.get_children_ids();
if children.len() == 1 { if children.len() == 1 {
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
snapshot.name = Cow::Owned(instance_name.to_owned()); .name(instance_name)
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); .metadata(InstanceMetadata {
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
Ok(Some(snapshot)) Ok(Some(snapshot))
} else { } else {

View File

@@ -1,7 +1,5 @@
use std::borrow::Cow;
use crate::{ use crate::{
snapshot::InstanceSnapshot, snapshot::{InstanceMetadata, InstanceSnapshot},
vfs::{Vfs, VfsEntry, VfsFetcher}, vfs::{Vfs, VfsEntry, VfsFetcher},
}; };
@@ -38,10 +36,13 @@ impl SnapshotMiddleware for SnapshotRbxmx {
let children = root_instance.get_children_ids(); let children = root_instance.get_children_ids();
if children.len() == 1 { if children.len() == 1 {
let mut snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0]); let snapshot = InstanceSnapshot::from_tree(&temp_tree, children[0])
snapshot.name = Cow::Owned(instance_name.to_owned()); .name(instance_name)
snapshot.metadata.instigating_source = Some(entry.path().to_path_buf().into()); .metadata(InstanceMetadata {
snapshot.metadata.relevant_paths = vec![entry.path().to_path_buf()]; instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf()],
..Default::default()
});
Ok(Some(snapshot)) Ok(Some(snapshot))
} else { } else {

View File

@@ -1,4 +1,4 @@
use std::{borrow::Cow, str}; use std::str;
use maplit::hashmap; use maplit::hashmap;
use rbx_dom_weak::{RbxId, RbxTree, RbxValue}; use rbx_dom_weak::{RbxId, RbxTree, RbxValue};
@@ -48,18 +48,15 @@ impl SnapshotMiddleware for SnapshotTxt {
.path() .path()
.with_file_name(format!("{}.meta.json", instance_name)); .with_file_name(format!("{}.meta.json", instance_name));
let mut snapshot = InstanceSnapshot { let mut snapshot = InstanceSnapshot::new()
snapshot_id: None, .name(instance_name)
metadata: InstanceMetadata { .class_name("StringValue")
.properties(properties)
.metadata(InstanceMetadata {
instigating_source: Some(entry.path().to_path_buf().into()), instigating_source: Some(entry.path().to_path_buf().into()),
relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()], relevant_paths: vec![entry.path().to_path_buf(), meta_path.clone()],
..Default::default() ..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()? { if let Some(meta_entry) = vfs.get(meta_path).with_not_found()? {
let meta_contents = meta_entry.contents(vfs)?; let meta_contents = meta_entry.contents(vfs)?;