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 {
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
.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()
}
}

View File

@@ -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)?;

View File

@@ -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)?;

View File

@@ -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<F: VfsFetcher>(vfs: &Vfs<F>, 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)?;

View File

@@ -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))
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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)?;