znippy-common 0.10.7

Core logic and data structures for Znippy, a parallel chunked compression system.
Documentation
//! The **keyed security-feed** handlers: one `type_id` per feed type, each
//! archive laid out by [`crate::feed`].
//!
//! A feed archive (the CVE advisory set first; malware/known-bad, popular names
//! and signer keys later) is a znippy whose files are not packages but
//! **frames**: ~64 KB Arrow IPC batches of rows sorted by a key, named so that
//! the archive's own sorted lookup is the key index:
//!
//! ```text
//! h/header                      the feed header (kind, generation, rev, …)
//! k/<group>/<first key>         data frame: whole keys only, sorted by key
//! t/<group>/<first key>         tombstone frame (delta archives only)
//! s/<first secondary key>       secondary index frame: (skey, group, key)
//! ```
//!
//! Each group (an ecosystem, for advisories) is its own data sub-index
//! (`GroupKey { pkg_type: type_id, repo: <group> }`), and these handlers'
//! columns ride on it — so `znippy list` / DuckDB show the key index directly:
//!
//! | column | meaning |
//! |---|---|
//! | `section` | `header`, `data`, `tombstone` or `secondary` |
//! | `group` | the group (ecosystem) the frame belongs to |
//! | `first_key` / `last_key` | the key range the frame holds |
//! | `keys` | distinct keys in the frame |
//! | `rows` | rows in the frame |
//!
//! Tier A by rule P-5: `std` + `arrow` + this crate.

use crate::plugin::{ArchiveTypePlugin, Claim, Col, ExtensionRow, ExtensionValue, HandlerSpec};

/// `type_id` of the CVE / vulnerability **advisory** feed. 27 is the first free
/// discriminant above `generic` (26), below the out-of-crate handlers (40-42).
pub const ADVISORY_FEED_TYPE_ID: i8 = 27;

/// The columns every feed handler contributes (one row per frame).
pub const FEED_FIELDS: &[(&str, Col)] = &[
    ("section", Col::Utf8),
    ("group", Col::Utf8),
    ("first_key", Col::Utf8),
    ("last_key", Col::Utf8),
    ("keys", Col::U32),
    ("rows", Col::U32),
];

fn is_feed_path(path: &str) -> bool {
    path == crate::feed::HEADER_PATH
        || path.starts_with(crate::feed::DATA_PREFIX)
        || path.starts_with(crate::feed::TOMB_PREFIX)
        || path.starts_with(crate::feed::SEC_PREFIX)
}

/// The advisory feed handler.
pub static ADVISORY_FEED_SPEC: HandlerSpec = HandlerSpec {
    name: "advisory",
    aliases: &["cve", "advisories", "vuln-feed"],
    type_id: ADVISORY_FEED_TYPE_ID,
    ecosystem: "security feed — vulnerability advisories, one sub-index per ecosystem",
    extensions: &[],
    claim: Claim::Layout(is_feed_path),
    description: "Keyed advisory feed: ~64 KB Arrow frames per ecosystem sorted by \
                  package, a name→frame key index in the sorted lookup, an \
                  id→(ecosystem, package) secondary index, signed base + delta \
                  generations.",
    fields: FEED_FIELDS,
    commands: &[],
};

/// The advisory feed handler (`--format advisory`).
pub struct AdvisoryFeedPlugin;

impl ArchiveTypePlugin for AdvisoryFeedPlugin {
    fn spec(&self) -> &HandlerSpec {
        &ADVISORY_FEED_SPEC
    }

    fn extract_metadata(&self, path: &str, data: &[u8]) -> Option<ExtensionRow> {
        let s = crate::feed::frame_stats(path, data).ok()?;
        let mut fields = std::collections::HashMap::new();
        fields.insert("section".into(), ExtensionValue::Str(s.section.as_str().into()));
        fields.insert("group".into(), ExtensionValue::Str(s.group));
        fields.insert("first_key".into(), ExtensionValue::Str(s.first_key));
        fields.insert("last_key".into(), ExtensionValue::Str(s.last_key));
        fields.insert("keys".into(), ExtensionValue::U32(s.keys));
        fields.insert("rows".into(), ExtensionValue::U32(s.rows));
        Some(ExtensionRow { fields })
    }
}