use std::fmt;
use std::str::FromStr;
use crate::types::hooks::InvalidPolicy;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum Policy {
#[default]
Confined,
}
impl Policy {
pub fn new(text: &str) -> Result<Self, InvalidPolicy> {
let text = text.to_owned();
match text.as_str() {
"confined" => Ok(Self::Confined),
"pure" => Err(InvalidPolicy::PureUnsupported),
"trusted" => Err(InvalidPolicy::NotAllowed { text }),
_ => Err(InvalidPolicy::Unknown { text }),
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Confined => "confined",
}
}
}
impl fmt::Display for Policy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Policy {
type Err = InvalidPolicy;
fn from_str(text: &str) -> Result<Self, Self::Err> {
Self::new(text)
}
}
impl TryFrom<&str> for Policy {
type Error = InvalidPolicy;
fn try_from(text: &str) -> Result<Self, Self::Error> {
Self::new(text)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{Diagnostic, Location, ManifestError};
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn policy_parses_confined() -> TestResult {
assert_eq!(Policy::new("confined")?, Policy::Confined);
assert_eq!(Policy::default(), Policy::Confined);
assert_eq!(Policy::Confined.to_string(), "confined");
Ok(())
}
#[test]
fn policy_refuses_pure_in_api_0() -> TestResult {
let error = Policy::new("pure").err().ok_or("expected a refusal")?;
let location = Location::at_key("packs/core/pack.toml", "hooks[1].grants.policy");
assert_eq!(
Diagnostic::new(location, ManifestError::from(error)).to_string(),
"packs/core/pack.toml: hooks[1].grants.policy: \"pure\" is not supported in api 0 (airsl has no require under pure, airsstack/airsl#12)"
);
Ok(())
}
#[test]
fn policy_refuses_trusted_as_not_allowed() -> TestResult {
let error = Policy::new("trusted").err().ok_or("expected a refusal")?;
let location = Location::at_key("packs/core/pack.toml", "hooks[1].grants.policy");
assert_eq!(
Diagnostic::new(location, ManifestError::from(error)).to_string(),
"packs/core/pack.toml: hooks[1].grants.policy: \"trusted\" is not allowed for pack hooks"
);
Ok(())
}
#[test]
fn policy_refuses_an_unknown_value_naming_the_allowed_set() -> TestResult {
for text in ["foo", "open", ""] {
let error = Policy::new(text).err().ok_or("expected a refusal")?;
let location = Location::at_key("packs/core/pack.toml", "hooks[1].grants.policy");
assert_eq!(
Diagnostic::new(location, ManifestError::from(error)).to_string(),
format!(
"packs/core/pack.toml: hooks[1].grants.policy: \"{text}\" (expected confined)"
)
);
}
Ok(())
}
#[test]
fn policy_builds_alike_through_new_from_str_and_try_from() {
fn shown(built: Result<Policy, InvalidPolicy>) -> Result<String, String> {
built
.map(|value| value.to_string())
.map_err(|error| error.to_string())
}
for text in ["confined", "pure", "trusted"] {
let expected = shown(Policy::new(text));
assert_eq!(shown(text.parse::<Policy>()), expected);
assert_eq!(shown(Policy::try_from(text)), expected);
}
assert!(Policy::new("confined").is_ok());
assert!(Policy::new("pure").is_err());
assert!(Policy::new("trusted").is_err());
}
}