Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: put Source trait under cargo::sources #12527

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use std::time::Instant;

use cargo::core::dependency::DepKind;
use cargo::core::resolver::{self, ResolveOpts, VersionPreferences};
use cargo::core::source::{GitReference, QueryKind, SourceId};
use cargo::core::Resolve;
use cargo::core::{Dependency, PackageId, Registry, Summary};
use cargo::core::{GitReference, SourceId};
use cargo::sources::source::QueryKind;
use cargo::util::{CargoResult, Config, Graph, IntoUrl, PartialVersion};

use proptest::collection::{btree_map, vec};
Expand Down
2 changes: 1 addition & 1 deletion crates/xtask-bump-check/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use std::task;
use cargo::core::dependency::Dependency;
use cargo::core::registry::PackageRegistry;
use cargo::core::Package;
use cargo::core::QueryKind;
use cargo::core::Registry;
use cargo::core::SourceId;
use cargo::core::Workspace;
use cargo::sources::source::QueryKind;
use cargo::util::command_prelude::*;
use cargo::util::ToSemver;
use cargo::CargoResult;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
//! [2]: https://github.com/rust-lang/rust/blob/9bb6e60d1f1360234aae90c97964c0fa5524f141/compiler/rustc_errors/src/json.rs#L312-L315

use crate::core::compiler::BuildContext;
use crate::core::{Dependency, PackageId, QueryKind, Workspace};
use crate::core::{Dependency, PackageId, Workspace};
use crate::sources::source::QueryKind;
use crate::sources::SourceConfigMap;
use crate::util::{iter_join, CargoResult, Config};
use anyhow::{bail, format_err, Context};
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use self::package_id_spec::PackageIdSpec;
pub use self::registry::Registry;
pub use self::resolver::{Resolve, ResolveVersion};
pub use self::shell::{Shell, Verbosity};
pub use self::source::{GitReference, QueryKind, Source, SourceId, SourceMap};
pub use self::source_id::{GitReference, SourceId};
pub use self::summary::{FeatureMap, FeatureValue, Summary};
pub use self::workspace::{
find_workspace_root, resolve_relative_path, MaybePackage, Workspace, WorkspaceConfig,
Expand All @@ -27,6 +27,6 @@ pub mod profiles;
pub mod registry;
pub mod resolver;
pub mod shell;
pub mod source;
mod source_id;
pub mod summary;
mod workspace;
4 changes: 2 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::source::MaybePackage;
use crate::core::{Dependency, Manifest, PackageId, SourceId, Target};
use crate::core::{SourceMap, Summary, Workspace};
use crate::core::{Summary, Workspace};
use crate::sources::source::{MaybePackage, SourceMap};
use crate::util::config::PackageCacheLock;
use crate::util::errors::{CargoResult, HttpNotSuccessful};
use crate::util::interning::InternedString;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::OnceLock;
use serde::de;
use serde::ser;

use crate::core::source::SourceId;
use crate::core::SourceId;
use crate::util::interning::InternedString;
use crate::util::{CargoResult, ToSemver};

Expand Down Expand Up @@ -238,7 +238,7 @@ impl fmt::Debug for PackageId {
#[cfg(test)]
mod tests {
use super::PackageId;
use crate::core::source::SourceId;
use crate::core::SourceId;
use crate::sources::CRATES_IO_INDEX;
use crate::util::IntoUrl;

Expand Down
5 changes: 4 additions & 1 deletion src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use std::collections::{HashMap, HashSet};
use std::task::{ready, Poll};

use crate::core::PackageSet;
use crate::core::{Dependency, PackageId, QueryKind, Source, SourceId, SourceMap, Summary};
use crate::core::{Dependency, PackageId, SourceId, Summary};
use crate::sources::config::SourceConfigMap;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::sources::source::SourceMap;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{CanonicalUrl, Config};
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use crate::core::resolver::{
ActivateError, ActivateResult, CliFeatures, RequestedFeatures, ResolveOpts, VersionOrdering,
VersionPreferences,
};
use crate::core::{
Dependency, FeatureValue, PackageId, PackageIdSpec, QueryKind, Registry, Summary,
};
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
use crate::sources::source::QueryKind;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::PartialVersion;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt;
use std::task::Poll;

use crate::core::{Dependency, PackageId, QueryKind, Registry, Summary};
use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::sources::source::QueryKind;
use crate::util::edit_distance::edit_distance;
use crate::util::{Config, VersionExt};
use anyhow::Error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::core::PackageId;
use crate::sources::registry::CRATES_IO_HTTP_INDEX;
use crate::sources::source::Source;
use crate::sources::{DirectorySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
use crate::sources::{GitSource, PathSource, RegistrySource};
use crate::util::{config, CanonicalUrl, CargoResult, Config, IntoUrl, ToSemver};
Expand Down Expand Up @@ -29,9 +30,9 @@ static SOURCE_ID_CACHE: OnceLock<Mutex<HashSet<&'static SourceIdInner>>> = OnceL
/// `SourceId` is usually associated with an instance of [`Source`], which is
/// supposed to provide a `SourceId` via [`Source::source_id`] method.
///
/// [`Source`]: super::Source
/// [`Source::source_id`]: super::Source::source_id
/// [`PackageId`]: super::super::PackageId
/// [`Source`]: crate::sources::source::Source
/// [`Source::source_id`]: crate::sources::source::Source::source_id
/// [`PackageId`]: super::PackageId
#[derive(Clone, Copy, Eq, Debug)]
pub struct SourceId {
inner: &'static SourceIdInner,
Expand Down Expand Up @@ -395,7 +396,7 @@ impl SourceId {
self,
config: &'a Config,
yanked_whitelist: &HashSet<PackageId>,
) -> CargoResult<Box<dyn super::Source + 'a>> {
) -> CargoResult<Box<dyn Source + 'a>> {
trace!("loading SourceId; {}", self);
match self.inner.kind {
SourceKind::Git(..) => Ok(Box::new(GitSource::new(self, config)?)),
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
//! - [`core::compiler::fingerprint`]:
//! The `fingerprint` module contains all the code that handles detecting
//! if a crate needs to be recompiled.
//! - [`core::source`]:
//! The [`core::Source`] trait is an abstraction over different sources of packages.
//! - [`sources::source`]:
//! The [`sources::source::Source`] trait is an abstraction over different sources of packages.
//! Sources are uniquely identified by a [`core::SourceId`]. Sources are implemented in the [`sources`]
//! directory.
//! - [`util`]:
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::core::dependency::DepKind;
use crate::core::registry::PackageRegistry;
use crate::core::FeatureValue;
use crate::core::Package;
use crate::core::QueryKind;
use crate::core::Registry;
use crate::core::Shell;
use crate::core::Summary;
use crate::core::Workspace;
use crate::sources::source::QueryKind;
use crate::util::toml_mut::dependency::Dependency;
use crate::util::toml_mut::dependency::GitSource;
use crate::util::toml_mut::dependency::MaybeWorkspace;
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use std::{env, fs};

use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, UnitOutput};
use crate::core::{
Dependency, Edition, Package, PackageId, PackageIdSpec, Source, SourceId, Target, Workspace,
Dependency, Edition, Package, PackageId, PackageIdSpec, SourceId, Target, Workspace,
};
use crate::ops::{common_for_install_and_uninstall::*, FilterRule};
use crate::ops::{CompileFilter, Packages};
use crate::sources::source::Source;
use crate::sources::{GitSource, PathSource, SourceConfigMap};
use crate::util::errors::CargoResult;
use crate::util::{Config, Filesystem, Rustc};
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use serde::{Deserialize, Serialize};

use crate::core::compiler::{DirtyReason, Freshness};
use crate::core::Target;
use crate::core::{Dependency, FeatureValue, Package, PackageId, QueryKind, Source, SourceId};
use crate::core::{Dependency, FeatureValue, Package, PackageId, SourceId};
use crate::ops::{self, CompileFilter, CompileOptions};
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::Config;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use anyhow::{bail, format_err, Context as _};
use cargo_credential::{Operation, Secret};
use crates_io::{self, Registry};

use crate::core::source::Source;
use crate::core::SourceId;
use crate::sources::source::Source;
use crate::sources::{RegistrySource, SourceConfigMap};
use crate::util::auth;
use crate::util::config::{Config, PathAndArgs};
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/registry/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use crate::core::manifest::ManifestMetadata;
use crate::core::resolver::CliFeatures;
use crate::core::Dependency;
use crate::core::Package;
use crate::core::QueryKind;
use crate::core::SourceId;
use crate::core::Workspace;
use crate::ops;
use crate::ops::PackageOpts;
use crate::ops::Packages;
use crate::sources::source::QueryKind;
use crate::sources::SourceConfigMap;
use crate::sources::CRATES_IO_REGISTRY;
use crate::util::auth;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
//! [`Package`]: crate::core::package
//! [`Target`]: crate::core::Target
//! [`Manifest`]: crate::core::Manifest
//! [`Source`]: crate::core::Source
//! [`SourceMap`]: crate::core::SourceMap
//! [`Source`]: crate::sources::source::Source
//! [`SourceMap`]: crate::sources::source::SourceMap
//! [`PackageRegistry`]: crate::core::registry::PackageRegistry
//! [source implementations]: crate::sources
//! [`Downloads`]: crate::core::package::Downloads
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/sources/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! structure usable by Cargo itself. Currently this is primarily used to map
//! sources to one another via the `replace-with` key in `.cargo/config`.

use crate::core::{GitReference, PackageId, Source, SourceId};
use crate::core::{GitReference, PackageId, SourceId};
use crate::sources::source::Source;
use crate::sources::{ReplacedSource, CRATES_IO_REGISTRY};
use crate::util::config::{self, ConfigRelativePath, OptValue};
use crate::util::errors::CargoResult;
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/sources/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::fmt::{self, Debug, Formatter};
use std::path::{Path, PathBuf};
use std::task::Poll;

use crate::core::source::MaybePackage;
use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::core::{Dependency, Package, PackageId, SourceId, Summary};
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::Config;
Expand Down
5 changes: 4 additions & 1 deletion src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! See [GitSource].

use crate::core::source::{MaybePackage, QueryKind, Source, SourceId};
use crate::core::GitReference;
use crate::core::SourceId;
use crate::core::{Dependency, Package, PackageId, Summary};
use crate::sources::git::utils::GitRemote;
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::hex::short_hash;
Expand Down
14 changes: 11 additions & 3 deletions src/cargo/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
//! Implementations of `Source` trait.
//! The trait for sources of Cargo packages and its built-in implemetations.
//!
//! Cargo provides several built-in implementations of [`Source`] trait. Namely,
//! A source is a provider that contains source files and metadata of packages.
//! It provides a number of methods to fetch those package informations, for
//! example, querying metadata or downloading files for a package. These
//! informations then can be used as dependencies for other Cargo packages.
//!
//! This module provides [`Source`][source::Source] trait as an abstraction of different sources,
//! as well as [`SourceMap`][source::SourceMap] struct as a map of all available sources.
//!
//! Several built-in implementations of `Source` trait are provided. Namely,
//!
//! * [`RegistrySource`] --- A source that provides an index for people to query
//! a crate's metadata, and fetch files for a certain crate. crates.io falls
Expand All @@ -16,7 +24,6 @@
//! This module also contains [`SourceConfigMap`], which is effectively the
//! representation of the `[source.*]` value in Cargo configuration.
//!
//! [`Source`]: crate::core::Source
//! [source replacement]: https://doc.rust-lang.org/nightly/cargo/reference/source-replacement.html

pub use self::config::SourceConfigMap;
Expand All @@ -32,3 +39,4 @@ pub mod git;
pub mod path;
pub mod registry;
pub mod replaced;
pub mod source;
6 changes: 4 additions & 2 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::fmt::{self, Debug, Formatter};
use std::path::{Path, PathBuf};
use std::task::Poll;

use crate::core::source::MaybePackage;
use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::core::{Dependency, Package, PackageId, SourceId, Summary};
use crate::ops;
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::util::{internal, CargoResult, Config};
use anyhow::Context as _;
use cargo_util::paths;
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,10 @@ use tar::Archive;
use tracing::debug;

use crate::core::dependency::Dependency;
use crate::core::source::MaybePackage;
use crate::core::{Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::core::{Package, PackageId, SourceId, Summary};
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::sources::PathSource;
use crate::util::hex;
use crate::util::network::PollExt;
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/sources/replaced.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::core::source::MaybePackage;
use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::core::{Dependency, Package, PackageId, SourceId, Summary};
use crate::sources::source::MaybePackage;
use crate::sources::source::QueryKind;
use crate::sources::source::Source;
use crate::util::errors::CargoResult;
use std::task::Poll;

Expand Down
20 changes: 2 additions & 18 deletions src/cargo/core/source/mod.rs → src/cargo/sources/source.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
//! Fundamental types and traits for sources of Cargo packages.
//!
//! A source is a provider that contains source files and metadata of packages.
//! It provides a number of methods to fetch those package informations, for
//! example, querying metadata or downloading files for a package. These
//! informations then can be used as dependencies for other Cargo packages.
//!
//! Notably, this module contains
//!
//! * [`Source`] trait as an abstraction of different sources
//! * [`SourceMap`] struct as a map of all available sources
//! * [`SourceId`] struct as an unique identifier for a certain source
//!
//! For implementations of `Source` trait, see [`crate::sources`].
//! [`Source`] trait for sources of Cargo packages.

use std::collections::hash_map::HashMap;
use std::fmt;
use std::task::Poll;

use crate::core::package::PackageSet;
use crate::core::SourceId;
use crate::core::{Dependency, Package, PackageId, Summary};
use crate::util::{CargoResult, Config};

mod source_id;

pub use self::source_id::{GitReference, SourceId};

/// An abstraction of different sources of Cargo packages.
///
/// The [`Source`] trait generalizes the API to interact with these providers.
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/profile_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn named_config_profile() {
let ws = Workspace::new(&paths::root().join("Cargo.toml"), &config).unwrap();
let profiles = Profiles::new(&ws, profile_name).unwrap();

let crates_io = cargo::core::source::SourceId::crates_io(&config).unwrap();
let crates_io = cargo::core::SourceId::crates_io(&config).unwrap();
let a_pkg = PackageId::new("a", "0.1.0", crates_io).unwrap();
let dep_pkg = PackageId::new("dep", "0.1.0", crates_io).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ fn setup() -> RegistryBuilder {
fn not_update() {
let registry = setup().build();

use cargo::core::{Shell, Source, SourceId};
use cargo::core::{Shell, SourceId};
use cargo::sources::source::Source;
use cargo::sources::RegistrySource;
use cargo::util::Config;

Expand Down