mode.rs: Implement more traits for use in frontend

This commit is contained in:
John 2023-04-23 12:14:46 -05:00
parent 59ba8ac20b
commit 04736f1153

View File

@ -28,12 +28,37 @@ impl FromStr for Mode {
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
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<str> 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<usize> for Mode {
fn from(value: usize) -> Self {
match value {
0 => Self::Chip8,
1 => Self::SChip,
2 => Self::XOChip,
_ => Self::Chip8,
}
}
}