Cargo.toml (Rust) to JSON Conversion
Convert Rust's Cargo.toml manifest file to JSON and understand its structure. Covers package metadata, dependencies, features, and workspace configuration.
Detailed Explanation
Cargo.toml is the manifest file for Rust projects, managed by the Cargo package manager. It is one of the most well-known real-world uses of TOML. Understanding its JSON equivalent is useful for tooling, CI pipelines, and programmatic dependency management.
A typical Cargo.toml:
[package]
name = "my-app"
version = "0.1.0"
edition = "2021"
authors = ["Alice <alice@example.com>"]
description = "A sample Rust application"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
reqwest = "0.11"
[dev-dependencies]
criterion = "0.5"
[features]
default = ["logging"]
logging = ["serde/std"]
[[bin]]
name = "my-app"
path = "src/main.rs"
Converts to JSON:
{
"package": {
"name": "my-app",
"version": "0.1.0",
"edition": "2021",
"authors": ["Alice <alice@example.com>"],
"description": "A sample Rust application"
},
"dependencies": {
"serde": {"version": "1.0", "features": ["derive"]},
"tokio": {"version": "1", "features": ["full"]},
"reqwest": "0.11"
},
"dev-dependencies": {
"criterion": "0.5"
},
"features": {
"default": ["logging"],
"logging": ["serde/std"]
},
"bin": [
{
"name": "my-app",
"path": "src/main.rs"
}
]
}
Notable conversion details:
- Dependencies can be either a simple string (
"0.11") or an inline table with version and features. Both forms are valid TOML and produce different JSON structures. [[bin]]uses the array of tables syntax, creating a JSON array of objects under thebinkey.- Features are arrays of strings referencing other features or dependency features (
"serde/std"). - Hyphenated keys like
dev-dependenciesare valid bare keys in TOML and become regular JSON string keys.
Cargo reads only TOML, but the JSON representation is used by cargo metadata, crate registries (crates.io API), and build tools that analyze dependency graphs programmatically.
Use Case
Building a dependency analysis tool that reads Cargo.toml files, converts them to JSON for processing, and generates reports on dependency versions, feature flags, and potential security vulnerabilities.