/** * @file `parseCargoLock(content)` — parses a Rust `Cargo.lock` (v1/v2/v3/v4) * into a `ParsedLockfile`. Cargo.lock uses a constrained TOML dialect: a * top-level `version = N` scalar plus repeating `[[package]]` array-of-table * entries. We line-scan it instead of pulling in a full TOML parser — the * spec for the lockfile is stable and small (`name`, `version`, `source`, * `checksum`, `dependencies = [ ... ]`), and a hand-rolled scanner is ~100× * cheaper than dragging in `@iarna/toml` for one use case. `dependencies` * entries come in two forms (cargo strips redundant versions when they're * unambiguous): * * - `"name 1.2.3"` — name + space-separated version * - `"name"` — name only, used when that crate has just one version in the * graph * - `"name 1.2.3 (registry+…)"` — name + version + source spec * `parseGitUrl`-style detection here treats `source = "git+…"` as the git * VCS source, with `#` as the commit pin. The parser is forgiving — * unknown keys ignored, missing fields default to empty. It never throws. * Source material (in lock-step order, newest → oldest): * * 1. **C++ native parser** in socket-btm/node-smol-builder: * additions/source-patched/src/socketsecurity/manifest/parser_cargo.cc * Same algorithm — keep the two in lock-step. * 2. **socket-sdxgen** — algorithm oracle, broader coverage: * socket-sdxgen/src/parsers/cargo/index.mts (851 lines) * 3. **cdxgen** (pinned v11.11.0): * https://github.com/CycloneDX/cdxgen/blob/v11.11.0/lib/parsers/rust.js * (parseCargoLock) * 4. **Cargo's own lockfile encoder** — the source of truth for the format we're * parsing: * https://github.com/rust-lang/cargo/blob/0.86.0/src/cargo/core/resolver/encode.rs * Lockfile format docs: * https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html * https://doc.rust-lang.org/cargo/reference/resolver.html#lockfile-format * Regression guard: * * - `[[patch.unused]]` blocks must NOT materialize as PackageRefs. Only * `[[package]]` opens an entry; any other section header (including * `[[patch.unused]]`, `[metadata]`, `[patch.crates-io]`, …) closes the * current entry to undefined. See the fixture under socket-btm's * test/fixtures/sdxgen-bug-regressions/ cargo-patch-unused-no-leak/. */ import type { PackageRef, ParsedLockfile } from '../manifest/types.js'; export type PackageIndex = Record; export interface CargoEntryState { name: string; version: string; source: string | undefined; checksum: string | undefined; dependencies: string[]; inDependencies: boolean; } export declare function addToCargoIndex(packageIndex: PackageIndex, name: string, idx: number): void; /** * Strip Cargo's `name version (source)` dependency entry down to just the crate * name. The version/source are advisory for cycle-breaking and not part of * `PackageRef.dependencies` (which is a flat list of names, matching the * npm/yarn/pnpm convention). */ export declare function extractCargoDepName(entry: string): string; export declare function freezeCargoEntry(entry: CargoEntryState): PackageRef; export declare function jsParseCargoLock(content: string): ParsedLockfile; export declare function newCargoEntry(): CargoEntryState; export declare function parseCargoGitSource(source: string): { url: string; commit: string | undefined; } | undefined; /** * Parse a TOML array of strings on a single line: `dependencies = [ "foo 1.0", * "bar 2.0" ]`. Returns the strings as raw entries (each callsite runs * `extractCargoDepName`). */ export declare function parseInlineArray(value: string): string[]; /** * Strip outer double-quotes from a TOML string value. */ export declare function stripTomlString(value: string): string; /** * Read the value half of a `key = value` TOML line. Returns the raw value text * with quotes left intact; callers run `stripTomlString` if they want the * inner string. */ export declare function valueAfterEquals(line: string): string; export declare const parseCargoLock: (content: string) => ParsedLockfile;