Cargo (crates.io) Registry Adapter
Relevant source files
The following files were used as context for generating this wiki page:
The Cargo Registry Adapter is a specialized implementation within the regxa project designed to interface with the crates.io API. Its primary purpose is to fetch, normalize, and expose metadata for Rust crates, ensuring that ecosystem-specific data is translated into the universal Package, Version, Dependency, and Maintainer formats used throughout the library.
By implementing the core Registry contract, the adapter allows users to query the Rust ecosystem using standardized Package URLs (PURLs). It handles the complexities of the crates.io REST API, including version-specific dependency resolution, owner information retrieval, and specific metadata mapping such as crate size and download statistics.
Sources: src/registries/cargo.ts:1-248, src/registries/AGENTS.md:1-12
Architecture and Components
The adapter is built around the CargoRegistry class, which implements the Registry interface. It utilizes a centralized Client for all HTTP communication, ensuring consistent retry logic and error handling across the project.
Core Structure
The following class diagram illustrates the relationship between the Cargo adapter and the core registry system.
The CargoRegistry is instantiated via a factory function and registered globally under the "cargo" key using the register utility. This allows the factory to be invoked whenever a PURL of type pkg:cargo/ is processed.
Sources: src/registries/cargo.ts:114-118, src/registries/cargo.ts:244-248, src/core/registry.ts
Data Normalization Logic
One of the adapter's most critical roles is transforming raw crates.io JSON responses into normalized TypeScript interfaces.
Package Metadata Mapping
When fetchPackage is called, the adapter retrieves data from /api/v1/crates/{name}. It prioritizes the max_stable_version for determining the "latest" version to avoid returning pre-release or alpha metadata by default.
| Normalized Field | Source Data (crates.io) | Logic/Normalization |
|---|---|---|
name | crate.name | Direct mapping |
repository | crate.repository | Cleaned via normalizeRepositoryURL |
licenses | version.license | Extracted from the latest stable version and normalized |
latestVersion | max_stable_version | Falls back to max_version if no stable exists |
metadata.downloads | crate.downloads | Total crate downloads |
Sources: src/registries/cargo.ts:133-162, test/unit/registries.test.ts:199-231
Version and Status
The adapter maps crate versions to the Version interface. A key feature is the handling of "yanked" crates. If the yanked boolean is true in the crates.io response, the normalized status field is set to "yanked".
The integrity hash is constructed by prefixing the crates.io checksum with sha256-.
Sources: src/registries/cargo.ts:178-193, test/unit/registries.test.ts:316-322
Dependency and Maintainer Retrieval
The adapter interacts with specific crates.io sub-resources to provide deep metadata.
Dependency Scopes
Cargo dependency types (kind) are mapped to the project's universal scope union:
devdependencies becomedevelopment.builddependencies becomebuild.- All others default to
runtime.
Sources: src/registries/cargo.ts:208-223
Maintainers (Owners)
Maintainers are fetched from the /owner_user endpoint. The adapter maps crate "owners" to the Maintainer interface, using the crates.io user ID as the uuid.
| Cargo Field | Maintainer Interface |
|---|---|
id | uuid (stringified) |
login | login |
name | name |
url | url |
Sources: src/registries/cargo.ts:232-241
URL Building Logic
The urls() method provides a URLBuilder that generates canonical links for the Cargo ecosystem.
// Example from src/registries/cargo.ts:251-267
urls(): URLBuilder {
return {
registry: (name: string, version?: string) => {
const base = `https://crates.io/crates/${name}`;
return version ? `${base}/${version}` : base;
},
download: (name: string, version: string) => {
return `https://crates.io/api/v1/crates/${name}/${version}/download`;
},
documentation: (name: string, version?: string) => {
return version ? `https://docs.rs/${name}/${version}` : `https://docs.rs/${name}`;
},
purl: (name: string, version?: string) => {
return buildPURL({ type: "cargo", name, version });
},
};
}
Sources: src/registries/cargo.ts:251-274
Summary
The Cargo Registry Adapter acts as a robust bridge between the Rust package ecosystem and the universal metadata requirements of the project. By handling crates.io-specific nuances—such as stable version prioritization, yanked status mapping, and repository URL normalization—it provides a seamless experience for users interacting with Rust crates through PURLs. Its architecture remains decoupled from other registries, relying solely on the core contracts and the centralized HTTP client.