From 04736f1153ae29330d07f26445cffb36aa7d5531 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Sun, 23 Apr 2023 12:14:46 -0500 Subject: [PATCH] mode.rs: Implement more traits for use in frontend --- src/cpu/mode.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/cpu/mode.rs b/src/cpu/mode.rs index f531a8c..3645b41 100644 --- a/src/cpu/mode.rs +++ b/src/cpu/mode.rs @@ -28,12 +28,37 @@ impl FromStr for Mode { fn from_str(s: &str) -> std::result::Result { match s.to_lowercase().as_str() { - "chip8" | "chip-8" => Ok(Mode::Chip8), - "schip" | "superchip" => Ok(Mode::SChip), - "xo-chip" | "xochip" => Ok(Mode::XOChip), - _ => Err(Error::InvalidMode { - mode: s.to_string(), - }), + "chip" | "chip8" | "chip-8" => Ok(Mode::Chip8), + "s" | "schip" | "superchip" | "super chip" => Ok(Mode::SChip), + "xo" | "xochip" | "xo-chip" => Ok(Mode::XOChip), + _ => Err(Error::InvalidMode { mode: s.into() }), + } + } +} + +impl AsRef for Mode { + fn as_ref(&self) -> &str { + match self { + Mode::Chip8 => "Chip-8", + Mode::SChip => "Super Chip", + Mode::XOChip => "XO-Chip", + } + } +} + +impl ToString for Mode { + fn to_string(&self) -> String { + self.as_ref().into() + } +} + +impl From for Mode { + fn from(value: usize) -> Self { + match value { + 0 => Self::Chip8, + 1 => Self::SChip, + 2 => Self::XOChip, + _ => Self::Chip8, } } }