Skip to content

Commit 4f00929

Browse files
committed
docs(sources): doc comments for DirectorySource and ReplacedSource
1 parent 83131fc commit 4f00929

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Diff for: src/cargo/sources/directory.rs

+47
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,64 @@ use anyhow::Context as _;
1313
use cargo_util::{paths, Sha256};
1414
use serde::Deserialize;
1515

16+
/// `DirectorySource` contains a number of crates on the file system. It was
17+
/// designed for representing vendored dependencies for `cargo vendor`.
18+
///
19+
/// `DirectorySource` at this moment is just a root directory containing other
20+
/// directories, which contain the source files of packages. Assumptions would
21+
/// be made to determine if a directory should be included as a package of a
22+
/// directory source's:
23+
///
24+
/// * Ignore directories starting with dot `.` (tend to be hidden).
25+
/// * Only when a `Cargo.toml` exists in a directory will it be included as
26+
/// a package. `DirectorySource` at this time only looks at one level of
27+
/// directories and never went deeper.
28+
/// * There must be a [`Checksum`] file `.cargo-checksum.json` file at the same
29+
/// level of `Cargo.toml` to ensure the integrity when a directory source was
30+
/// created (usually by `cargo vendor`). A failure to find or parse a single
31+
/// checksum results in a denial of loading any package in this source.
32+
/// * Otherwise, there is no other restrction of the name of directories. At
33+
/// this moment, it is `cargo vendor` that defines the layout and the name of
34+
/// each directory.
35+
///
36+
/// The file tree of a directory source may look like:
37+
///
38+
/// ```text
39+
/// [source root]
40+
/// ├── a-valid-crate/
41+
/// │ ├── src/
42+
/// │ ├── .cargo-checksum.json
43+
/// │ └── Cargo.toml
44+
/// ├── .ignored-a-dot-crate/
45+
/// │ ├── src/
46+
/// │ ├── .cargo-checksum.json
47+
/// │ └── Cargo.toml
48+
/// ├── skipped-no-manifest/
49+
/// │ ├── src/
50+
/// │ └── .cargo-checksum.json
51+
/// └── no-checksum-so-fails-the-entire-source-reading/
52+
/// └── Cargo.toml
53+
/// ```
1654
pub struct DirectorySource<'cfg> {
55+
/// The unique identifier of this source.
1756
source_id: SourceId,
57+
/// The root path of this source.
1858
root: PathBuf,
59+
/// Packages that this sources has discovered.
1960
packages: HashMap<PackageId, (Package, Checksum)>,
2061
config: &'cfg Config,
2162
updated: bool,
2263
}
2364

65+
/// The checksum file to ensure the integrity of a package in a directory source.
66+
///
67+
/// The file name is simply `.cargo-checksum.json`. The checksum algorithm as
68+
/// of now is SHA256.
2469
#[derive(Deserialize)]
2570
struct Checksum {
71+
/// Checksum of the package. Normally it is computed from the `.crate` file.
2672
package: Option<String>,
73+
/// Checksums of each source file.
2774
files: HashMap<String, String>,
2875
}
2976

Diff for: src/cargo/sources/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
//! Implementations of `Source` trait.
2+
//!
3+
//! Cargo provides several built-in implementations of [`Source`] trait. Namely,
4+
//!
5+
//! * [`RegistrySource`] --- A source that provides an index for people to query
6+
//! a crate's metadata, and fetch files for a certain crate. crates.io falls
7+
//! into this category. So do local registry and sparse registry.
8+
//! * [`DirectorySource`] --- Files are downloaded ahead of time. Primarily
9+
//! designed for crates generated from `cargo vendor`.
10+
//! * [`GitSource`] --- This gets crate information from a git repository.
11+
//! * [`PathSource`] --- This gets crate information from a local path on the
12+
//! filesystem.
13+
//! * [`ReplacedSource`] --- This manages the [source replacement] feature,
14+
//! redirecting operations on the original source to the replacement.
15+
//!
16+
//! This module also contains [`SourceConfigMap`], which is effectively the
17+
//! representation of the `[source.*]` value in Cargo configuration.
18+
//!
19+
//! [`Source`]: crate::core::Source
20+
//! [source replacement]: https://doc.rust-lang.org/nightly/cargo/reference/source-replacement.html
21+
122
pub use self::config::SourceConfigMap;
223
pub use self::directory::DirectorySource;
324
pub use self::git::GitSource;

Diff for: src/cargo/sources/replaced.rs

+11
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ use std::task::Poll;
55

66
use anyhow::Context as _;
77

8+
/// A source that replaces one source with the other. This manages the [source
9+
/// replacement] feature.
10+
///
11+
/// The implementation is merely redirecting from the original to the replacement.
12+
///
13+
/// [source replacement]: https://doc.rust-lang.org/nightly/cargo/reference/source-replacement.html
814
pub struct ReplacedSource<'cfg> {
15+
/// The identifier of the original source.
916
to_replace: SourceId,
17+
/// The identifier of the new replacement source.
1018
replace_with: SourceId,
1119
inner: Box<dyn Source + 'cfg>,
1220
}
1321

1422
impl<'cfg> ReplacedSource<'cfg> {
23+
/// Creates a replaced source.
24+
///
25+
/// The `src` argument is the new replacement source.
1526
pub fn new(
1627
to_replace: SourceId,
1728
replace_with: SourceId,

0 commit comments

Comments
 (0)