Skip to content

Commit 6fdabf7

Browse files
committed
docs(source): doc comments for PathSource
1 parent 141f74f commit 6fdabf7

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/cargo/sources/path.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,28 @@ use ignore::gitignore::GitignoreBuilder;
1414
use log::{trace, warn};
1515
use walkdir::WalkDir;
1616

17+
/// A source represents one or multiple packages gathering from a given root
18+
/// path on the filesystem.
19+
///
20+
/// It's the cornerstone of every other source --- other implementations
21+
/// eventually need to call `PathSource` to read local packages somewhere on
22+
/// the filesystem.
23+
///
24+
/// It also provides convenient methods like [`PathSource::list_files`] to
25+
/// list all files in a package, given its ability to walk the filesystem.
1726
pub struct PathSource<'cfg> {
27+
/// The unique identifier of this source.
1828
source_id: SourceId,
29+
/// The root path of this source.
1930
path: PathBuf,
31+
/// Whether this source has updated all package informations it may contain.
2032
updated: bool,
33+
/// Packages that this sources has discovered.
2134
packages: Vec<Package>,
22-
config: &'cfg Config,
35+
/// Whether this source should discover nested packages recursively.
36+
/// See [`PathSource::new_recursive`] for more.
2337
recursive: bool,
38+
config: &'cfg Config,
2439
}
2540

2641
impl<'cfg> PathSource<'cfg> {
@@ -41,9 +56,9 @@ impl<'cfg> PathSource<'cfg> {
4156

4257
/// Creates a new source which is walked recursively to discover packages.
4358
///
44-
/// This is similar to the `new` method except that instead of requiring a
45-
/// valid package to be present at `root` the folder is walked entirely to
46-
/// crawl for packages.
59+
/// This is similar to the [`PathSource::new`] method except that instead
60+
/// of requiring a valid package to be present at `root` the folder is
61+
/// walked entirely to crawl for packages.
4762
///
4863
/// Note that this should be used with care and likely shouldn't be chosen
4964
/// by default!
@@ -54,6 +69,8 @@ impl<'cfg> PathSource<'cfg> {
5469
}
5570
}
5671

72+
/// Preloads a package for this source. The source is assumed that it has
73+
/// yet loaded any other packages.
5774
pub fn preload_with(&mut self, pkg: Package) {
5875
assert!(!self.updated);
5976
assert!(!self.recursive);
@@ -62,6 +79,7 @@ impl<'cfg> PathSource<'cfg> {
6279
self.packages.push(pkg);
6380
}
6481

82+
/// Gets the package on the root path.
6583
pub fn root_package(&mut self) -> CargoResult<Package> {
6684
trace!("root_package; source={:?}", self);
6785

@@ -76,6 +94,8 @@ impl<'cfg> PathSource<'cfg> {
7694
}
7795
}
7896

97+
/// Returns the packages discovered by this source. It may walk the
98+
/// the filesystem if package informations haven't yet updated.
7999
pub fn read_packages(&self) -> CargoResult<Vec<Package>> {
80100
if self.updated {
81101
Ok(self.packages.clone())
@@ -96,7 +116,8 @@ impl<'cfg> PathSource<'cfg> {
96116
///
97117
/// The basic assumption of this method is that all files in the directory
98118
/// are relevant for building this package, but it also contains logic to
99-
/// use other methods like .gitignore to filter the list of files.
119+
/// use other methods like `.gitignore`, `package.include`, or
120+
/// `package.exclude` to filter the list of files.
100121
pub fn list_files(&self, pkg: &Package) -> CargoResult<Vec<PathBuf>> {
101122
self._list_files(pkg).with_context(|| {
102123
format!(
@@ -106,6 +127,7 @@ impl<'cfg> PathSource<'cfg> {
106127
})
107128
}
108129

130+
/// See [`PathSource::list_files`].
109131
fn _list_files(&self, pkg: &Package) -> CargoResult<Vec<PathBuf>> {
110132
let root = pkg.root();
111133
let no_include_option = pkg.manifest().include().is_empty();
@@ -218,6 +240,11 @@ impl<'cfg> PathSource<'cfg> {
218240
Ok(None)
219241
}
220242

243+
/// Lists files relevant to building this package inside this source by
244+
/// consulting both Git index (tracked) or status (untracked) under
245+
/// a given Git repository.
246+
///
247+
/// This looks into Git submodules as well.
221248
fn list_files_git(
222249
&self,
223250
pkg: &Package,
@@ -373,6 +400,11 @@ impl<'cfg> PathSource<'cfg> {
373400
}
374401
}
375402

403+
/// Lists files relevant to building this package inside this source by
404+
/// walking the filesystem from the package root path.
405+
///
406+
/// This is a fallback for [`PathSource::list_files_git`] when the package
407+
/// is not tracked under a Git repository.
376408
fn list_files_walk(
377409
&self,
378410
pkg: &Package,
@@ -383,6 +415,7 @@ impl<'cfg> PathSource<'cfg> {
383415
Ok(ret)
384416
}
385417

418+
/// Helper recursive function for [`PathSource::list_files_walk`].
386419
fn walk(
387420
&self,
388421
path: &Path,
@@ -448,6 +481,7 @@ impl<'cfg> PathSource<'cfg> {
448481
Ok(())
449482
}
450483

484+
/// Gets the last modified file in a package.
451485
pub fn last_modified_file(&self, pkg: &Package) -> CargoResult<(FileTime, PathBuf)> {
452486
if !self.updated {
453487
return Err(internal(format!(
@@ -479,10 +513,12 @@ impl<'cfg> PathSource<'cfg> {
479513
Ok((max, max_path))
480514
}
481515

516+
/// Returns the root path of this source.
482517
pub fn path(&self) -> &Path {
483518
&self.path
484519
}
485520

521+
/// Discovers packages inside this source if it hasn't yet done.
486522
pub fn update(&mut self) -> CargoResult<()> {
487523
if !self.updated {
488524
let packages = self.read_packages()?;

0 commit comments

Comments
 (0)