cargo_lock/lib.rs
1#![doc = include_str!("../README.md")]
2#![doc(html_logo_url = "https://raw.githubusercontent.com/RustSec/logos/main/rustsec-logo-lg.png")]
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4#![forbid(unsafe_code)]
5#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]
6
7//! # Usage
8//!
9//! ```
10//! use cargo_lock::Lockfile;
11//!
12//! let lockfile = Lockfile::load("tests/examples/Cargo.lock").unwrap();
13//! println!("number of dependencies: {}", lockfile.packages.len());
14//! ```
15//!
16//! # Dependency tree API
17//!
18//! When the `dependency-tree` feature of this crate is enabled, it supports
19//! computing a directed graph of the dependency tree expressed in the
20//! lockfile, modeled using the [`petgraph`] crate, along with support for
21//! printing dependency trees ala the [`cargo-tree`] crate, a CLI interface
22//! for which is provided by the `cargo lock tree` subcommand described above.
23//!
24//! This same graph representation of a `Cargo.lock` file is programmatically
25//! available via this crate's API.
26//!
27//! # Command Line Interface
28//!
29//! This crate provides a `cargo lock` Cargo subcommand which can be installed
30//! by running the following:
31//!
32//! ```text
33//! $ cargo install cargo-lock --features cli
34//! ```
35//!
36//! It supports the following subcommands:
37//!
38//! ### `list`: summarize packages in `Cargo.lock`
39//!
40//! The `cargo lock list` subcommand (which can be shortened to just
41//! `cargo lock` if you prefer) provides a short synopsis of the packages
42//! enumerated in `Cargo.lock`:
43//!
44//! ```text
45//! $ cargo lock
46//! - autocfg 1.0.0
47//! - cargo-lock 4.0.1
48//! - fixedbitset 0.2.0
49//! - gumdrop 0.8.0
50//! - gumdrop_derive 0.8.0
51//! - idna 0.2.0
52//! - indexmap 1.3.2
53//! - matches 0.1.8
54//! [...]
55//! ```
56//!
57//! Adding a `-d` (or `--dependencies`) flag will show transitive dependencies:
58//!
59//! ```text
60//! $ cargo lock -d
61//! - autocfg 1.0.0
62//! - cargo-lock 4.0.1
63//! - gumdrop 0.8.0
64//! - petgraph 0.5.1
65//! - semver 0.10.0
66//! - serde 1.0.116
67//! - toml 0.5.6
68//! - url 2.1.1
69//! - fixedbitset 0.2.0
70//! - gumdrop 0.8.0
71//! - gumdrop_derive 0.8.0
72//! - gumdrop_derive 0.8.0
73//! - proc-macro2 1.0.21
74//! - quote 1.0.3
75//! - syn 1.0.40
76//! - idna 0.2.0
77//! - matches 0.1.8
78//! - unicode-bidi 0.3.4
79//! - unicode-normalization 0.1.12
80//! [...]
81//! ```
82//!
83//! Adding a `-s` (or `--source`) flag will show source information for each
84//! package (when available):
85//!
86//! ```text
87//! - autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)
88//! - cargo-lock 4.0.1
89//! - fixedbitset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)
90//! - gumdrop 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)
91//! - gumdrop_derive 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)
92//! - idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)
93//! - indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)
94//! [...]
95//! ```
96//!
97//! ### `translate`: convert `Cargo.lock` files between the V1 and V2 formats
98//!
99//! The `cargo lock translate` subcommand can translate V1 Cargo.lock files to
100//! the [V2 format] and vice versa:
101//!
102//! ```text
103//! $ cargo lock translate
104//! ```
105//!
106//! ...will translate Cargo.lock to the V2 format. To translate a V2 Cargo.lock
107//! file back to the V1 format, use:
108//!
109//! ```text
110//! $ cargo lock translate -v1
111//! ```
112//!
113//! ### `tree`: provide information for how a dependency is included
114//!
115//! The `cargo lock tree` subcommand (similar to the `cargo-tree` command)
116//! can provide a visualization of the current dependency tree or how a
117//! particular dependency is being used in your project, by consulting
118//! `Cargo.lock` alone:
119//!
120//! ```text
121//! $ cargo lock tree
122//! cargo-lock 4.0.1
123//! ├── url 2.1.1
124//! │ ├── percent-encoding 2.1.0
125//! │ ├── matches 0.1.8
126//! │ └── idna 0.2.0
127//! │ ├── unicode-normalization 0.1.12
128//! │ │ └── smallvec 1.2.0
129//! │ ├── unicode-bidi 0.3.4
130//! │ │ └── matches 0.1.8
131//! │ └── matches 0.1.8
132//! ├── toml 0.5.6
133//! │ └── serde 1.0.116
134//! │ └── serde_derive 1.0.116
135//! [...]
136//! ```
137//!
138//! ```text
139//! $ cargo lock tree syn
140//! syn 1.0.14
141//! ├── serde_derive 1.0.104
142//! │ └── serde 1.0.104
143//! │ ├── toml 0.5.6
144//! │ │ └── cargo-lock 3.0.0
145//! │ ├── semver 0.9.0
146//! │ │ └── cargo-lock 3.0.0
147//! │ └── cargo-lock 3.0.0
148//! └── gumdrop_derive 0.7.0
149//! └── gumdrop 0.7.0
150//! └── cargo-lock 3.0.0
151//! ```
152//!
153//! [RustSec]: https://rustsec.org/
154//! [V2 format]: https://github.com/rust-lang/cargo/pull/7070
155//! [`petgraph`]: https://github.com/petgraph/petgraph
156//! [`cargo-tree`]: https://github.com/sfackler/cargo-tree
157
158pub mod dependency;
159pub mod package;
160
161mod error;
162mod lockfile;
163mod metadata;
164mod patch;
165
166pub use crate::{
167 dependency::Dependency,
168 error::{Error, Result},
169 lockfile::{Lockfile, ResolveVersion},
170 metadata::{Metadata, MetadataKey, MetadataValue},
171 package::{Checksum, Name, Package, SourceId, Version},
172 patch::Patch,
173};
174
175/// Use `BTreeMap` for all `Map` types in the crate
176use std::collections::BTreeMap as Map;