2025-05-02 20:53:12 +02:00
|
|
|
use crate::meta_commands::{MetaCommand, MetaCommandExecuteResult, MetaCommandParseError};
|
|
|
|
|
use crate::statements::{Statement, StatementExecuteResult, StatementParseError};
|
2025-05-02 20:35:45 +02:00
|
|
|
|
|
|
|
|
pub enum Command {
|
|
|
|
|
MetaCommand(MetaCommand),
|
|
|
|
|
Statement(Statement),
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 20:53:12 +02:00
|
|
|
pub struct CommandExecuteResult {
|
|
|
|
|
pub should_exit: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MetaCommandExecuteResult> for CommandExecuteResult {
|
|
|
|
|
fn from(value: MetaCommandExecuteResult) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
should_exit: value.should_exit,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<StatementExecuteResult> for CommandExecuteResult {
|
|
|
|
|
fn from(_value: StatementExecuteResult) -> Self {
|
|
|
|
|
Self { should_exit: false }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Command {
|
|
|
|
|
pub fn execute(&self) -> CommandExecuteResult {
|
|
|
|
|
match self {
|
|
|
|
|
Command::MetaCommand(cmd) => cmd.execute().into(),
|
|
|
|
|
Command::Statement(stmt) => stmt.execute().into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 20:35:45 +02:00
|
|
|
pub enum CommandParseError {
|
|
|
|
|
MetaCommand(MetaCommandParseError),
|
|
|
|
|
Statement(StatementParseError),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for CommandParseError {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
CommandParseError::MetaCommand(meta_command_parse_error) => {
|
|
|
|
|
write!(f, "{meta_command_parse_error}")
|
|
|
|
|
}
|
|
|
|
|
CommandParseError::Statement(statement_parse_error) => {
|
|
|
|
|
write!(f, "{statement_parse_error}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MetaCommand> for Command {
|
|
|
|
|
fn from(value: MetaCommand) -> Self {
|
|
|
|
|
Command::MetaCommand(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MetaCommandParseError> for CommandParseError {
|
|
|
|
|
fn from(value: MetaCommandParseError) -> Self {
|
|
|
|
|
CommandParseError::MetaCommand(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Statement> for Command {
|
|
|
|
|
fn from(value: Statement) -> Self {
|
|
|
|
|
Command::Statement(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<StatementParseError> for CommandParseError {
|
|
|
|
|
fn from(value: StatementParseError) -> Self {
|
|
|
|
|
CommandParseError::Statement(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::str::FromStr for Command {
|
|
|
|
|
type Err = CommandParseError;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
if s.starts_with(".") {
|
|
|
|
|
s.parse::<MetaCommand>()
|
|
|
|
|
.map(|x| x.into())
|
|
|
|
|
.map_err(|x| x.into())
|
|
|
|
|
} else {
|
|
|
|
|
s.parse::<Statement>()
|
|
|
|
|
.map(|x| x.into())
|
|
|
|
|
.map_err(|x| x.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|