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-15 15:25:55 +02:00
|
|
|
CreateItem { name: String },
|
2024-05-10 12:03:38 +02:00
|
|
|
Edit,
|
2024-05-10 23:46:51 +02:00
|
|
|
|
|
|
|
ShowAll,
|
|
|
|
HideDone,
|
2024-05-12 12:58:54 +02:00
|
|
|
Test,
|
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-15 15:25:55 +02:00
|
|
|
"ci" | "create-item" => Some(Commands::CreateItem {
|
|
|
|
name: rest.join(" ").to_string(),
|
|
|
|
}),
|
2024-05-10 12:03:38 +02:00
|
|
|
"e" | "edit" => Some(Commands::Edit),
|
2024-05-10 23:46:51 +02:00
|
|
|
"show-all" => Some(Commands::ShowAll),
|
|
|
|
"hide-done" => Some(Commands::HideDone),
|
2024-05-12 12:58:54 +02:00
|
|
|
"test" => Some(Commands::Test),
|
2024-05-09 14:10:43 +02:00
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|