mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-21 13:15:50 +00:00
Prototype JsonModelPlugin, untested
Also cleaned up all of the warnings in the other plugin code
This commit is contained in:
52
src/plugins/json_model_plugin.rs
Normal file
52
src/plugins/json_model_plugin.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use regex::Regex;
|
||||
use serde_json;
|
||||
|
||||
use core::Route;
|
||||
use plugin::{Plugin, PluginChain, TransformFileResult, RbxChangeResult, FileChangeResult};
|
||||
use rbx::{RbxItem, RbxValue};
|
||||
use vfs::VfsItem;
|
||||
|
||||
lazy_static! {
|
||||
static ref JSON_MODEL_PATTERN: Regex = Regex::new(r"^(.*?)\.model\.json$").unwrap();
|
||||
}
|
||||
|
||||
pub struct JsonModelPlugin;
|
||||
|
||||
impl JsonModelPlugin {
|
||||
pub fn new() -> JsonModelPlugin {
|
||||
JsonModelPlugin
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for JsonModelPlugin {
|
||||
fn transform_file(&self, _plugins: &PluginChain, vfs_item: &VfsItem) -> TransformFileResult {
|
||||
match vfs_item {
|
||||
&VfsItem::File { ref contents, ref name } => {
|
||||
let rbx_name = match JSON_MODEL_PATTERN.captures(name) {
|
||||
Some(captures) => captures.get(1).unwrap().as_str().to_string(),
|
||||
None => return TransformFileResult::Pass,
|
||||
};
|
||||
|
||||
let mut rbx_item: RbxItem = match serde_json::from_str(contents) {
|
||||
Ok(v) => v,
|
||||
Err(_) => return TransformFileResult::Pass, // This should be an error in the future
|
||||
};
|
||||
|
||||
rbx_item.properties.insert("Name".to_string(), RbxValue::String {
|
||||
value: rbx_name,
|
||||
});
|
||||
|
||||
TransformFileResult::Value(Some(rbx_item))
|
||||
},
|
||||
&VfsItem::Dir { .. } => TransformFileResult::Pass,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_file_change(&self, _route: &Route) -> FileChangeResult {
|
||||
FileChangeResult::Pass
|
||||
}
|
||||
|
||||
fn handle_rbx_change(&self, _route: &Route, _rbx_item: &RbxItem) -> RbxChangeResult {
|
||||
RbxChangeResult::Pass
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user