在需要验证 Rust Cargo.toml 清单的依赖问题、缺失元数据、功能冲突、工作区配置以及 crates.io 发布准备情况时使用。
货物.toml验证器是一项面向实际任务的技能,主要用于Validate Rust Cargo.toml 显示文件的结构正确性、依赖性卫生、特性配置、工作空间设置和箱.io发布准备。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
验证 Rust Cargo.toml 清单文件的结构正确性、依赖健康度、功能(feature)配置、工作区(workspace)设置,以及是否符合 crates.io 发布要求。使用 Python 3.11+ 内置的 tomllib 进行原生 TOML 解析 —— 无需任何外部依赖。
python3 scripts/cargo_toml_validator.py validate Cargo.toml python3 scripts/cargo_toml_validator.py validate Cargo.toml --strict python3 scripts/cargo_toml_validator.py validate Cargo.toml --format json
python3 scripts/cargo_toml_validator.py check Cargo.toml python3 scripts/cargo_toml_validator.py check Cargo.toml --format summary
python3 scripts/cargo_toml_validator.py explain Cargo.toml python3 scripts/cargo_toml_validator.py explain Cargo.toml --format json
python3 scripts/cargo_toml_validator.py suggest Cargo.toml python3 scripts/cargo_toml_validator.py suggest Cargo.toml --format json
| 标志 | 说明 |
|---|---|
--strict |
将警告视为错误 — 返回退出码 1(适用于 CI 环境) |
--format text |
人类可读格式输出(默认) |
--format json |
机器可读的 JSON 格式输出 |
--format summary |
精简摘要格式(含各类问题数量统计) |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| S1 | error | 文件未找到或不可读 |
| S2 | error | 空文件 |
| S3 | error | TOML 语法无效 |
| S4 | error/warning | 缺少 [package] 区块(对二进制/库项目为 error;对虚拟工作区为 warning) |
| S5 | error/warning | 缺少必需字段:name、version(error),edition(warning) |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| M1 | warning | 缺少 edition 字段(默认为 2015) |
| M2 | info | 使用的 edition 已过时(2015/2018,而 2021/2024 可用) |
| M3 | warning | 未指定 license 或 license-file(影响 crates.io 发布) |
| M4 | warning | 未指定 description(影响 crates.io 发布) |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| D1 | error | 使用通配符版本号 * |
| D2 | warning | 未锁定版本的依赖(无版本限定符) |
| D3 | warning | Git 依赖未通过 rev/tag/branch 显式锁定 |
| D4 | info | 路径依赖(path dependency)—— 将阻断 crates.io 发布 |
| D5 | warning | 同一依赖同时出现在 [dependencies] 和 [dev-dependencies] 中,且版本不一致 |
| D6 | info | 使用了已弃用的 crate 名称(如 failure、error-chain、iron、rustc-serialize,以及旧版 hyper/tokio/actix-web/rocket/time) |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| F1 | error | Feature 启用了不存在的依赖项 |
| F2 | warning | 空 Feature(不含依赖项或子 Feature) |
| F3 | error | Feature 存在循环依赖 |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| W1 | warning | [workspace] 区块未定义 members |
| W2 | info | 同时存在 [package] 和无 members 的 [workspace](语义模糊) |
| W3 | info | 检测到 [workspace.dependencies] — 提示成员应设置 workspace = true |
| 规则 | 严重程度 | 说明 |
|---|---|---|
| B1 | info | 已发布 crate 缺少文档链接(documentation 字段) |
| B2 | info | 存在构建脚本(build script),但未声明 [build-dependencies] |
| B3 | info | 依赖数量过多(>30 个) |
| B4 | info | 缺少仓库(repository)或主页(homepage)URL |
| 码值 | 含义 |
|---|---|
| 0 | 无错误(允许存在警告,除非启用 --strict) |
| 1 | 发现错误(或在 --strict 模式下出现警告) |
| 2 | 文件未找到 / 解析失败 |
Cargo.toml validate — Cargo.toml
=================================
[ERROR ] S5: Missing required field: package.version
Set version directly or use version.workspace = true.
[WARNING] M1: Missing edition field — defaults to 2015
Add edition = "2021" or edition = "2024" to [package].
[WARNING] D3: Git dependency 'my-fork' in [dependencies] is not pinned
Pin to a rev, tag, or branch for reproducibility. URL: https://github.com/user/fork
[INFO ] D4: Path dependency 'utils' in [dependencies] — blocks crates.io publish
Path: ../utils. Fine for local dev, but won't work on crates.io.
[INFO ] B4: Missing repository and homepage URL
Add repository = "https://github.com/..." to [package] for crates.io visibility.
Result: INVALID
Summary: 1 error(s), 2 warning(s), 2 info
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章