use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
use std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CapabilitySet {
Bounding,
Effective,
Inheritable,
Permitted,
Ambient,
}
impl CapabilitySet {
pub(crate) const ALL: &[Self] = &[
Self::Bounding,
Self::Effective,
Self::Inheritable,
Self::Permitted,
Self::Ambient,
];
pub fn parse(raw_value: &str) -> Result<Self, Error> {
platform::outcome(
ffi::cz_capability_set_parse(raw_value),
format!("parse the capability set {raw_value}"),
)
.map(|outcome| Self::from_description(&outcome.text()))
}
pub fn description(self) -> &'static str {
match self {
Self::Bounding => "bounding",
Self::Effective => "effective",
Self::Inheritable => "inheritable",
Self::Permitted => "permitted",
Self::Ambient => "ambient",
}
}
fn from_description(description: &str) -> Self {
Self::ALL
.iter()
.copied()
.find(|case| case.description() == description)
.unwrap_or_else(|| panic!("Swift's CapabilitySet has a case Rust lacks: {description:?}"))
}
}
impl fmt::Display for CapabilitySet {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.description())
}
}