From dbd499701f4450d5a2e7d42ce9272ddd5d3e8092 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 11 Dec 2018 18:23:20 -0800 Subject: [PATCH] Snapshot tinkering, this is an idea --- server/src/lib.rs | 1 + server/src/rbx_snapshot.rs | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 server/src/rbx_snapshot.rs diff --git a/server/src/lib.rs b/server/src/lib.rs index 4945f343..fd3d600b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -15,6 +15,7 @@ pub mod message_queue; pub mod path_map; pub mod project; pub mod rbx_session; +pub mod rbx_snapshot; pub mod session; pub mod session_id; pub mod web; diff --git a/server/src/rbx_snapshot.rs b/server/src/rbx_snapshot.rs new file mode 100644 index 00000000..0f11f1ca --- /dev/null +++ b/server/src/rbx_snapshot.rs @@ -0,0 +1,60 @@ +use std::{ + str, + borrow::Cow, + collections::HashMap, +}; + +use rbx_tree::{RbxTree, RbxId}; + +use crate::{ + imfs::{Imfs, ImfsItem, ImfsFile, ImfsDirectory}, +}; + +pub struct RbxSnapshotInstance<'a> { + name: String, + class_name: String, + properties: HashMap>, + children: Vec>, +} + +pub enum RbxSnapshotValue<'a> { + String(Cow<'a, str>), +} + +pub fn reify(snapshot: RbxSnapshotInstance, tree: &mut RbxTree, parent_id: RbxId) { + unimplemented!() +} + +pub fn render<'a>(imfs: &'a Imfs, imfs_item: &'a ImfsItem) -> RbxSnapshotInstance<'a> { + match imfs_item { + ImfsItem::File(file) => { + let name = file.path.file_stem().unwrap().to_str().unwrap(); + let source = str::from_utf8(&file.contents).unwrap(); + let mut properties = HashMap::new(); + properties.insert("Source".to_string(), RbxSnapshotValue::String(Cow::Borrowed(source))); + + RbxSnapshotInstance { + name: name.to_string(), + class_name: "ModuleScript".to_string(), + properties, + children: Vec::new(), + } + }, + ImfsItem::Directory(directory) => { + let name = directory.path.file_name().unwrap().to_str().unwrap(); + let mut children = Vec::new(); + + for child_path in &directory.children { + let child_item = imfs.get(child_path).unwrap(); + children.push(render(imfs, child_item)); + } + + RbxSnapshotInstance { + name: name.to_string(), + class_name: "Folder".to_string(), + properties: HashMap::new(), + children, + } + }, + } +} \ No newline at end of file