use {
super::*,
crate::{
pattern::*,
verb::{
Internal,
VerbId,
VerbInvocation,
VerbStore,
Verb,
},
},
bet::BeTree,
};
#[derive(Debug, Clone)]
pub enum Command {
None,
VerbEdit(VerbInvocation),
VerbInvocate(VerbInvocation),
Internal {
internal: Internal,
input_invocation: Option<VerbInvocation>,
},
VerbTrigger {
verb_id: VerbId,
input_invocation: Option<VerbInvocation>,
},
PatternEdit {
raw: String,
expr: BeTree<PatternOperator, PatternParts>,
},
Click(u16, u16),
DoubleClick(u16, u16),
}
impl Command {
pub fn empty() -> Command {
Command::None
}
pub fn is_none(&self) -> bool {
matches!(self, Command::None)
}
pub fn triggered_verb<'v>(
&self,
verb_store: &'v VerbStore,
) -> Option<&'v Verb> {
match self {
Self::VerbTrigger { verb_id, .. } => Some(verb_store.verb(*verb_id)),
_ => None,
}
}
pub fn as_verb_invocation(&self) -> Option<&VerbInvocation> {
match self {
Self::VerbEdit(vi) => Some(vi),
Self::VerbInvocate(vi) => Some(vi),
Self::Internal {
input_invocation, ..
} => input_invocation.as_ref(),
Self::VerbTrigger {
input_invocation, ..
} => input_invocation.as_ref(),
_ => None,
}
}
pub fn from_parts(
mut cp: CommandParts,
finished: bool,
) -> Self {
if let Some(verb_invocation) = cp.verb_invocation.take() {
if finished {
Self::VerbInvocate(verb_invocation)
} else {
Self::VerbEdit(verb_invocation)
}
} else if finished {
Self::Internal {
internal: Internal::open_stay,
input_invocation: None,
}
} else {
Self::PatternEdit {
raw: cp.raw_pattern,
expr: cp.pattern,
}
}
}
pub fn is_verb_invocated_from_input(&self) -> bool {
matches!(self, Self::VerbInvocate(_))
}
pub fn from_raw(
raw: String,
finished: bool,
) -> Self {
let parts = CommandParts::from(raw);
Self::from_parts(parts, finished)
}
pub fn from_pattern(pattern: &InputPattern) -> Self {
Command::from_raw(pattern.raw.clone(), false)
}
}
impl Default for Command {
fn default() -> Command {
Command::empty()
}
}