Skip to main content

znippy_common/plugins/native/
skeletons.rs

1//! Skeleton package handlers — dummy implementations for additional ecosystems.
2//!
3//! Each handler here is a *stub*: it self-describes via [`meta()`](ArchiveTypePlugin::meta)
4//! so it shows up in `znippy handlers` and is selectable with `--format <name>`,
5//! and it implements a generic filename-based `coords` subcommand +
6//! `extract_metadata` (package name + best-effort version parsed from the
7//! filename, zero decompression cost). Real per-ecosystem parsing (manifest
8//! files inside the archive, signatures, dep graphs) is left as a TODO for
9//! whoever promotes the stub to a full handler.
10//!
11//! To promote a skeleton: replace the generated `extract_metadata` body with
12//! real parsing, add `schema_fields()`, and add any ecosystem-specific
13//! subcommands to its `meta().commands` + `run_command`.
14//!
15//! `type_id` registry continues from the native handlers (1=cargo, 2=python,
16//! 3=maven, 6=npm, 11=gem, 14=conda — npm is `plugins::npm_native`, gem is
17//! `plugins::gem_native`, conda is `plugins::conda_native`, NONE a skeleton). See
18//! `.nornir/plugins-design.md` §f3 for the authoritative table, including the
19//! tier-B ids that live in their own crates (25=media, 40=skidbladnir,
20//! 41=rust-toolchain, 42=git).
21//!
22//! A promoted handler stays **here**, in `znippy-common/src/plugins/native/`, for as
23//! long as it needs nothing but `std`, `arrow` and this crate. Needing a
24//! third-party parser, a decompressor, a network fetch, its own crate-type or
25//! its own release cadence is what makes it a `znippy-plugin-*` crate instead —
26//! rule **P-5**.
27
28use crate::plugin::{ArchiveTypePlugin, ExtensionRow, ExtensionValue, HandlerCommand, HandlerMeta};
29use std::collections::HashMap;
30
31/// Best-effort `(name, version?)` split from a package filename.
32///
33/// Strips the first matching extension, then splits at the last `-`/`_`/`@`
34/// that is immediately followed by a digit (the conventional version start).
35/// Falls back to `(stem, None)` for formats without an embedded version
36/// (e.g. raw ELF binaries).
37fn parse_coords(path: &str, extensions: &[&str]) -> (String, Option<String>) {
38    let filename = path.rsplit('/').next().unwrap_or(path);
39    let stem = extensions
40        .iter()
41        .find_map(|ext| filename.strip_suffix(ext))
42        .unwrap_or(filename);
43
44    let mut split_pos = None;
45    for (i, c) in stem.char_indices() {
46        if matches!(c, '-' | '_' | '@') {
47            if let Some(next) = stem[i + 1..].chars().next() {
48                if next.is_ascii_digit() {
49                    split_pos = Some(i);
50                }
51            }
52        }
53    }
54
55    match split_pos {
56        Some(pos) => (stem[..pos].to_string(), Some(stem[pos + 1..].to_string())),
57        None => (stem.to_string(), None),
58    }
59}
60
61/// Stamp out a skeleton handler that parses `name` + best-effort `version`
62/// from the filename and advertises a single `coords` subcommand.
63macro_rules! skeleton_handler {
64    (
65        struct: $struct:ident,
66        name: $name:literal,
67        type_id: $tid:literal,
68        aliases: [$($alias:literal),* $(,)?],
69        ecosystem: $eco:literal,
70        extensions: [$($ext:literal),* $(,)?],
71        description: $desc:literal $(,)?
72    ) => {
73        #[doc = concat!("Skeleton handler for the ", $eco, " ecosystem (stub).")]
74        pub struct $struct;
75
76        impl $struct {
77            const EXTENSIONS: &'static [&'static str] = &[$($ext),*];
78        }
79
80        impl ArchiveTypePlugin for $struct {
81            fn name(&self) -> &str { $name }
82
83            fn type_id(&self) -> i8 { $tid }
84
85            fn meta(&self) -> HandlerMeta {
86                HandlerMeta {
87                    name: $name.into(),
88                    aliases: vec![$($alias.into()),*],
89                    type_id: $tid,
90                    ecosystem: $eco.into(),
91                    extensions: vec![$($ext.into()),*],
92                    description: concat!($desc, " (skeleton — filename-only)").into(),
93                    commands: vec![
94                        HandlerCommand::new(
95                            "coords",
96                            concat!("Print ", $name, " package name + version parsed from a path"),
97                        ),
98                    ],
99                }
100            }
101
102            fn run_command(&self, cmd: &str, args: &[String]) -> anyhow::Result<()> {
103                match cmd {
104                    "coords" => {
105                        let path = args.first().ok_or_else(|| {
106                            anyhow::anyhow!(concat!("usage: ", $name, " coords <file>"))
107                        })?;
108                        let (name, version) = parse_coords(path, Self::EXTENSIONS);
109                        match version {
110                            Some(v) => println!("{} {}", name, v),
111                            None => println!("{}", name),
112                        }
113                        Ok(())
114                    }
115                    other => anyhow::bail!(concat!($name, ": unknown subcommand '{}'"), other),
116                }
117            }
118
119            fn matches_path(&self, path: &str) -> bool {
120                Self::EXTENSIONS.iter().any(|ext| path.ends_with(ext))
121            }
122
123            fn extract_metadata(&self, path: &str, _data: &[u8]) -> Option<ExtensionRow> {
124                let (name, version) = parse_coords(path, Self::EXTENSIONS);
125                let mut fields = HashMap::new();
126                fields.insert("name".into(), ExtensionValue::Str(name));
127                fields.insert(
128                    "version".into(),
129                    ExtensionValue::OptStr(version),
130                );
131                Some(ExtensionRow { fields })
132            }
133        }
134    };
135}
136
137skeleton_handler! {
138    struct: GoPlugin,
139    name: "go",
140    type_id: 4,
141    aliases: ["golang"],
142    ecosystem: "Go / Go modules (proxy.golang.org)",
143    extensions: [".zip", ".mod", ".info"],
144    description: "Go module zips published to the module proxy",
145}
146
147skeleton_handler! {
148    struct: NugetPlugin,
149    name: "nuget",
150    type_id: 5,
151    aliases: ["dotnet", ".net"],
152    ecosystem: ".NET / NuGet (nuget.org)",
153    extensions: [".nupkg", ".snupkg"],
154    description: ".NET NuGet packages (zip with a .nuspec manifest)",
155}
156
157// npm (type_id 6) is a REAL handler now — see `plugins::npm_native::NpmPlugin`
158// (authoritative name+version from package.json). It is intentionally absent from
159// the skeleton list below; `register` folds in `NpmPlugin` from `npm_native`.
160
161skeleton_handler! {
162    struct: ElfPlugin,
163    name: "elf",
164    type_id: 7,
165    aliases: ["binary", "so"],
166    ecosystem: "Linux ELF binaries / shared objects",
167    extensions: [".elf", ".so", ".bin", ".out"],
168    description: "Raw ELF executables / shared libraries (no embedded version)",
169}
170
171// rpm (type_id 8) is a REAL handler now — see `plugins::rpm_native::RpmPlugin`
172// (authoritative NEVRA incl. epoch from the header tag table). It is intentionally
173// absent from the skeleton list below; `builtin_handlers` folds in `RpmPlugin`.
174
175// deb (type_id 9) is a REAL handler now — see `plugins::deb_native::DebPlugin`
176// (authoritative control fields from the control tarball: ar → control.tar.* →
177// control). Intentionally absent from the skeleton list; `builtin_handlers` folds
178// in `DebPlugin`.
179
180skeleton_handler! {
181    struct: FlatpakPlugin,
182    name: "flatpak",
183    type_id: 10,
184    aliases: ["flatpakref"],
185    ecosystem: "Flatpak bundles (Flathub)",
186    extensions: [".flatpak", ".flatpakref"],
187    description: "Flatpak single-file application bundles (OSTree)",
188}
189
190// gem (type_id 11) is a REAL handler now — see `plugins::gem_native::GemPlugin`
191// (authoritative name/version/platform from metadata.gz). It is intentionally
192// absent from the skeleton list; `builtin_handlers` folds in `GemPlugin`.
193
194// `image` was an alias here and is NOT any more. It collided with the REAL media
195// handler (`znippy-plugin-media`, type_id 25), which advertises `image` for a
196// picture. `find_handler` takes the first match in `builtin_handlers()` order —
197// `sort_by_key(type_id)` — so docker (12) won over media (25) and `--format
198// image` over a photo tree silently sealed the archive with `pkg_type = 12`, a
199// skeleton that extracts nothing, while `znippy handlers` listed the alias under
200// media. media's `image` was unreachable and docker's was a lie about the
201// content; the collision is now asserted against in
202// `znippy-cli::handlers::tests::every_name_and_alias_in_the_register_is_unique`.
203// `oci` and `container` say "container image" with no ambiguity at all, so
204// nothing is lost here.
205skeleton_handler! {
206    struct: DockerPlugin,
207    name: "docker",
208    type_id: 12,
209    aliases: ["oci", "container"],
210    ecosystem: "OCI / Docker container images",
211    extensions: [".oci", ".docker"],
212    description: "OCI image layouts / docker save tarballs (manifest + layers)",
213}
214
215skeleton_handler! {
216    struct: HelmPlugin,
217    name: "helm",
218    type_id: 13,
219    aliases: ["chart", "k8s"],
220    ecosystem: "Helm charts (Kubernetes / Artifact Hub)",
221    extensions: [".tgz"],
222    description: "Helm chart archives (Chart.yaml + templates)",
223}
224
225// conda (type_id 14) is a REAL handler now — see
226// `plugins::conda_native::CondaPlugin` (authoritative name/version/build/subdir
227// from info/index.json for .tar.bz2). It is intentionally absent from the
228// skeleton list; `builtin_handlers` folds in `CondaPlugin`.
229
230skeleton_handler! {
231    struct: SnapPlugin,
232    name: "snap",
233    type_id: 15,
234    aliases: ["snapcraft", "snapd"],
235    ecosystem: "Snap packages (Snapcraft / Snap Store)",
236    extensions: [".snap"],
237    description: "Snap application packages (squashfs image)",
238}
239
240skeleton_handler! {
241    struct: AppImagePlugin,
242    name: "appimage",
243    type_id: 16,
244    aliases: ["appdir"],
245    ecosystem: "AppImage portable Linux applications",
246    extensions: [".AppImage", ".appimage"],
247    description: "AppImage self-mounting application bundles (ELF + squashfs)",
248}
249
250skeleton_handler! {
251    struct: ComposerPlugin,
252    name: "composer",
253    type_id: 17,
254    aliases: ["php", "packagist"],
255    ecosystem: "PHP / Composer (Packagist)",
256    extensions: [".zip", ".phar"],
257    description: "Composer/PHP packages (zip with composer.json, or PHAR)",
258}
259
260skeleton_handler! {
261    struct: HexPlugin,
262    name: "hex",
263    type_id: 18,
264    aliases: ["elixir", "erlang", "mix"],
265    ecosystem: "Erlang / Elixir (hex.pm)",
266    extensions: [".tar"],
267    description: "Hex packages (tar of metadata + contents.tar.gz)",
268}
269
270skeleton_handler! {
271    struct: CabalPlugin,
272    name: "cabal",
273    type_id: 19,
274    aliases: ["haskell", "hackage"],
275    ecosystem: "Haskell / Cabal (Hackage)",
276    extensions: [".tar.gz"],
277    description: "Cabal source packages (sdist tarball with a .cabal file)",
278}
279
280skeleton_handler! {
281    struct: SwiftPlugin,
282    name: "swift",
283    type_id: 20,
284    aliases: ["swiftpm", "spm"],
285    ecosystem: "Swift / Swift Package Manager",
286    extensions: [".zip"],
287    description: "Swift package archives (Package.swift + sources)",
288}
289
290skeleton_handler! {
291    struct: ParquetPlugin,
292    name: "parquet",
293    type_id: 21,
294    aliases: ["geoparquet", "pq"],
295    ecosystem: "Apache Parquet / GeoParquet columnar data",
296    extensions: [".parquet", ".geoparquet"],
297    description: "Parquet column files (GeoParquet adds a geo metadata key)",
298}
299
300skeleton_handler! {
301    struct: DatasetPlugin,
302    name: "dataset",
303    type_id: 22,
304    aliases: ["hf", "huggingface", "hub"],
305    ecosystem: "ML datasets (Hugging Face Hub / Croissant)",
306    extensions: [".dataset", ".hf"],
307    description: "Versioned ML dataset bundles (data shards + dataset card)",
308}
309
310skeleton_handler! {
311    struct: ArrowPlugin,
312    name: "arrow",
313    type_id: 23,
314    aliases: ["ipc", "feather"],
315    ecosystem: "Apache Arrow IPC / Feather",
316    extensions: [".arrow", ".feather", ".ipc"],
317    description: "Arrow IPC record-batch files (zero-copy columnar)",
318}
319
320skeleton_handler! {
321    struct: GeoJsonPlugin,
322    name: "geojson",
323    type_id: 24,
324    aliases: ["geo", "gis", "shapefile"],
325    ecosystem: "GIS vector data (GeoJSON / Shapefile / GeoPackage)",
326    extensions: [".geojson", ".gpkg", ".shp", ".fgb"],
327    description: "Geospatial vector datasets (features + CRS)",
328}
329
330/// Every skeleton handler, in `type_id` order. The register in `znippy-cli`
331/// folds these in alongside the native handlers.
332pub fn skeleton_handlers() -> Vec<Box<dyn ArchiveTypePlugin>> {
333    vec![
334        Box::new(GoPlugin),
335        Box::new(NugetPlugin),
336        Box::new(ElfPlugin),
337        Box::new(FlatpakPlugin),
338        Box::new(DockerPlugin),
339        Box::new(HelmPlugin),
340        Box::new(SnapPlugin),
341        Box::new(AppImagePlugin),
342        Box::new(ComposerPlugin),
343        Box::new(HexPlugin),
344        Box::new(CabalPlugin),
345        Box::new(SwiftPlugin),
346        Box::new(ParquetPlugin),
347        Box::new(DatasetPlugin),
348        Box::new(ArrowPlugin),
349        Box::new(GeoJsonPlugin),
350    ]
351}
352
353#[cfg(test)]
354mod tests {
355    use super::*;
356
357    #[test]
358    fn parse_coords_splits_name_and_version() {
359        assert_eq!(
360            parse_coords("foo/bar-1.2.3.nupkg", &[".nupkg"]),
361            ("bar".to_string(), Some("1.2.3".to_string()))
362        );
363        assert_eq!(
364            parse_coords("lodash-4.17.21.tgz", &[".tgz"]),
365            ("lodash".to_string(), Some("4.17.21".to_string()))
366        );
367    }
368
369    #[test]
370    fn parse_coords_handles_no_version() {
371        assert_eq!(
372            parse_coords("/usr/bin/ls.elf", &[".elf"]),
373            ("ls".to_string(), None)
374        );
375    }
376
377    #[test]
378    fn skeletons_have_unique_sequential_type_ids() {
379        let mut ids: Vec<i8> = skeleton_handlers().iter().map(|h| h.type_id()).collect();
380        let mut sorted = ids.clone();
381        sorted.sort();
382        sorted.dedup();
383        assert_eq!(ids.len(), sorted.len(), "type_ids must be unique");
384        ids.sort();
385        assert_eq!(*ids.first().unwrap(), 4, "skeletons start at type_id 4");
386        assert_eq!(*ids.last().unwrap(), 24);
387    }
388
389    #[test]
390    fn every_skeleton_matches_and_extracts() {
391        for h in skeleton_handlers() {
392            let m = h.meta();
393            assert!(!m.extensions.is_empty(), "{} has no extensions", m.name);
394            let sample = format!("pkg-1.0{}", m.extensions[0]);
395            assert!(h.matches_path(&sample), "{} should match {}", m.name, sample);
396            let row = h.extract_metadata(&sample, &[]).expect("row");
397            assert!(row.fields.contains_key("name"));
398        }
399    }
400}