use std::fmt;
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub enum BreakpointSpec {
All,
Numeric(usize),
Output((usize, String)),
Input((usize, usize)),
Block((Option<usize>, Option<usize>)),
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub enum DebugCommand {
Ack,
Breakpoint(Option<BreakpointSpec>),
Continue,
DebugClientStarting,
Delete(Option<BreakpointSpec>),
Error(String),
ExitDebugger,
FunctionList,
InspectFunction(usize),
Inspect,
InspectInput(usize, usize),
InspectOutput(usize, String),
InspectBlock(Option<usize>, Option<usize>),
Invalid,
List,
Modify(Option<Vec<String>>),
RunReset,
Step(Option<usize>),
Validate,
}
impl fmt::Display for DebugCommand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DebugCommand {}", String::from(self))
}
}
impl From<&DebugCommand> for String {
fn from(command: &DebugCommand) -> Self {
match serde_json::to_string(command) {
Ok(command_string) => command_string,
_ => String::new(), }
}
}
impl From<DebugCommand> for String {
fn from(command: DebugCommand) -> Self {
match serde_json::to_string(&command) {
Ok(command_string) => command_string,
_ => String::new(), }
}
}
impl From<String> for DebugCommand {
fn from(command_string: String) -> Self {
match serde_json::from_str(&command_string) {
Ok(command) => command,
_ => DebugCommand::Invalid,
}
}
}
#[cfg(test)]
mod test {
use crate::debug_command::DebugCommand;
#[test]
fn display_debug_command() {
println!("{}", DebugCommand::Ack);
println!("{}", DebugCommand::Breakpoint(None));
println!("{}", DebugCommand::Continue);
println!("{}", DebugCommand::Delete(None));
println!("{}", DebugCommand::Error("Hello".into()));
println!("{}", DebugCommand::ExitDebugger);
println!("{}", DebugCommand::FunctionList);
println!("{}", DebugCommand::InspectFunction(0));
println!("{}", DebugCommand::Inspect);
println!("{}", DebugCommand::InspectInput(0, 0));
println!("{}", DebugCommand::InspectOutput(0, "Hello".into()));
println!("{}", DebugCommand::InspectBlock(None, None));
println!("{}", DebugCommand::Invalid);
println!("{}", DebugCommand::List);
println!("{}", DebugCommand::RunReset);
println!("{}", DebugCommand::Step(None));
println!("{}", DebugCommand::Validate);
println!("{}", DebugCommand::DebugClientStarting);
println!("{}", DebugCommand::Modify(None));
}
#[test]
fn test_command_to_string() {
assert_eq!(String::from(DebugCommand::Ack), "\"Ack\"".to_string());
}
#[test]
fn debug_command_from_string() {
let command: DebugCommand = DebugCommand::from("\"Ack\"".to_string());
assert_eq!(command, DebugCommand::Ack);
}
#[test]
fn invalid_debug_command_from_string() {
let command: DebugCommand = DebugCommand::from("\"Foo\"".to_string());
assert_eq!(command, DebugCommand::Invalid);
}
}