/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::collections::BTreeMap; use webrender_api::debugger::DebuggerTextureContent; use crate::net; // Types for defining debug commands (and queries) that can be run in CLI or GUI mode pub struct CommandContext<'a> { pub net: &'a mut net::HttpConnection, args: BTreeMap, } impl<'a> CommandContext<'a> { pub fn new( args: BTreeMap, net: &'a mut net::HttpConnection, ) -> Self { CommandContext { args, net, } } #[allow(dead_code)] pub fn arg_string( &self, key: &str, ) -> &str { self.args[key].as_str() } } #[derive(Debug)] pub enum CommandOutput { Log(String), Err(String), TextDocument { title: String, content: String, }, SerdeDocument { kind: String, content: String, }, Textures(Vec), } pub struct ParamDescriptor { pub name: &'static str, pub is_required: bool, } pub struct CommandDescriptor { pub name: &'static str, pub alias: Option<&'static str>, pub help: &'static str, pub params: &'static [ParamDescriptor], } impl Default for CommandDescriptor { fn default() -> Self { CommandDescriptor { name: "", alias: None, help: "", params: &[], } } } pub trait Command { fn descriptor(&self) -> CommandDescriptor; fn run( &mut self, ctx: &mut CommandContext, ) -> CommandOutput; } pub struct CommandList { commands: Vec>, } impl CommandList { pub fn new() -> Self { CommandList { commands: Vec::new(), } } pub fn register_command( &mut self, cmd: Box, ) { assert!(!cmd.descriptor().name.is_empty(), "Invalid cmd name"); self.commands.push(cmd); } pub fn cmds(&self) -> &[Box] { &self.commands } pub fn get_mut<'a>(&mut self, name: &'a str) -> Option<&mut Box> { self.commands .iter_mut() .find(|cmd| { let desc = cmd.descriptor(); desc.name == name || desc.alias == Some(name) }) } }