From 93349ae2dc69a808b630a5d255ec717764578c67 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 26 Feb 2019 22:51:21 -0800 Subject: [PATCH] Use rbx_reflection to allow type inference on projects (#130) * Start dependency on rbx_reflection * Alive and working, all tests pass * Update CHANGELOG --- CHANGELOG.md | 1 + Cargo.lock | 22 ++++++++++++++----- server/Cargo.toml | 3 ++- server/src/project.rs | 8 +++---- server/src/rbx_snapshot.rs | 18 ++++++++++++++- server/tests/read_projects.rs | 2 +- .../default.project.json | 5 +---- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2dff637..9de2f88e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Types like `Color3` and `CFrame` can now be updated live! * Fixed plugin assets flashing in on first load ([#121](https://github.com/LPGhatguy/rojo/issues/121)) * Changed Rojo's HTTP server from Rouille to Hyper, which reduced the release size by around a megabyte. +* Added property type inference to projects, which makes specifying services a lot easier ([#130](https://github.com/LPGhatguy/rojo/pull/130)) ## [0.5.0 Alpha 4](https://github.com/LPGhatguy/rojo/releases/tag/v0.5.0-alpha.4) (February 8, 2019) * Added support for nested partitions ([#102](https://github.com/LPGhatguy/rojo/issues/102)) diff --git a/Cargo.lock b/Cargo.lock index f3cc9caf..f55103cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1029,12 +1029,12 @@ dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lz4 1.23.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rbx_dom_weak 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rbx_dom_weak 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rbx_dom_weak" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1042,6 +1042,16 @@ dependencies = [ "uuid 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rbx_reflection" +version = "1.0.373" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rbx_dom_weak 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rbx_xml" version = "0.3.0" @@ -1052,7 +1062,7 @@ dependencies = [ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rbx_dom_weak 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rbx_dom_weak 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1185,7 +1195,8 @@ dependencies = [ "pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rbx_binary 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rbx_dom_weak 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rbx_dom_weak 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rbx_reflection 1.0.373 (registry+https://github.com/rust-lang/crates.io-index)", "rbx_xml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1848,7 +1859,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_pcg 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "086bd09a33c7044e56bb44d5bdde5a60e7f119a9e95b0775f545de759a32fe05" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rbx_binary 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c4d3bffbec1f20f66684448e46ef45dad2fbf1185c3d8f3e29018f027484ffc" -"checksum rbx_dom_weak 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a67bbec623f2f12992dbf2e647deb3fe38613d4f874a8feeae566a931bebe63a" +"checksum rbx_dom_weak 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "673606053dba942f6c4607661c423424711a60cb40481f1e20c55acc21f53a38" +"checksum rbx_reflection 1.0.373 (registry+https://github.com/rust-lang/crates.io-index)" = "b37499dc2e097063be0eff716f54fb060ef3beb732e5f699b56feb6d9aa2442b" "checksum rbx_xml 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2b394e64979c52190e6462dd81d5b2258cdfb7e3891cefaff5d3c4af7cdc07b5" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" diff --git a/server/Cargo.toml b/server/Cargo.toml index 2ee9623c..2a1bfc71 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -31,8 +31,9 @@ maplit = "1.0.1" notify = "4.0" rand = "0.4" rbx_binary = "0.3.0" -rbx_dom_weak = "0.3.0" +rbx_dom_weak = "0.3.1" rbx_xml = "0.3.0" +rbx_reflection = "1.0.373" regex = "1.0" reqwest = "0.9.5" rlua = "0.16" diff --git a/server/src/project.rs b/server/src/project.rs index acefea5e..bfafc31d 100644 --- a/server/src/project.rs +++ b/server/src/project.rs @@ -9,7 +9,7 @@ use std::{ use log::warn; use failure::Fail; use maplit::hashmap; -use rbx_dom_weak::RbxValue; +use rbx_dom_weak::{UnresolvedRbxValue, RbxValue}; use serde_derive::{Serialize, Deserialize}; pub static PROJECT_FILENAME: &'static str = "default.project.json"; @@ -64,7 +64,7 @@ struct SourceProjectNode { class_name: Option, #[serde(rename = "$properties", default = "HashMap::new", skip_serializing_if = "HashMap::is_empty")] - properties: HashMap, + properties: HashMap, #[serde(rename = "$ignoreUnknownInstances", skip_serializing_if = "Option::is_none")] ignore_unknown_instances: Option, @@ -188,7 +188,7 @@ pub enum ProjectSaveError { pub struct ProjectNode { pub class_name: Option, pub children: HashMap, - pub properties: HashMap, + pub properties: HashMap, pub ignore_unknown_instances: Option, #[serde(serialize_with = "crate::path_serializer::serialize_option")] @@ -284,7 +284,7 @@ impl Project { properties: hashmap! { String::from("HttpEnabled") => RbxValue::Bool { value: true, - }, + }.into(), }, ..Default::default() }, diff --git a/server/src/rbx_snapshot.rs b/server/src/rbx_snapshot.rs index c3301918..5cbd0b3c 100644 --- a/server/src/rbx_snapshot.rs +++ b/server/src/rbx_snapshot.rs @@ -15,6 +15,7 @@ use log::info; use maplit::hashmap; use rbx_dom_weak::{RbxTree, RbxValue, RbxInstanceProperties}; use serde_derive::{Serialize, Deserialize}; +use rbx_reflection::{try_resolve_value, ValueResolveError}; use crate::{ imfs::{ @@ -119,6 +120,19 @@ pub enum SnapshotError { ProjectNodeInvalidTransmute { partition_path: PathBuf, }, + + PropertyResolveError { + #[fail(cause)] + inner: ValueResolveError, + }, +} + +impl From for SnapshotError { + fn from(inner: ValueResolveError) -> SnapshotError { + SnapshotError::PropertyResolveError { + inner, + } + } } impl fmt::Display for SnapshotError { @@ -147,6 +161,7 @@ impl fmt::Display for SnapshotError { writeln!(output, "")?; writeln!(output, "Partition target ($path): {}", partition_path.display()) }, + SnapshotError::PropertyResolveError { inner } => write!(output, "{}", inner), } } } @@ -226,7 +241,8 @@ pub fn snapshot_project_node<'source>( } for (key, value) in &node.properties { - snapshot.properties.insert(key.clone(), value.clone()); + let resolved_value = try_resolve_value(&snapshot.class_name, key, value)?; + snapshot.properties.insert(key.clone(), resolved_value); } if let Some(ignore_unknown_instances) = node.ignore_unknown_instances { diff --git a/server/tests/read_projects.rs b/server/tests/read_projects.rs index 38cf93b4..63195acf 100644 --- a/server/tests/read_projects.rs +++ b/server/tests/read_projects.rs @@ -65,7 +65,7 @@ fn single_partition_game() { let mut http_service_properties = HashMap::new(); http_service_properties.insert("HttpEnabled".to_string(), RbxValue::Bool { value: true, - }); + }.into()); let http_service = ProjectNode { class_name: Some(String::from("HttpService")), diff --git a/test-projects/single_partition_game/default.project.json b/test-projects/single_partition_game/default.project.json index 3fbff1ac..04222f69 100644 --- a/test-projects/single_partition_game/default.project.json +++ b/test-projects/single_partition_game/default.project.json @@ -11,10 +11,7 @@ "HttpService": { "$className": "HttpService", "$properties": { - "HttpEnabled": { - "Type": "Bool", - "Value": true - } + "HttpEnabled": true } } }