pub trait LockfileParser {
// Required methods
fn parse(&self, lockfile_path: &Path) -> Result<Vec<LockfileEntry>>;
fn supports_lockfile(&self, path: &Path) -> bool;
fn lockfile_name(&self) -> &str;
}Expand description
Parses package manager-specific lockfiles into structured entries.
Each package manager has its own lockfile format:
- npm:
package-lock.json - Bun:
bun.lock(JSONC format) - pnpm:
pnpm-lock.yaml - Yarn Classic:
yarn.lock - Yarn Modern:
yarn.lock(different format) - Cargo:
Cargo.lock
Implementations handle the parsing logic for these formats.
§Example
ⓘ
use cuenv_workspaces::{LockfileParser, LockfileEntry};
use std::path::Path;
struct NpmLockfileParser;
impl LockfileParser for NpmLockfileParser {
fn parse(&self, lockfile_path: &Path) -> Result<Vec<LockfileEntry>> {
// Parse package-lock.json and convert to LockfileEntry structs
todo!()
}
fn supports_lockfile(&self, path: &Path) -> bool {
path.file_name()
.and_then(|n| n.to_str())
.map(|n| n == "package-lock.json")
.unwrap_or(false)
}
fn lockfile_name(&self) -> &str {
"package-lock.json"
}
}Required Methods§
Sourcefn parse(&self, lockfile_path: &Path) -> Result<Vec<LockfileEntry>>
fn parse(&self, lockfile_path: &Path) -> Result<Vec<LockfileEntry>>
Parses a lockfile into structured entries.
Each entry represents a resolved dependency with its version, source, checksum, and direct dependencies.
§Errors
Returns an error if:
- The lockfile cannot be read
- The lockfile format is invalid or corrupted
- Required fields are missing
Sourcefn supports_lockfile(&self, path: &Path) -> bool
fn supports_lockfile(&self, path: &Path) -> bool
Checks if this parser supports the given lockfile path.
This is typically a simple filename check, but may involve inspecting file contents for formats that can’t be distinguished by name alone.
Sourcefn lockfile_name(&self) -> &str
fn lockfile_name(&self) -> &str
Returns the expected lockfile name for this parser.
For example: "package-lock.json", "Cargo.lock", "pnpm-lock.yaml".