Skip to main content

Crate berry

Crate berry 

Source
Expand description

§Berry

A fast parser for Yarn Berry (v2+) lockfiles.

let src = concat!(
  "# This file is generated by running \"yarn install\" inside your project.\n",
  "# Manual changes might be lost - proceed with caution!\n\n",
  "__metadata:\n  version: 8\n  cacheKey: 10\n\n",
  "\"left-pad@npm:^1.3.0\":\n  version: 1.3.0\n  resolution: \"left-pad@npm:1.3.0\"\n",
  "  dependencies:\n    \"@scope/dep\": \"npm:^2.0.0\"\n  languageName: node\n  linkType: hard\n",
);
let lockfile = berry::parse(src)?;
assert_eq!(lockfile.metadata().version, "8");
assert!(lockfile.remaining().is_empty());
for entry in &lockfile {
  assert_eq!(entry.descriptors().first().unwrap().to_string(), "left-pad@npm:^1.3.0");
  assert_eq!(entry.version(), Some("1.3.0"));
  let dep = entry.dependencies().get("@scope/dep").unwrap();
  assert_eq!(dep.ident().scope(), Some("@scope"));
  assert_eq!(dep.range(), "npm:^2.0.0");
}

The parse produces span tables (u32 offsets into the text) instead of owned strings or maps: a constant number of allocations, independent of the number of entries, and none when Buffers are reused. Every accessor is a borrowed, Copy view.

Inputs: parse (&str), parse_bytes (&[u8], validated as UTF-8) and parse_file (a path; memory-mapped or read, see its docs for the concurrent-modification contract of mapped files: Yarn rewrites yarn.lock in place), each with a _with variant that reuses Buffers.

Inputs of 2.5 MB and more are parsed in parallel on the current rayon pool; the result is always identical to a sequential parse. RAYON_NUM_THREADS=1 (or a one-thread ThreadPool::install) makes every parse sequential. A parse called from inside a rayon pool whose workers are all busy with long tasks waits for them; latency-sensitive callers should call it from outside the pool.


This project is not affiliated with Yarn or the Yarn team.

Re-exports§

pub use ident::Descriptor;
pub use ident::Ident;
pub use ident::Locator;
pub use ident::Protocol;
pub use ident::Range;

Modules§

ident
Package names, descriptors, ranges and locators.

Structs§

BinIter
Iterator over a Bins.
Bins
bin lines (name, path) (EntryView::bin), in source order.
Buffers
Reusable storage for crate::parse_with: the span tables of a previous parse, kept with their capacity. Parsing into recycled buffers of a similar lockfile makes no allocation at all.
Dependencies
dependencies or peerDependencies lines (EntryView::dependencies), in source order, each as a Descriptor (ident() is the key, range() the value).
DependenciesMeta
dependenciesMeta items (EntryView::dependencies_meta), in source order.
DependencyIter
Iterator over a Dependencies.
DependencyLookup
Name-sorted lookup table over one list of dependency lines (Dependencies::lookup). Holds the last occurrence of each name.
DependencyMeta
One dependenciesMeta value (Manifest.ts).
DependencyMetaIter
Iterator over a DependenciesMeta.
DescriptorIter
Iterator over a Descriptors.
Descriptors
An entry’s descriptors (EntryView::descriptors).
Entries
Iterator over the entries of a Lockfile, in file order.
EntryView
One entry of a Lockfile: its descriptors (the key line) and its package fields.
Error
A parse error: kind plus byte offset into the input.
LastWins
Iterator returned by Dependencies::last_wins.
Lockfile
A parsed Yarn Berry lockfile.
Metadata
__metadata of a lockfile.
PeerDependenciesMeta
peerDependenciesMeta items (EntryView::peer_dependencies_meta), in source order.
PeerDependencyMeta
One peerDependenciesMeta value.
PeerDependencyMetaIter
Iterator over a PeerDependenciesMeta.

Enums§

ErrorKind
What went wrong.
FileError
Error of crate::parse_file.
LinkType
The type of link to use for a package.

Functions§

parse
Parse a lockfile.
parse_bytes
parse for input given as bytes, e.g. a file read with std::fs::read.
parse_bytes_with
parse_bytes reusing the storage in buffers (see parse_with).
parse_file
Read (or memory-map) and parse a lockfile file.
parse_file_with
parse_file reusing the storage in buffers: the span tables and, for files that are read rather than mapped, the read buffer.
parse_with
parse reusing the storage in buffers (taken out of it; get it back with Lockfile::into_buffers). On error the storage stays in buffers.