Documentation
¶
Index ¶
- func EnrichNodes(client *CratesIOClient, nodes map[PackageKey]*sbom.Node)
- func NewPackageNode(name, version, checksum string) *sbom.Node
- func ParseDependencyRef(dep string) (name, version string)
- func ReadMemberManifests(dir string, root *CargoToml) map[string]*CargoToml
- type CargoLock
- type CargoToml
- type CratesIOClient
- type Decomposer
- func (d *Decomposer) DefaultOptions() any
- func (d *Decomposer) Extract(opts *api.DecomposerOptions) (*sbom.NodeList, error)
- func (d *Decomposer) ExtractWithContext(ctx context.Context, opts *api.DecomposerOptions) (*sbom.NodeList, error)
- func (d *Decomposer) FindCodeBases(index *code.PathIndex) ([]string, error)
- func (d *Decomposer) GenerationContext() api.GenerationContext
- func (d *Decomposer) Requirements(_ *api.DecomposerOptions) []api.Requirement
- type DependencySpec
- type DependencyTree
- func (dt *DependencyTree) Build(opts *api.DecomposerOptions) (*sbom.NodeList, error)
- func (dt *DependencyTree) Enrich(client *CratesIOClient)
- func (dt *DependencyTree) GetDependencyCount() int
- func (dt *DependencyTree) GetDirectDependencyCount() int
- func (dt *DependencyTree) ListDependencies() []string
- func (dt *DependencyTree) SetMemberManifests(members map[string]*CargoToml)
- type DependencyType
- type LockPackage
- type Options
- type PackageInfo
- type PackageKey
- type TargetDeps
- type WorkspaceInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnrichNodes ¶ added in v0.3.3
func EnrichNodes(client *CratesIOClient, nodes map[PackageKey]*sbom.Node)
EnrichNodes fetches metadata from crates.io for the given crate nodes in parallel and populates license, description, homepage, repository, documentation, crate size and MSRV on them. Nodes for crates that are not on crates.io should be left out: the lookup would only fail.
func NewPackageNode ¶ added in v0.3.3
NewPackageNode builds the node for a crate at a version, the way every Rust reader renders one: a pkg:cargo purl, the crates.io download URL and the library purpose, plus the crate's SHA-256 when known. Readers of other Cargo data, such as the dependency list embedded in a binary, use it so their packages look the same as the source decomposer's.
func ParseDependencyRef ¶
ParseDependencyRef parses a dependency reference string from Cargo.lock. Dependencies can be in formats: - "name" (simple, when only one version exists) - "name version" (when multiple versions might exist) - "name version (source)" (with explicit source)
func ReadMemberManifests ¶ added in v0.4.0
ReadMemberManifests reads the manifests of the local packages of the project rooted at dir, other than the root manifest itself: the members its workspace table lists, patterns expanded, and whatever those and the root require by path, as long as it sits inside dir. They are returned by the name of the package each describes.
A member's own manifest is what says which of its dependencies are for development or for building, which Cargo.lock does not record. A manifest that cannot be read or parsed is skipped: its package's dependencies are then all taken as normal ones.
Types ¶
type CargoLock ¶
type CargoLock struct {
Version int `toml:"version"`
Packages []LockPackage `toml:"package"`
Metadata toml.Primitive `toml:"metadata"` // Ignored but captured to avoid parse errors
}
CargoLock represents the structure of a Cargo.lock file.
func ParseCargoLock ¶
ParseCargoLock parses a Cargo.lock file from the given directory.
func ParseCargoLockData ¶
ParseCargoLockData parses Cargo.lock content from bytes.
func (*CargoLock) BuildPackageIndex ¶
func (cl *CargoLock) BuildPackageIndex() (byKey map[PackageKey]*LockPackage, byName map[string][]*LockPackage)
BuildPackageIndex creates a map from package key to package for quick lookups. It also creates an index by name only for packages with unique names.
func (*CargoLock) FindRootPackages ¶
func (cl *CargoLock) FindRootPackages() []*LockPackage
type CargoToml ¶
type CargoToml struct {
Package PackageInfo `toml:"package"`
Dependencies map[string]DependencySpec `toml:"dependencies"`
DevDependencies map[string]DependencySpec `toml:"dev-dependencies"`
BuildDependencies map[string]DependencySpec `toml:"build-dependencies"`
Target map[string]TargetDeps `toml:"target"`
Workspace *WorkspaceInfo `toml:"workspace"`
// contains filtered or unexported fields
}
CargoToml represents the structure of a Cargo.toml file.
func ParseCargoToml ¶
ParseCargoToml parses a Cargo.toml file from the given directory.
func ParseCargoTomlData ¶
ParseCargoTomlData parses Cargo.toml content from bytes.
func (*CargoToml) GetDirectDependencies ¶
func (ct *CargoToml) GetDirectDependencies() map[string]DependencyType
GetDirectDependencies returns a map of dependency names to their types. This includes dependencies from all sections and target-specific dependencies.
The names are the crates', which is how Cargo.lock refers to them: a dependency declared under another name is listed by the crate it renames.
A crate may be declared in more than one section, and the map holds one type per name: the one that puts the crate furthest into what is built. A normal dependency is part of the package whatever else it is used for, and a build dependency is compiled to build it, where a dev dependency only serves its tests.
type CratesIOClient ¶ added in v0.2.1
type CratesIOClient struct {
Agent *khttp.Agent
BaseURL string
// RequestInterval is the least time between the start of one request
// and the start of the next. It defaults to what crates.io asks of
// automated clients; another registry speaking the same API may
// allow less.
RequestInterval time.Duration
// Logger is where the client says what went wrong with its requests.
// A decomposer sets it to its caller's; left nil, nothing is logged.
Logger *clog.Logger
}
CratesIOClient fetches crate metadata from the crates.io API.
func NewCratesIOClient ¶ added in v0.2.1
func NewCratesIOClient(concurrency int) *CratesIOClient
NewCratesIOClient creates a new client for the crates.io API.
The concurrency is kept for the callers that state one. It bounds the agent's parallel requests, of which FetchAll makes none: crates.io is asked one crate at a time (see RequestInterval).
func (*CratesIOClient) FetchAll ¶ added in v0.2.1
func (c *CratesIOClient) FetchAll(packages []PackageKey) map[PackageKey]*crateVersion
FetchAll fetches metadata for multiple crates. Returns a map of PackageKey to parsed metadata. A crate whose request fails is skipped.
The requests go one after the other, RequestInterval apart: the registry's limit is on how often it is asked, so sending several at once only gets them refused. A lockfile of some hundred crates therefore takes minutes to enrich. When several requests in a row fail the rest are not sent.
type Decomposer ¶
type Decomposer struct{}
func New ¶
func New() *Decomposer
func (*Decomposer) DefaultOptions ¶
func (d *Decomposer) DefaultOptions() any
DefaultOptions returns the default options for the Rust decomposer.
func (*Decomposer) Extract ¶
func (d *Decomposer) Extract(opts *api.DecomposerOptions) (*sbom.NodeList, error)
Extract is ExtractWithContext with a background context, for a caller that has none to give.
func (*Decomposer) ExtractWithContext ¶ added in v0.4.1
func (d *Decomposer) ExtractWithContext(ctx context.Context, opts *api.DecomposerOptions) (*sbom.NodeList, error)
ExtractWithContext parses Cargo.toml and Cargo.lock files and builds the complete dependency graph as a protobom NodeList.
func (*Decomposer) FindCodeBases ¶
func (d *Decomposer) FindCodeBases(index *code.PathIndex) ([]string, error)
FindCodeBases locates Rust codebases by looking for Cargo.lock files.
func (*Decomposer) GenerationContext ¶ added in v0.4.1
func (d *Decomposer) GenerationContext() api.GenerationContext
GenerationContext says in what context the decomposer's data is read: Cargo.lock and Cargo.toml, as they stand in the source tree.
func (*Decomposer) Requirements ¶
func (d *Decomposer) Requirements(_ *api.DecomposerOptions) []api.Requirement
Requirements returns the requirements for the decomposer. No external binary required - uses pure Go implementation.
type DependencySpec ¶
type DependencySpec struct {
Version string `toml:"version"`
Path string `toml:"path"`
Git string `toml:"git"`
Branch string `toml:"branch"`
Tag string `toml:"tag"`
Rev string `toml:"rev"`
Features []string `toml:"features"`
Optional bool `toml:"optional"`
// Package is the crate's real name when the dependency is declared
// under another: tmp = { package = "tempfile" }. The table's key is
// then only what the code calls it; the lock knows the crate by this
// name.
Package string `toml:"package"`
// contains filtered or unexported fields
}
DependencySpec represents a dependency specification in Cargo.toml. Dependencies can be specified in multiple formats: - Simple: "1.0" - Table: { version = "1.0", features = ["foo"] }
func (*DependencySpec) GetVersion ¶
func (ds *DependencySpec) GetVersion() string
GetVersion returns the version specification.
func (*DependencySpec) IsGit ¶
func (ds *DependencySpec) IsGit() bool
IsGit returns true if this is a git dependency.
func (*DependencySpec) IsLocalPath ¶
func (ds *DependencySpec) IsLocalPath() bool
IsLocalPath returns true if this is a path dependency.
func (*DependencySpec) UnmarshalTOML ¶
func (ds *DependencySpec) UnmarshalTOML(data any) error
UnmarshalTOML implements custom unmarshaling for dependency specs.
type DependencyTree ¶
type DependencyTree struct {
// contains filtered or unexported fields
}
DependencyTree builds a complete dependency graph from Cargo files.
func NewDependencyTree ¶
func NewDependencyTree(toml *CargoToml, lock *CargoLock, opts *Options) *DependencyTree
NewDependencyTree creates a new dependency tree from parsed Cargo files.
func (*DependencyTree) Build ¶
func (dt *DependencyTree) Build(opts *api.DecomposerOptions) (*sbom.NodeList, error)
Build constructs the complete dependency graph as a protobom NodeList.
func (*DependencyTree) Enrich ¶ added in v0.2.1
func (dt *DependencyTree) Enrich(client *CratesIOClient)
Enrich fetches metadata from crates.io for all cached dependency nodes and populates license, description, homepage, repository, documentation, and other fields on the protobom nodes.
func (*DependencyTree) GetDependencyCount ¶
func (dt *DependencyTree) GetDependencyCount() int
GetDependencyCount returns the total number of dependencies.
func (*DependencyTree) GetDirectDependencyCount ¶
func (dt *DependencyTree) GetDirectDependencyCount() int
GetDirectDependencyCount returns the number of direct dependencies.
func (*DependencyTree) ListDependencies ¶
func (dt *DependencyTree) ListDependencies() []string
ListDependencies returns a sorted list of all dependencies for debugging/testing.
func (*DependencyTree) SetMemberManifests ¶ added in v0.4.0
func (dt *DependencyTree) SetMemberManifests(members map[string]*CargoToml)
SetMemberManifests gives the tree the manifests of the project's local packages other than the one the root Cargo.toml describes, by package name (see ReadMemberManifests). With them, the dependencies of a workspace member are typed by what the member itself declares; without, they are all taken as normal dependencies.
type DependencyType ¶
type DependencyType int
DependencyType represents the type of dependency relationship.
const ( DepTypeNormal DependencyType = iota DepTypeDev DepTypeBuild )
func (DependencyType) String ¶
func (dt DependencyType) String() string
String returns a string representation of the dependency type.
type LockPackage ¶
type LockPackage struct {
Name string `toml:"name"`
Version string `toml:"version"`
Source string `toml:"source,omitempty"`
Checksum string `toml:"checksum,omitempty"`
Dependencies []string `toml:"dependencies,omitempty"`
}
LockPackage represents a package entry in Cargo.lock.
func ResolveDependency ¶
func ResolveDependency(dep string, byKey map[PackageKey]*LockPackage, byName map[string][]*LockPackage) (*LockPackage, error)
ResolveDependency resolves a dependency reference to a specific package. If version is empty and multiple versions exist, it returns the first one found.
type Options ¶
type Options struct {
// IncludeDevDependencies includes dev dependencies in the output.
IncludeDevDependencies bool
// IncludeBuildDependencies includes build dependencies in the output.
IncludeBuildDependencies bool
// Concurrency controls the number of parallel HTTP requests to crates.io (default: 10).
Concurrency int
}
Options configures the Rust dependency extraction.
type PackageInfo ¶
type PackageInfo struct {
Name string `toml:"name"`
Version string `toml:"version"`
Edition string `toml:"edition"`
Description string `toml:"description"`
License string `toml:"license"`
Homepage string `toml:"homepage"`
Repository string `toml:"repository"`
}
PackageInfo contains package metadata from Cargo.toml.
type PackageKey ¶
PackageKey uniquely identifies a package by name and version.
func (PackageKey) String ¶
func (pk PackageKey) String() string
type TargetDeps ¶
type TargetDeps struct {
Dependencies map[string]DependencySpec `toml:"dependencies"`
DevDependencies map[string]DependencySpec `toml:"dev-dependencies"`
BuildDependencies map[string]DependencySpec `toml:"build-dependencies"`
}
TargetDeps represents target-specific dependencies.
type WorkspaceInfo ¶
type WorkspaceInfo struct {
Members []string `toml:"members"`
Dependencies map[string]DependencySpec `toml:"dependencies"`
}
WorkspaceInfo represents workspace configuration.