Skip to content

Commit 87813bf

Browse files
committed
refactor(publish): Clarify which SourceId is being used
Ran into confusion on this when updating #11062 to be on top of #10907. This now threads both types of sources through which comments explaining each so callers will always have to explicitly acknowledge which they are dealing with.
1 parent 83a1f66 commit 87813bf

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/cargo/ops/registry.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
142142
}
143143
}
144144

145-
let (mut registry, _reg_cfg, reg_id) = registry(
145+
let (mut registry, _reg_cfg, reg_ids) = registry(
146146
opts.config,
147147
opts.token.clone(),
148148
opts.index.as_deref(),
149149
publish_registry.as_deref(),
150150
true,
151151
!opts.dry_run,
152152
)?;
153-
verify_dependencies(pkg, &registry, reg_id)?;
153+
verify_dependencies(pkg, &registry, reg_ids.original)?;
154154

155155
// Prepare a tarball, with a non-suppressible warning if metadata
156156
// is missing since this is being put online.
@@ -180,7 +180,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
180180
pkg,
181181
tarball.file(),
182182
&mut registry,
183-
reg_id,
183+
reg_ids.original,
184184
opts.dry_run,
185185
)?;
186186

@@ -456,12 +456,12 @@ fn registry(
456456
registry: Option<&str>,
457457
force_update: bool,
458458
validate_token: bool,
459-
) -> CargoResult<(Registry, RegistryConfig, SourceId)> {
460-
let (sid, sid_no_replacement) = get_source_id(config, index, registry)?;
459+
) -> CargoResult<(Registry, RegistryConfig, RegistrySourceIds)> {
460+
let source_ids = get_source_id(config, index, registry)?;
461461
let reg_cfg = registry_configuration(config, registry)?;
462462
let api_host = {
463463
let _lock = config.acquire_package_cache_lock()?;
464-
let mut src = RegistrySource::remote(sid, &HashSet::new(), config)?;
464+
let mut src = RegistrySource::remote(source_ids.replacement, &HashSet::new(), config)?;
465465
// Only update the index if the config is not available or `force` is set.
466466
if force_update {
467467
src.invalidate_cache()
@@ -470,13 +470,14 @@ fn registry(
470470
match src.config()? {
471471
Poll::Pending => src
472472
.block_until_ready()
473-
.with_context(|| format!("failed to update {}", sid))?,
473+
.with_context(|| format!("failed to update {}", source_ids.replacement))?,
474474
Poll::Ready(cfg) => break cfg,
475475
}
476476
};
477477

478-
cfg.and_then(|cfg| cfg.api)
479-
.ok_or_else(|| format_err!("{} does not support API commands", sid))?
478+
cfg.and_then(|cfg| cfg.api).ok_or_else(|| {
479+
format_err!("{} does not support API commands", source_ids.replacement)
480+
})?
480481
};
481482
let token = if validate_token {
482483
if index.is_some() {
@@ -495,7 +496,7 @@ fn registry(
495496
Ok((
496497
Registry::new_handle(api_host, token, handle),
497498
reg_cfg,
498-
sid_no_replacement,
499+
source_ids,
499500
))
500501
}
501502

@@ -919,7 +920,7 @@ fn get_source_id(
919920
config: &Config,
920921
index: Option<&str>,
921922
reg: Option<&str>,
922-
) -> CargoResult<(SourceId, SourceId)> {
923+
) -> CargoResult<RegistrySourceIds> {
923924
let sid = match (reg, index) {
924925
(None, None) => SourceId::crates_io(config)?,
925926
(Some(r), None) => SourceId::alt_registry(config, r)?,
@@ -943,18 +944,34 @@ fn get_source_id(
943944
bail!("crates-io is replaced with non-remote-registry source {replacement_sid};\ninclude `--registry crates-io` to use crates.io");
944945
}
945946
} else {
946-
Ok((builtin_replacement_sid, sid))
947+
Ok(RegistrySourceIds {
948+
original: sid,
949+
replacement: builtin_replacement_sid,
950+
})
947951
}
948952
}
949953

954+
struct RegistrySourceIds {
955+
/// Use when looking up the auth token, or writing out `Cargo.lock`
956+
original: SourceId,
957+
/// Use when interacting with the source (querying / publishing , etc)
958+
///
959+
/// The source for crates.io may be replaced by a built-in source for accessing crates.io with
960+
/// the sparse protocol, or a source for the testing framework (when the replace_crates_io
961+
/// function is used)
962+
///
963+
/// User-defined source replacement is not applied.
964+
replacement: SourceId,
965+
}
966+
950967
pub fn search(
951968
query: &str,
952969
config: &Config,
953970
index: Option<String>,
954971
limit: u32,
955972
reg: Option<String>,
956973
) -> CargoResult<()> {
957-
let (mut registry, _, source_id) =
974+
let (mut registry, _, source_ids) =
958975
registry(config, None, index.as_deref(), reg.as_deref(), false, false)?;
959976
let (crates, total_crates) = registry.search(query, limit).with_context(|| {
960977
format!(
@@ -1011,7 +1028,7 @@ pub fn search(
10111028
&ColorSpec::new(),
10121029
);
10131030
} else if total_crates > limit && limit >= search_max_limit {
1014-
let extra = if source_id.is_crates_io() {
1031+
let extra = if source_ids.original.is_crates_io() {
10151032
format!(
10161033
" (go to https://crates.io/search?q={} to see more)",
10171034
percent_encode(query.as_bytes(), NON_ALPHANUMERIC)

0 commit comments

Comments
 (0)