2024-05-09 14:10:43 +02:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
pub enum Commands {
|
|
|
|
Write,
|
|
|
|
Quit,
|
|
|
|
WriteQuit,
|
2024-05-09 15:26:58 +02:00
|
|
|
Archive,
|
2024-05-09 18:00:25 +02:00
|
|
|
CreateSection { name: String },
|
2024-05-10 12:03:38 +02:00
|
|
|
Edit,
|
2024-05-09 14:10:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Commands {
|
|
|
|
pub fn is_write(&self) -> bool {
|
|
|
|
matches!(self, Commands::Write | Commands::WriteQuit)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_quit(&self) -> bool {
|
|
|
|
matches!(self, Commands::Quit | Commands::WriteQuit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CommandParser {}
|
|
|
|
|
|
|
|
impl CommandParser {
|
|
|
|
pub fn parse(raw_command: &str) -> Option<Commands> {
|
|
|
|
let prepared = raw_command.trim();
|
|
|
|
// TODO: respect quotes
|
|
|
|
let parts = prepared.split_whitespace().collect_vec();
|
|
|
|
|
|
|
|
match parts.split_first() {
|
2024-05-09 18:00:25 +02:00
|
|
|
Some((command, rest)) => match *command {
|
2024-05-09 14:10:43 +02:00
|
|
|
"w" | "write" => Some(Commands::Write),
|
|
|
|
"q" | "quit" => Some(Commands::Quit),
|
|
|
|
"wq" | "write-quit" => Some(Commands::WriteQuit),
|
2024-05-09 15:26:58 +02:00
|
|
|
"a" | "archive" => Some(Commands::Archive),
|
2024-05-09 18:00:25 +02:00
|
|
|
"cs" | "create-section" => rest.first().map(|name| Commands::CreateSection {
|
|
|
|
name: name.to_string(),
|
|
|
|
}),
|
2024-05-10 12:03:38 +02:00
|
|
|
"e" | "edit" => Some(Commands::Edit),
|
2024-05-09 14:10:43 +02:00
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|