holger-traits 3.3.2

holger's shared vocabulary: ArtifactId, RepositoryBackendTrait, ZnippyPlugin discriminants, and ArtifactFormat — including `ArtifactFormat::home()`, the format→crate map that answers which crate serves PyPI when grepping cannot.
Documentation
//! **Generic**: a file that is no package of any format (`raw`), named by its path and digest.

use super::{Format, Located, Named, Purl};

/// The file kinds that are packages of a format with advisories: one of them coming in as a
/// generic file would be matched against nothing, so it has no
/// generic identity.
pub const PACKAGE_KINDS: [&str; 11] = [
    ".jar", ".war", ".ear", ".pom", ".deb", ".rpm", ".crate", ".gem", ".nupkg", ".apk", ".conda",
];

/// The Generic descriptor.
pub struct Generic;

/// The one Generic descriptor ([`super::FORMATS`]).
pub static GENERIC: Generic = Generic;

impl Format for Generic {
    fn name(&self) -> &'static str {
        "generic"
    }

    fn repository(&self) -> Option<crate::ArtifactFormat> {
        Some(crate::ArtifactFormat::Raw)
    }
    fn ecosystem(&self, _distro: Option<&'static str>) -> &'static str {
        "raw"
    }
    fn claims(&self, _path: &str) -> bool {
        true
    }
    fn identify(&self, at: &Located<'_>) -> Result<Named, String> {
        let file = at.file();
        let lower = file.to_ascii_lowercase();
        if let Some(kind) = PACKAGE_KINDS.iter().find(|k| lower.ends_with(*k)) {
            return Err(format!(
                "{file:?} is a {kind} package, but no format names it here (it came in as a raw \
                 file), so no ecosystem and no advisory can be matched; upload it under its \
                 format's repository"
            ));
        }
        if file.is_empty() {
            return Err("a file with no name".into());
        }
        let (name, version) = match at.declared {
            Some((n, v)) => (n.to_string(), v.to_string()),
            None => (at.path.to_string(), String::new()),
        };
        let purl = Purl::new("generic", None, &name, &version)
            .with("checksum", &format!("sha256:{}", at.sha256));
        let coords = at.declared.map(|(n, v)| (n.to_string(), v.to_string()));
        Ok(Named { purl, coords })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::test_at as at;

    #[test]
    fn a_file_is_named_by_its_path_and_digest_but_a_package_kind_is_refused() {
        let n = GENERIC.identify(&at("files/notes.txt", None)).unwrap();
        assert_eq!(
            n.purl.to_string(),
            "pkg:generic/files%2Fnotes.txt?checksum=sha256:ab"
        );
        assert_eq!(n.coords, None);
        for kind in PACKAGE_KINDS {
            let e = GENERIC
                .identify(&at(&format!("x/a-1{kind}"), None))
                .unwrap_err();
            assert!(e.contains("no format names it here"), "{kind}: {e}");
        }
        assert!(GENERIC.identify(&at("", None)).is_err());
        assert!(GENERIC.claims("anything"));
        assert_eq!(GENERIC.ecosystem(None), "raw");
    }
}