osdb/src/meta_commands.rs

44 lines
1 KiB
Rust

#[derive(Debug, Eq, PartialEq)]
pub enum MetaCommand {
Exit,
}
pub struct MetaCommandExecuteResult {
pub should_exit: bool,
}
impl MetaCommand {
pub fn execute(&self) -> MetaCommandExecuteResult {
match self {
MetaCommand::Exit => MetaCommandExecuteResult { should_exit: true },
}
}
}
#[derive(Debug)]
pub enum MetaCommandParseError {
Unrecognized { cmd: String },
}
impl std::fmt::Display for MetaCommandParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MetaCommandParseError::Unrecognized { cmd } => {
write!(f, "unrecognized meta-command {cmd:?}")
}
}
}
}
impl std::str::FromStr for MetaCommand {
type Err = MetaCommandParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
".exit" => Ok(MetaCommand::Exit),
cmd => Err(MetaCommandParseError::Unrecognized {
cmd: cmd.to_string(),
}),
}
}
}