use xuanji::{RuleKey, Severity};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnsafeBoundary {
pub(crate) crate_package: String,
pub(crate) allowed_locations: Vec<String>,
pub(crate) reason: String,
pub(crate) anchor: Option<String>,
pub(crate) severity: Severity,
}
impl UnsafeBoundary {
pub fn rule_key(&self) -> RuleKey {
RuleKey::of(
"tianheng.rule/hunyi/unsafe-confinement",
[(
"allowed",
super::canonical_path_set(&self.allowed_locations),
)],
)
}
pub fn in_crate(package: &str) -> UnsafeCrateDraft {
UnsafeCrateDraft {
crate_package: package.to_string(),
}
}
pub fn allowed_locations(&self) -> &[String] {
&self.allowed_locations
}
pub fn reason(&self) -> &str {
&self.reason
}
}
crate::dsl::boundary_common!(UnsafeBoundary, UnsafeBoundaryDraft);
#[doc(hidden)]
pub struct UnsafeCrateDraft {
crate_package: String,
}
impl UnsafeCrateDraft {
pub fn only_under<I, S>(self, locations: I) -> UnsafeBoundaryDraft
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
UnsafeBoundaryDraft {
crate_package: self.crate_package,
allowed_locations: locations.into_iter().map(Into::into).collect(),
severity: Severity::Enforce,
}
}
}
#[doc(hidden)]
pub struct UnsafeBoundaryDraft {
crate_package: String,
allowed_locations: Vec<String>,
severity: Severity,
}
impl UnsafeBoundaryDraft {
pub fn because(self, reason: &str) -> UnsafeBoundary {
UnsafeBoundary {
crate_package: self.crate_package,
allowed_locations: self.allowed_locations,
reason: reason.to_string(),
anchor: None,
severity: self.severity,
}
}
}