use std::fmt;
use pmpx_plugin_abi as abi;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u32)]
pub enum Verb {
Install = abi::PMPX_VERB_INSTALL,
Remove = abi::PMPX_VERB_REMOVE,
Run = abi::PMPX_VERB_RUN,
Build = abi::PMPX_VERB_BUILD,
Test = abi::PMPX_VERB_TEST,
Update = abi::PMPX_VERB_UPDATE,
Exec = abi::PMPX_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::PMPX_VERB_INSTALL => Some(Verb::Install),
abi::PMPX_VERB_REMOVE => Some(Verb::Remove),
abi::PMPX_VERB_RUN => Some(Verb::Run),
abi::PMPX_VERB_BUILD => Some(Verb::Build),
abi::PMPX_VERB_TEST => Some(Verb::Test),
abi::PMPX_VERB_UPDATE => Some(Verb::Update),
abi::PMPX_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())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_verb_has_its_number() {
for (verb, number) in [
(Verb::Install, abi::PMPX_VERB_INSTALL),
(Verb::Remove, abi::PMPX_VERB_REMOVE),
(Verb::Run, abi::PMPX_VERB_RUN),
(Verb::Build, abi::PMPX_VERB_BUILD),
(Verb::Test, abi::PMPX_VERB_TEST),
(Verb::Update, abi::PMPX_VERB_UPDATE),
(Verb::Exec, abi::PMPX_VERB_EXEC),
] {
assert_eq!(verb.to_abi(), number);
assert_eq!(Verb::from_abi(number), Some(verb));
}
assert_eq!(Verb::ALL.len(), 7, "the closed set has seven verbs");
}
#[test]
fn an_unknown_number_has_no_verb() {
assert_eq!(Verb::from_abi(999), None);
}
}