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
//! **APK**: an Alpine/Wolfi/Chainguard `.apk`, named under its distribution.

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

/// The Apk descriptor.
pub struct Apk;

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

impl Format for Apk {
    fn name(&self) -> &'static str {
        "apk"
    }
    fn ecosystem(&self, distro: Option<&'static str>) -> &'static str {
        distro.unwrap_or("alpine")
    }
    fn claims(&self, path: &str) -> bool {
        let file = path.rsplit('/').next().unwrap_or(path).to_ascii_lowercase();
        let ends = |exts: &[&str]| exts.iter().any(|e| file.ends_with(e));
        ends(&[".apk"])
    }
    fn identify(&self, at: &Located<'_>) -> Result<Named, String> {
        let distro = at.distro.unwrap_or("alpine");
        let (name, version) = match at.declared {
            Some((n, v)) => (n.to_string(), v.to_string()),
            None => {
                let stem = at.file().strip_suffix(".apk").ok_or_else(|| {
                    format!("{:?} is no APK (`<name>-<version>-r<n>.apk`)", at.path)
                })?;
                let mut it = stem.rsplitn(3, '-');
                match (it.next(), it.next(), it.next()) {
                    (Some(r), Some(v), Some(n))
                        if r.starts_with('r') && !n.is_empty() && !v.is_empty() =>
                    {
                        (n.to_string(), format!("{v}-{r}"))
                    }
                    _ => {
                        return Err(format!(
                            "{:?} is no APK (`<name>-<version>-r<n>.apk`)",
                            at.path
                        ))
                    }
                }
            }
        };
        let arch = at.path.rsplit('/').nth(1).unwrap_or_default();
        let purl = Purl::new("apk", Some(distro), &name, &version).with("arch", arch);
        Ok(Named {
            purl,
            coords: Some((name, version)),
        })
    }
}

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

    #[test]
    fn an_apk_is_named_under_its_distribution() {
        let n = APK
            .identify(&Located {
                distro: Some("wolfi"),
                ..at("x86_64/openssl-3.2.0-r0.apk", None)
            })
            .unwrap();
        assert_eq!(
            n.purl.to_string(),
            "pkg:apk/wolfi/openssl@3.2.0-r0?arch=x86_64"
        );
        assert_eq!(APK.ecosystem(Some("wolfi")), "wolfi");
        assert_eq!(APK.ecosystem(None), "alpine");
        assert!(APK.identify(&at("x86_64/openssl-3.2.0.apk", None)).is_err());
    }
}