use std::fmt;
use std::str::FromStr;
use crate::abi;
use crate::PluginError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u32)]
pub enum Verb {
Install = abi::VERB_INSTALL,
Remove = abi::VERB_REMOVE,
Run = abi::VERB_RUN,
Build = abi::VERB_BUILD,
Test = abi::VERB_TEST,
Update = abi::VERB_UPDATE,
Exec = abi::VERB_EXEC,
}
impl Verb {
pub const ALL: &'static [Verb] = &[
Verb::Install,
Verb::Remove,
Verb::Run,
Verb::Build,
Verb::Test,
Verb::Update,
Verb::Exec,
];
pub const fn to_abi(self) -> u32 {
self as u32
}
pub const fn from_abi(n: u32) -> Option<Verb> {
match n {
abi::VERB_INSTALL => Some(Verb::Install),
abi::VERB_REMOVE => Some(Verb::Remove),
abi::VERB_RUN => Some(Verb::Run),
abi::VERB_BUILD => Some(Verb::Build),
abi::VERB_TEST => Some(Verb::Test),
abi::VERB_UPDATE => Some(Verb::Update),
abi::VERB_EXEC => Some(Verb::Exec),
_ => None,
}
}
pub const fn as_str(self) -> &'static str {
match self {
Verb::Install => "install",
Verb::Remove => "remove",
Verb::Run => "run",
Verb::Build => "build",
Verb::Test => "test",
Verb::Update => "update",
Verb::Exec => "exec",
}
}
}
impl fmt::Display for Verb {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Verb {
type Err = PluginError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Verb::ALL
.iter()
.copied()
.find(|v| v.as_str() == s)
.ok_or_else(|| PluginError::other(format!("unknown verb: {s}")))
}
}