use std::{ fmt::{self, Display}, str::FromStr, }; pub struct Instruction { pub name: String, pub is_short: bool, pub is_keep: bool, pub is_return: bool, pub is_cross_stack: bool, } pub enum InstructionParseError { InvalidMode, } impl Display for InstructionParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { InstructionParseError::InvalidMode => write!(f, "Invalid mode"), } } } impl FromStr for Instruction { type Err = InstructionParseError; fn from_str(s: &str) -> Result { let name = s.chars().take(3).collect(); let mut is_short = false; let mut is_keep = false; let mut is_return = false; let mut is_cross_stack = false; for mode in s.chars().skip(3) { match mode { '2' => is_short = true, 'k' => is_keep = true, 'r' => is_return = true, 'x' => is_cross_stack = true, _ => return Err(Self::Err::InvalidMode), } } Ok(Self { name, is_short, is_keep, is_return, is_cross_stack, }) } }