Skip to content

Commit f9b6cbc

Browse files
committed
refactor: split get_source_id to some small functions
This would be useful if we want to reuse some logic of this function in the future.
1 parent 416a2b9 commit f9b6cbc

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

src/cargo/ops/registry/mod.rs

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,11 @@ fn get_source_id(
191191
gctx: &GlobalContext,
192192
reg_or_index: Option<&RegistryOrIndex>,
193193
) -> CargoResult<RegistrySourceIds> {
194-
let sid = match reg_or_index {
195-
None => SourceId::crates_io(gctx)?,
196-
Some(RegistryOrIndex::Index(url)) => SourceId::for_registry(url)?,
197-
Some(RegistryOrIndex::Registry(r)) => SourceId::alt_registry(gctx, r)?,
198-
};
199-
// Load source replacements that are built-in to Cargo.
200-
let builtin_replacement_sid = SourceConfigMap::empty(gctx)?
201-
.load(sid, &HashSet::new())?
202-
.replaced_source_id();
203-
let replacement_sid = SourceConfigMap::new(gctx)?
204-
.load(sid, &HashSet::new())?
205-
.replaced_source_id();
194+
let sid = get_initial_source_id(gctx, reg_or_index)?;
195+
let (builtin_replacement_sid, replacement_sid) = get_replacement_source_ids(gctx, sid)?;
196+
206197
if reg_or_index.is_none() && replacement_sid != builtin_replacement_sid {
207-
// Neither --registry nor --index was passed and the user has configured source-replacement.
208-
if let Some(replacement_name) = replacement_sid.alt_registry_key() {
209-
bail!("crates-io is replaced with remote registry {replacement_name};\ninclude `--registry {replacement_name}` or `--registry crates-io`");
210-
} else {
211-
bail!("crates-io is replaced with non-remote-registry source {replacement_sid};\ninclude `--registry crates-io` to use crates.io");
212-
}
198+
bail!(gen_replacement_error(replacement_sid));
213199
} else {
214200
Ok(RegistrySourceIds {
215201
original: sid,
@@ -218,6 +204,56 @@ fn get_source_id(
218204
}
219205
}
220206

207+
fn get_initial_source_id(
208+
gctx: &GlobalContext,
209+
reg_or_index: Option<&RegistryOrIndex>,
210+
) -> CargoResult<SourceId> {
211+
match reg_or_index {
212+
None => SourceId::crates_io(gctx),
213+
Some(reg_or_index) => get_initial_source_id_from_registry_or_index(gctx, reg_or_index),
214+
}
215+
}
216+
217+
fn get_initial_source_id_from_registry_or_index(
218+
gctx: &GlobalContext,
219+
reg_or_index: &RegistryOrIndex,
220+
) -> CargoResult<SourceId> {
221+
match reg_or_index {
222+
RegistryOrIndex::Index(url) => SourceId::for_registry(url),
223+
RegistryOrIndex::Registry(r) => SourceId::alt_registry(gctx, r),
224+
}
225+
}
226+
227+
fn get_replacement_source_ids(
228+
gctx: &GlobalContext,
229+
sid: SourceId,
230+
) -> CargoResult<(SourceId, SourceId)> {
231+
let builtin_replacement_sid = SourceConfigMap::empty(gctx)?
232+
.load(sid, &HashSet::new())?
233+
.replaced_source_id();
234+
let replacement_sid = SourceConfigMap::new(gctx)?
235+
.load(sid, &HashSet::new())?
236+
.replaced_source_id();
237+
Ok((builtin_replacement_sid, replacement_sid))
238+
}
239+
240+
fn gen_replacement_error(replacement_sid: SourceId) -> String {
241+
// Neither --registry nor --index was passed and the user has configured source-replacement.
242+
let error_message = if let Some(replacement_name) = replacement_sid.alt_registry_key() {
243+
format!(
244+
"crates-io is replaced with remote registry {};\ninclude `--registry {}` or `--registry crates-io`",
245+
replacement_name, replacement_name
246+
)
247+
} else {
248+
format!(
249+
"crates-io is replaced with non-remote-registry source {};\ninclude `--registry crates-io` to use crates.io",
250+
replacement_sid
251+
)
252+
};
253+
254+
error_message
255+
}
256+
221257
struct RegistrySourceIds {
222258
/// Use when looking up the auth token, or writing out `Cargo.lock`
223259
original: SourceId,

0 commit comments

Comments
 (0)