use std::fmt;
use std::path::PathBuf;
use super::ArtifactId;
pub const TAGGED_ID: &str = "tagged";
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Verdict {
Artifact(Finding),
Skip(SkipReason),
NotACandidate,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Provenance {
Anchored {
artifact: ArtifactId,
marker_dir: PathBuf,
},
Included {
pattern: String,
},
Cache {
cache: &'static crate::caches::Cache,
},
Tagged,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Finding {
pub path: PathBuf,
pub provenance: Provenance,
}
impl Finding {
#[must_use]
pub fn artifact(&self) -> Option<ArtifactId> {
match &self.provenance {
Provenance::Anchored { artifact, .. } => Some(*artifact),
Provenance::Cache { .. } | Provenance::Included { .. } | Provenance::Tagged => None,
}
}
#[must_use]
pub fn ecosystem(&self) -> Option<&'static str> {
match &self.provenance {
Provenance::Anchored { artifact, .. } => Some(artifact.ecosystem().id),
Provenance::Cache { cache } => Some(cache.id),
Provenance::Tagged => Some(TAGGED_ID),
Provenance::Included { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SkipReason {
NoMarker {
ecosystem: &'static str,
markers: &'static [&'static str],
},
NoInnerMarker {
ecosystem: &'static str,
markers: &'static [&'static str],
},
MarkerOutOfReach {
ecosystem: &'static str,
markers: &'static [&'static str],
levels: u8,
},
NotEnabled {
spec: String,
note: Option<&'static str>,
},
TaggedNotEnabled,
ToolCache,
Excluded {
pattern: String,
},
KeptByPolicy {
rule: &'static str,
detail: String,
},
FilesystemBoundary,
Symlink,
Covered {
by: PathBuf,
},
Unreadable,
}
impl SkipReason {
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::NoMarker { .. } => "no_marker",
Self::NoInnerMarker { .. } => "no_inner_marker",
Self::MarkerOutOfReach { .. } => "marker_out_of_reach",
Self::NotEnabled { .. } => "not_enabled",
Self::TaggedNotEnabled => "tagged_not_enabled",
Self::ToolCache => "tool_cache",
Self::Excluded { .. } => "excluded",
Self::KeptByPolicy { .. } => "kept_by_policy",
Self::FilesystemBoundary => "filesystem_boundary",
Self::Symlink => "symlink",
Self::Covered { .. } => "covered",
Self::Unreadable => "unreadable",
}
}
}
impl fmt::Display for SkipReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoMarker { ecosystem, markers } => {
write!(f, "no {ecosystem} marker ({}) proves it", markers.join(", "))
}
Self::MarkerOutOfReach {
ecosystem,
markers,
levels,
} => write!(
f,
"searched {levels} levels above it for a {ecosystem} marker ({}) and stopped \
short of the scan root, so one further up was never looked for",
markers.join(", ")
),
Self::NoInnerMarker { ecosystem, markers } => {
write!(f, "nothing inside it ({}) proves it is {ecosystem}", markers.join(", "))
}
Self::NotEnabled { spec, note } => match note {
Some(note) => write!(f, "not enabled — {note} Enable with `{spec}`"),
None => write!(f, "not enabled — enable with `{spec}`"),
},
Self::ToolCache => write!(f, "a tool cache, not build output"),
Self::TaggedNotEnabled => {
write!(
f,
"declares itself regenerable (CACHEDIR.TAG); pass `--clean-tagged` to remove it"
)
}
Self::Excluded { pattern } => write!(f, "excluded by `{pattern}`"),
Self::KeptByPolicy { rule, detail } => write!(f, "kept by {rule} ({detail})"),
Self::FilesystemBoundary => write!(f, "on a different filesystem than the scan root"),
Self::Symlink => write!(f, "is a symlink"),
Self::Covered { by } => {
write!(
f,
"already inside {}",
by.file_name().unwrap_or(by.as_os_str()).display()
)
}
Self::Unreadable => write!(f, "its anchor directory could not be read"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skip_reason_codes_are_stable() {
assert_eq!(SkipReason::Symlink.code(), "symlink");
assert_eq!(SkipReason::FilesystemBoundary.code(), "filesystem_boundary");
assert_eq!(SkipReason::Unreadable.code(), "unreadable");
assert_eq!(
SkipReason::NoMarker {
ecosystem: "rust",
markers: &["Cargo.toml"]
}
.code(),
"no_marker"
);
}
}