包 ID 规范
包 ID 规范
Cargo 的子命令经常需要引用依赖图中的特定包,以便进行更新、清理、构建等各种操作。为了解决这个问题,Cargo 支持包 ID 规范。规范是一个字符串,用于在包图中唯一引用一个包。
规范可以是完全限定的,例如 https://github.com/rust-lang/crates.io-index#[email protected]
,也可以是简写的,例如 regex
。只要简写形式能够唯一标识依赖图中的单个包,就可以使用简写形式。如果存在歧义,可以添加额外的限定符使其唯一。例如,如果图中存在两个版本的 regex
包,则可以使用版本来限定它,使其唯一,例如 [email protected]
。
规范语法
包 ID 规范的形式语法如下:
spec := pkgname |
[ kind "+" ] proto "://" hostname-and-path [ "?" query] [ "#" ( pkgname | semver ) ]
query = ( "branch" | "tag" | "rev" ) "=" ref
pkgname := name [ ("@" | ":" ) semver ]
semver := digits [ "." digits [ "." digits [ "-" prerelease ] [ "+" build ]]]
kind = "registry" | "git" | "file"
proto := "http" | "git" | ...
其中,方括号表示内容是可选的。
URL 形式可以用于 git 依赖,或者区分来自不同来源(例如不同注册表)的包。
示例规范
以下是对 crates.io
上的 regex
包的引用:
规范 | 名称 | 版本 |
---|---|---|
regex | regex | * |
[email protected] | regex | 1.4.* |
[email protected] | regex | 1.4.3 |
https://github.com/rust-lang/crates.io-index#regex | regex | * |
https://github.com/rust-lang/crates.io-index#[email protected] | regex | 1.4.3 |
registry+https://github.com/rust-lang/crates.io-index#[email protected] | regex | 1.4.3 |
以下是一些针对不同 git 依赖的规范示例:
规范 | 名称 | 版本 |
---|---|---|
https://github.com/rust-lang/cargo#0.52.0 | cargo | 0.52.0 |
https://github.com/rust-lang/cargo#[email protected] | cargo-platform | 0.1.2 |
ssh://[email protected]/rust-lang/regex.git#[email protected] | regex | 1.4.3 |
git+ssh://[email protected]/rust-lang/regex.git#[email protected] | regex | 1.4.3 |
git+ssh://[email protected]/rust-lang/regex.git?branch=dev#[email protected] | regex | 1.4.3 |
文件系统上的本地包可以使用 file://
URL 来引用它们:
规范 | 名称 | 版本 |
---|---|---|
file:///path/to/my/project/foo | foo | * |
file:///path/to/my/project/foo#1.1.8 | foo | 1.1.8 |
path+file:///path/to/my/project/foo#1.1.8 | foo | 1.1.8 |
规范的简洁性
其目标是为在依赖图中引用包提供简洁和详尽的语法。模糊的引用可能指向一个或多个包。如果同一个规范可以引用多个包,大多数命令会生成错误。