use xuanji::{RuleKey, ScanDepth, Severity};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImplTraitBoundary {
pub(crate) crate_package: String,
pub(crate) module: String,
pub(crate) forbidden_operands: Vec<String>,
pub(crate) reason: String,
pub(crate) anchor: Option<String>,
pub(crate) severity: Severity,
pub(crate) depth: ScanDepth,
}
impl ImplTraitBoundary {
pub fn rule_key(&self) -> RuleKey {
RuleKey::of(
"tianheng.rule/hunyi/impl-trait-exposure",
[(
"forbidden_operands",
super::canonical_path_set(&self.forbidden_operands),
)],
)
}
pub fn scan_depth(&self) -> ScanDepth {
self.depth
}
pub fn in_crate(package: &str) -> ImplTraitCrateDraft {
ImplTraitCrateDraft {
crate_package: package.to_string(),
}
}
pub fn crate_package(&self) -> &str {
&self.crate_package
}
pub fn module(&self) -> &str {
&self.module
}
pub fn forbidden_operands(&self) -> &[String] {
&self.forbidden_operands
}
pub fn reason(&self) -> &str {
&self.reason
}
pub fn with_anchor(mut self, anchor: &str) -> Self {
self.anchor = Some(anchor.to_string());
self
}
pub fn anchor(&self) -> Option<&str> {
self.anchor.as_deref()
}
pub fn severity(&self) -> Severity {
self.severity
}
pub fn including_submodules(&self) -> bool {
self.depth == ScanDepth::Subtree
}
}
#[doc(hidden)]
pub struct ImplTraitCrateDraft {
crate_package: String,
}
impl ImplTraitCrateDraft {
pub fn module(self, module: &str) -> ImplTraitModuleDraft {
ImplTraitModuleDraft {
crate_package: self.crate_package,
module: module.to_string(),
}
}
}
#[doc(hidden)]
pub struct ImplTraitModuleDraft {
crate_package: String,
module: String,
}
impl ImplTraitModuleDraft {
pub fn must_not_expose_impl_trait(self) -> ImplTraitBoundaryDraft {
ImplTraitBoundaryDraft {
crate_package: self.crate_package,
module: self.module,
forbidden_operands: Vec::new(),
severity: Severity::Enforce,
depth: ScanDepth::Shallow,
}
}
pub fn must_not_expose_impl_trait_of<I, S>(self, operands: I) -> ImplTraitBoundaryDraft
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
ImplTraitBoundaryDraft {
crate_package: self.crate_package,
module: self.module,
forbidden_operands: operands.into_iter().map(Into::into).collect(),
severity: Severity::Enforce,
depth: ScanDepth::Shallow,
}
}
}
#[doc(hidden)]
pub struct ImplTraitBoundaryDraft {
crate_package: String,
module: String,
forbidden_operands: Vec<String>,
severity: Severity,
depth: ScanDepth,
}
impl ImplTraitBoundaryDraft {
pub fn depth(mut self, depth: ScanDepth) -> Self {
self.depth = depth;
self
}
pub fn warn(mut self) -> Self {
self.severity = Severity::Warn;
self
}
pub fn including_submodules(self) -> Self {
self.depth(ScanDepth::Subtree)
}
pub fn because(self, reason: &str) -> ImplTraitBoundary {
ImplTraitBoundary {
crate_package: self.crate_package,
module: self.module,
forbidden_operands: self.forbidden_operands,
reason: reason.to_string(),
anchor: None,
severity: self.severity,
depth: self.depth,
}
}
}