Skip to content

Commit 5e17913

Browse files
committed
Switched to cargo's Source::download_now.
Closes rust-lang#90. Instead of rolling our own, complicated, download machinery, we now reuse a bit mroe code from cargo.
1 parent 8361ace commit 5e17913

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

src/bin/cargo_semver.rs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ extern crate serde;
88
extern crate serde_derive;
99
extern crate serde_json;
1010

11-
use cargo::core::{Package, PackageId, PackageSet, Source, SourceId, SourceMap, Workspace};
11+
use cargo::core::{Package, PackageId, Source, SourceId, Workspace};
12+
use cargo::sources::RegistrySource;
1213
use curl::easy::Easy;
1314
use log::debug;
1415
use rand::Rng;
@@ -94,7 +95,6 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
9495
// -C "name:version" requires fetching the appropriate package:
9596
WorkInfo::remote(
9697
config,
97-
SourceInfo::new(config)?,
9898
&PackageNameAndVersion::parse(&name_and_version)?,
9999
)?
100100
} else if let Some(path) = matches.opt_str("c").map(PathBuf::from) {
@@ -112,7 +112,7 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
112112
// -S "name:version" requires fetching the appropriate package:
113113
let info = PackageNameAndVersion::parse(&name_and_version)?;
114114
let version = info.version.to_owned();
115-
let work_info = WorkInfo::remote(config, SourceInfo::new(config)?, &info)?;
115+
let work_info = WorkInfo::remote(config, &info)?;
116116
(work_info, version)
117117
} else if let Some(path) = matches.opt_str("s") {
118118
// -s "local_path":
@@ -127,7 +127,7 @@ fn run(config: &cargo::Config, matches: &getopts::Matches, explain: bool) -> Res
127127
name: &name,
128128
version: &stable_crate.max_version,
129129
};
130-
let work_info = WorkInfo::remote(config, SourceInfo::new(config)?, &info)?;
130+
let work_info = WorkInfo::remote(config, &info)?;
131131
(work_info, stable_crate.max_version.clone())
132132
};
133133

@@ -341,33 +341,6 @@ impl<'a> PackageNameAndVersion<'a> {
341341
}
342342
}
343343

344-
/// A specification of a package source to fetch remote packages from.
345-
pub struct SourceInfo<'a> {
346-
/// The source id to be used.
347-
id: SourceId,
348-
/// The source to be used.
349-
source: Box<dyn Source + 'a>,
350-
}
351-
352-
impl<'a> SourceInfo<'a> {
353-
/// Construct a new source info for `crates.io`.
354-
pub fn new(config: &'a cargo::Config) -> Result<SourceInfo<'a>> {
355-
let source_id = SourceId::crates_io(config)?;
356-
let mut source = source_id.load(config, &HashSet::new())?;
357-
358-
debug!("source id loaded: {:?}", source_id);
359-
360-
// Update the index. Without this we would not be able to fetch recent packages if the
361-
// index is not up-to-date.
362-
source.update()?;
363-
364-
Ok(Self {
365-
id: source_id,
366-
source,
367-
})
368-
}
369-
}
370-
371344
/// A specification of a package and it's workspace.
372345
pub struct WorkInfo<'a> {
373346
/// The package to be compiled.
@@ -388,25 +361,29 @@ impl<'a> WorkInfo<'a> {
388361
/// specified `PackageNameAndVersion` from the `source`.
389362
pub fn remote(
390363
config: &'a cargo::Config,
391-
source: SourceInfo<'a>,
392364
&PackageNameAndVersion { name, version }: &PackageNameAndVersion,
393365
) -> Result<WorkInfo<'a>> {
366+
let source = {
367+
let source_id = SourceId::crates_io(&config)?;
368+
let mut source = RegistrySource::remote(source_id, &HashSet::new(), config);
369+
370+
debug!("source id loaded: {:?}", source_id);
371+
372+
source.update()?;
373+
374+
Box::new(source)
375+
};
376+
394377
// TODO: fall back to locally cached package instance, or better yet, search for it
395378
// first.
396-
let package_ids = [PackageId::new(name, version, source.id)?];
397-
debug!("(remote) package id: {:?}", package_ids[0]);
398-
let sources = {
399-
let mut s = SourceMap::new();
400-
s.insert(source.source);
401-
s
402-
};
379+
let package_id = PackageId::new(name, version, source.source_id())?;
380+
debug!("(remote) package id: {:?}", package_id);
403381

404-
let package_set = PackageSet::new(&package_ids, sources, config)?;
405-
let package = package_set.get_one(package_ids[0])?;
382+
let package = source.download_now(package_id, config)?;
406383
let workspace = Workspace::ephemeral(package.clone(), config, None, false)?;
407384

408385
Ok(Self {
409-
package: package.clone(),
386+
package: package,
410387
workspace,
411388
})
412389
}

0 commit comments

Comments
 (0)