merge impl-v2: server

This commit is contained in:
Lucien Greathouse 2018-06-10 22:59:04 -07:00
parent e30545c132
commit ec1f9bd706
35 changed files with 1643 additions and 1207 deletions

View file

@ -1,38 +1,116 @@
use std::borrow::Cow;
use std::collections::HashMap;
/// Represents data about a Roblox instance
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
use id::Id;
// TODO: Switch to enum to represent more value types
pub type RbxValue = String;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RbxInstance {
/// Maps to the `Name` property on Instance.
pub name: String,
/// Maps to the `ClassName` property on Instance.
pub class_name: String,
#[serde(default = "Vec::new")]
pub children: Vec<RbxInstance>,
#[serde(default = "HashMap::new")]
/// Contains all other properties of an Instance.
pub properties: HashMap<String, RbxValue>,
/// The route that this instance was generated from, if there was one.
pub route: Option<Vec<String>>,
/// All of the children of this instance. Order is relevant to preserve!
pub children: Vec<Id>,
pub parent: Option<Id>,
}
/// Any kind value that can be used by Roblox
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase", tag = "Type")]
pub enum RbxValue {
#[serde(rename_all = "PascalCase")]
String {
value: String,
},
#[serde(rename_all = "PascalCase")]
Bool {
value: bool,
},
#[serde(rename_all = "PascalCase")]
Number {
value: f64,
},
// TODO: Compound types like Vector3
// This seems like a really bad idea?
// Why isn't there a blanket impl for this for all T?
impl<'a> From<&'a RbxInstance> for Cow<'a, RbxInstance> {
fn from(instance: &'a RbxInstance) -> Cow<'a, RbxInstance> {
Cow::Borrowed(instance)
}
}
pub struct RbxTree {
instances: HashMap<Id, RbxInstance>,
}
impl RbxTree {
pub fn new() -> RbxTree {
RbxTree {
instances: HashMap::new(),
}
}
pub fn get_all_instances(&self) -> &HashMap<Id, RbxInstance> {
&self.instances
}
pub fn insert_instance(&mut self, id: Id, instance: RbxInstance) {
if let Some(parent_id) = instance.parent {
if let Some(mut parent) = self.instances.get_mut(&parent_id) {
if !parent.children.contains(&id) {
parent.children.push(id);
}
}
}
self.instances.insert(id, instance);
}
pub fn delete_instance(&mut self, id: Id) -> Vec<Id> {
let mut ids_to_visit = vec![id];
let mut ids_deleted = Vec::new();
for instance in self.instances.values_mut() {
match instance.children.iter().position(|&v| v == id) {
Some(index) => {
instance.children.remove(index);
},
None => {},
}
}
loop {
let id = match ids_to_visit.pop() {
Some(id) => id,
None => break,
};
match self.instances.get(&id) {
Some(instance) => ids_to_visit.extend_from_slice(&instance.children),
None => continue,
}
self.instances.remove(&id);
ids_deleted.push(id);
}
ids_deleted
}
pub fn get_instance<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap<Id, T>)
where T: From<&'a RbxInstance>
{
let mut ids_to_visit = vec![id];
loop {
let id = match ids_to_visit.pop() {
Some(id) => id,
None => break,
};
match self.instances.get(&id) {
Some(instance) => {
output.insert(id, instance.into());
for child_id in &instance.children {
ids_to_visit.push(*child_id);
}
},
None => continue,
}
}
}
}