diff options
author | 2017-04-14 15:47:36 -0500 | |
---|---|---|
committer | 2017-04-16 16:11:12 -0500 | |
commit | c3e3c63e9582cf68278564ec003a6719829801ff (patch) | |
tree | bac8c76bf617b868597e837ebde6e3243a794407 | |
parent | cargo fmt with rustfmt 0.8.0 (diff) | |
download | cargo-ebuild-c3e3c63e9582cf68278564ec003a6719829801ff.tar.gz cargo-ebuild-c3e3c63e9582cf68278564ec003a6719829801ff.tar.bz2 cargo-ebuild-c3e3c63e9582cf68278564ec003a6719829801ff.zip |
add some helper methods to make the code clearer
These helper methods make it a bit clearer to read the main function and
understand what is going on. They'll provide valuable abstraction for
Cargo API updates.
-rw-r--r-- | src/main.rs | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index 2cda495..955859d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,35 @@ extern crate rustc_serialize; extern crate time; use cargo::{Config, CliError, CliResult}; -use cargo::core::Package; +use cargo::core::{Package, Resolve}; use cargo::core::registry::PackageRegistry; use cargo::ops; -use cargo::util::important_paths; +use cargo::util::{important_paths, CargoResult}; use std::error::Error; use std::fs::OpenOptions; use std::io::Write; use std::path::PathBuf; +/// Finds the root Cargo.toml of the workspace +fn workspace(config: &Config, manifest_path: Option<String>) -> CargoResult<Package> { + let root = important_paths::find_root_manifest_for_wd(manifest_path, config.cwd())?; + Package::for_path(&root, config) +} + +/// Generates a package registry by using the Cargo.lock or creating one as necessary +fn registry<'a>(config: &'a Config, package: &Package) -> CargoResult<PackageRegistry<'a>> { + let mut registry = PackageRegistry::new(config); + registry.add_sources(&[package.package_id().source_id().clone()])?; + Ok(registry) +} + +/// Resolve the packages necessary for the workspace +fn resolve<'a>(registry: &mut PackageRegistry, + package: &'a Package, + config: &'a Config) -> CargoResult<Resolve> { + ops::resolve_pkg(registry, package, config) +} + #[derive(RustcDecodable)] struct Options { flag_verbose: bool, @@ -37,14 +57,12 @@ Options: fn real_main(options: Options, config: &Config) -> CliResult<Option<()>> { try!(config.shell().set_verbosity(options.flag_verbose, options.flag_quiet)); - // Load the root package - let root = try!(important_paths::find_root_manifest_for_wd(None, config.cwd())); - let package = try!(Package::for_path(&root, config)); + // Load the workspace and current package + let package = workspace(config, None)?; // Resolve all dependencies (generate or use Cargo.lock as necessary) - let mut registry = PackageRegistry::new(config); - try!(registry.add_sources(&[package.package_id().source_id().clone()])); - let resolve = try!(ops::resolve_pkg(&mut registry, &package, config)); + let mut registry = registry(config, &package)?; + let resolve = resolve(&mut registry, &package, config)?; // build the crates the package needs let mut crates = resolve.iter() |