script_commands.rs (2459B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 use crate::command::{Command, CommandDescriptor, CommandList, ParamDescriptor}; 6 use crate::command::{CommandContext, CommandOutput}; 7 use crate::wrench; 8 9 use webrender::scene::Scene; 10 11 // Implementation of a basic set of script commands 12 13 // Register the script commands in this source file 14 pub fn register(cmd_list: &mut CommandList) { 15 cmd_list.register_command(Box::new(ProcessCaptureCommand)); 16 } 17 18 pub struct ProcessCaptureCommand; 19 20 impl Command for ProcessCaptureCommand { 21 fn descriptor(&self) -> CommandDescriptor { 22 CommandDescriptor { 23 name: "process-capture", 24 help: r#" 25 Process a WR capture directory. 26 USAGE: process-capture [scene file] [output file] 27 "#, 28 params: &[ 29 ParamDescriptor { 30 name: "scene", 31 is_required: true, 32 }, 33 ParamDescriptor { 34 name: "out", 35 is_required: true, 36 }, 37 ], 38 ..Default::default() 39 } 40 } 41 42 fn run( 43 &mut self, 44 ctx: &mut CommandContext, 45 ) -> CommandOutput { 46 use std::io::Write; 47 48 let source = ctx.arg_string("scene"); 49 let target = ctx.arg_string("out"); 50 51 println!("Loading scene file '{}'", source); 52 let scene_file = match std::fs::read_to_string(source) { 53 Ok(f) => f, 54 Err(..) => return CommandOutput::Err("\tUnable to read scene".into()), 55 }; 56 57 println!("Deserialize scene file '{}'", source); 58 let scene: Scene = match ron::de::from_str(&scene_file) { 59 Ok(out) => out, 60 Err(..) => return CommandOutput::Err("\tDeserialization failed".into()), 61 }; 62 63 let yaml = match wrench::scene_to_yaml(&scene) { 64 Ok(yaml) => yaml, 65 Err(err) => return CommandOutput::Err( 66 format!("\tFailed to convert - {}", err) 67 ), 68 }; 69 70 let mut output = match std::fs::File::create(target) { 71 Ok(f) => f, 72 Err(..) => return CommandOutput::Err("\tUnable to open output file".into()), 73 }; 74 write!(output, "{}", yaml).expect("failed to write yaml"); 75 76 CommandOutput::Log(yaml) 77 } 78 }