Files
rojo/src/snapshot/instance_snapshot.rs
Lucien Greathouse 59ef5f05ea Upgrade to rbx_dom_weak 2.0 (#377)
* Mostly mechanical port bits

* Almost there

* It builds again!

* Turn on all the code again

* Tests compiling but not passing

* Stub work for value resolution

* Implement resolution minus enums and derived properties

* Implement property descriptor resolution

* Update referent snapshots

* Update unions test project

Using a place file instead of a model yields better
error messages in Roblox Studio.

* Add easy shortcut to testing with local rbx-dom

* Update rbx-dom

* Add enum resolution

* Update init.meta.json to use UnresolvedValue

* Expand value resolution support, add test

* Filter SharedString values from web API

* Add 'property' builder method to InstanceSnapshot

* Change InstanceSnapshot/InstanceBuilder boundary

* Fix remove_file crash

* rustfmt

* Update to latest rbx_dom_lua

* Update dependencies, including rbx_dom_weak

* Update to latest rbx-dom

* Update dependencies

* Update rbx-dom, fixing more bugs

* Remove experimental warning on binary place builds

* Remove unused imports
2021-02-18 20:56:09 -05:00

131 lines
3.4 KiB
Rust

//! Defines the structure of an instance snapshot.
use std::{borrow::Cow, collections::HashMap};
use rbx_dom_weak::{
types::{Ref, Variant},
WeakDom,
};
use serde::{Deserialize, Serialize};
use super::InstanceMetadata;
/// A lightweight description of what an instance should look like.
///
// Possible future improvements:
// - Use refcounted/interned strings
// - Replace use of Variant with a sum of Variant + borrowed value
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InstanceSnapshot {
// FIXME: Don't use Option<Ref> anymore!
/// A temporary ID applied to the snapshot that's used for Ref properties.
pub snapshot_id: Option<Ref>,
/// Rojo-specific metadata associated with the instance.
pub metadata: InstanceMetadata,
/// Correpsonds to the Name property of the instance.
pub name: Cow<'static, str>,
/// Corresponds to the ClassName property of the instance.
pub class_name: Cow<'static, str>,
/// All other properties of the instance, weakly-typed.
pub properties: HashMap<String, Variant>,
/// The children of the instance represented as more snapshots.
///
/// Order is relevant for Roblox instances!
pub children: Vec<InstanceSnapshot>,
}
impl 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 property<K, V>(mut self, key: K, value: V) -> Self
where
K: Into<String>,
V: Into<Variant>,
{
self.properties.insert(key.into(), value.into());
self
}
pub fn properties(self, properties: impl Into<HashMap<String, Variant>>) -> Self {
Self {
properties: properties.into(),
..self
}
}
pub fn children(self, children: impl Into<Vec<Self>>) -> Self {
Self {
children: children.into(),
..self
}
}
pub fn snapshot_id(self, snapshot_id: Option<Ref>) -> Self {
Self {
snapshot_id,
..self
}
}
pub fn metadata(self, metadata: impl Into<InstanceMetadata>) -> Self {
Self {
metadata: metadata.into(),
..self
}
}
pub fn from_tree(tree: &WeakDom, id: Ref) -> Self {
let instance = tree.get_by_ref(id).expect("instance did not exist in tree");
let children = instance
.children()
.iter()
.copied()
.map(|id| Self::from_tree(tree, id))
.collect();
Self {
snapshot_id: Some(id),
metadata: InstanceMetadata::default(),
name: Cow::Owned(instance.name.clone()),
class_name: Cow::Owned(instance.class.clone()),
properties: instance.properties.clone(),
children,
}
}
}
impl Default for InstanceSnapshot {
fn default() -> Self {
Self::new()
}
}