Skip to content

Commit

Permalink
refactor: alt_registry_key -> registry_key
Browse files Browse the repository at this point in the history
Given we remove `SourceId::name` field, `alt_registry_key` is no longer
only for alternative registries.
  • Loading branch information
weihanglo committed Sep 15, 2023
1 parent a867f22 commit a47b368
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
25 changes: 12 additions & 13 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ struct SourceIdInner {
kind: SourceKind,
/// For example, the exact Git revision of the specified branch for a Git Source.
precise: Option<String>,
/// Name of the alt registry in the `[registries]` table.
/// WARNING: this is not always set for alt-registries when the name is
/// not known.
alt_registry_key: Option<String>,
/// Name of the remote registry as defined in the `[registries]` table.
///
/// WARNING: this is not always set for alt-registries when the name is not known.
registry_key: Option<String>,
}

/// The possible kinds of code source.
Expand Down Expand Up @@ -107,7 +107,7 @@ impl SourceId {
canonical_url: CanonicalUrl::new(&url)?,
url,
precise: None,
alt_registry_key: key.map(|k| k.into()),
registry_key: key.map(|k| k.into()),
});
Ok(source_id)
}
Expand Down Expand Up @@ -315,7 +315,7 @@ impl SourceId {
pub fn display_registry_name(self) -> String {
if self.is_crates_io() {
CRATES_IO_REGISTRY.to_string()
} else if let Some(key) = &self.inner.alt_registry_key {
} else if let Some(key) = &self.inner.registry_key {
key.clone()
} else if self.precise().is_some() {
// We remove `precise` here to retrieve an permissive version of
Expand All @@ -327,10 +327,10 @@ impl SourceId {
}

/// Gets the name of the remote registry as defined in the `[registries]` table.
/// WARNING: alt registries that come from Cargo.lock, or --index will
/// not have a name.
pub fn alt_registry_key(&self) -> Option<&str> {
self.inner.alt_registry_key.as_deref()
///
/// WARNING: alt registries that come from Cargo.lock, or `--index` will not have a name.
pub fn registry_key(&self) -> Option<&str> {
self.inner.registry_key.as_deref()
}

/// Returns `true` if this source is from a filesystem path.
Expand Down Expand Up @@ -643,10 +643,9 @@ impl Hash for SourceId {
/// The hash of `SourceIdInner` is used to retrieve its interned value from
/// `SOURCE_ID_CACHE`. We only care about fields that make `SourceIdInner`
/// unique. Optional fields not affecting the uniqueness must be excluded,
/// such as [`name`] and [`alt_registry_key`]. That's why this is not derived.
/// such as [`registry_key`]. That's why this is not derived.
///
/// [`name`]: SourceIdInner::name
/// [`alt_registry_key`]: SourceIdInner::alt_registry_key
/// [`registry_key`]: SourceIdInner::registry_key
impl Hash for SourceIdInner {
fn hash<S: hash::Hasher>(&self, into: &mut S) {
self.kind.hash(into);
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 @@ -188,7 +188,7 @@ fn get_source_id(
.replaced_source_id();
if reg.is_none() && index.is_none() && replacement_sid != builtin_replacement_sid {
// Neither --registry nor --index was passed and the user has configured source-replacement.
if let Some(replacement_name) = replacement_sid.alt_registry_key() {
if let Some(replacement_name) = replacement_sid.registry_key() {
bail!("crates-io is replaced with remote registry {replacement_name};\ninclude `--registry {replacement_name}` or `--registry crates-io`");
} else {
bail!("crates-io is replaced with non-remote-registry source {replacement_sid};\ninclude `--registry crates-io` to use crates.io");
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/util/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn registry_credential_config_raw_uncached(
// If the name doesn't match, leave a note to help the user understand
// the potentially confusing situation.
if let Some(name) = name.as_deref() {
if Some(name) != sid.alt_registry_key() {
if Some(name) != sid.registry_key() {
config.shell().note(format!(
"name of alternative registry `{}` set to `{name}`",
sid.url()
Expand Down Expand Up @@ -429,7 +429,7 @@ impl fmt::Display for AuthorizationError {
write!(f, "\nor use environment variable CARGO_REGISTRY_TOKEN")?;
}
Ok(())
} else if let Some(name) = self.sid.alt_registry_key() {
} else if let Some(name) = self.sid.registry_key() {
let key = ConfigKey::from_str(&format!("registries.{name}.token"));
write!(
f,
Expand Down Expand Up @@ -500,7 +500,7 @@ fn credential_action(
let name = if sid.is_crates_io() {
Some(CRATES_IO_REGISTRY)
} else {
sid.alt_registry_key()
sid.registry_key()
};
let registry = RegistryInfo {
index_url: sid.url().as_str(),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,7 @@ pub fn save_credentials(
None
} else {
let name = registry
.alt_registry_key()
.registry_key()
.ok_or_else(|| internal("can't save credentials for anonymous registry"))?;
Some(name)
};
Expand Down

0 comments on commit a47b368

Please sign in to comment.