diff --git a/Cargo.toml b/Cargo.toml index 7cfdc120c5..e5e0b94eec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ name = "cargo-edit" readme = "README.md" repository = "https://github.com/killercup/cargo-edit" version = "0.3.1" +edition = "2018" [[bin]] name = "cargo-add" diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/bin/add/args.rs b/src/bin/add/args.rs index 8fd3865ffb..d712e06a20 100644 --- a/src/bin/add/args.rs +++ b/src/bin/add/args.rs @@ -5,7 +5,7 @@ use cargo_edit::{get_latest_dependency, CrateName}; use semver; use std::path::PathBuf; -use errors::*; +use crate::errors::*; #[derive(Debug, Deserialize)] /// Docopts input args. @@ -71,7 +71,8 @@ impl Args { /// Build dependencies from arguments pub fn parse_dependencies(&self) -> Result> { if !self.arg_crates.is_empty() { - return self.arg_crates + return self + .arg_crates .iter() .map(|crate_name| { Ok( diff --git a/src/bin/add/main.rs b/src/bin/add/main.rs index 179b963e45..588e3187f5 100644 --- a/src/bin/add/main.rs +++ b/src/bin/add/main.rs @@ -11,24 +11,18 @@ unused_qualifications )] -extern crate atty; -extern crate docopt; #[macro_use] extern crate error_chain; -extern crate semver; #[macro_use] extern crate serde_derive; -extern crate termcolor; +use crate::args::Args; +use cargo_edit::{Dependency, Manifest}; use std::io::Write; use std::process; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; -extern crate cargo_edit; -use cargo_edit::{Dependency, Manifest}; - mod args; -use args::Args; mod errors { error_chain! { @@ -52,7 +46,7 @@ mod errors { } } } -use errors::*; +use crate::errors::*; static USAGE: &'static str = r#" Usage: diff --git a/src/bin/rm/args.rs b/src/bin/rm/args.rs index c9e800088b..af311ed385 100644 --- a/src/bin/rm/args.rs +++ b/src/bin/rm/args.rs @@ -1,6 +1,6 @@ //! Handle `cargo rm` arguments -use errors::*; +use crate::errors::*; #[derive(Debug, Deserialize)] /// Docopts input args. diff --git a/src/bin/rm/main.rs b/src/bin/rm/main.rs index 16e8cefef5..4a409bf639 100644 --- a/src/bin/rm/main.rs +++ b/src/bin/rm/main.rs @@ -11,23 +11,18 @@ unused_qualifications )] -extern crate atty; -extern crate docopt; #[macro_use] extern crate error_chain; #[macro_use] extern crate serde_derive; -extern crate termcolor; +use crate::args::Args; +use cargo_edit::Manifest; use std::io::Write; use std::process; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; -extern crate cargo_edit; -use cargo_edit::Manifest; - mod args; -use args::Args; mod errors { error_chain! { @@ -39,7 +34,7 @@ mod errors { } } } -use errors::*; +use crate::errors::*; static USAGE: &'static str = r" Usage: diff --git a/src/bin/upgrade/main.rs b/src/bin/upgrade/main.rs index 821b6d7e8a..ec3528f54c 100644 --- a/src/bin/upgrade/main.rs +++ b/src/bin/upgrade/main.rs @@ -1,38 +1,37 @@ //! `cargo upgrade` #![warn( - missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts, - trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces, + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces, unused_qualifications )] -extern crate cargo_metadata; -extern crate docopt; #[macro_use] extern crate error_chain; #[macro_use] extern crate serde_derive; -extern crate toml_edit; +use crate::errors::*; +use cargo_edit::{find, get_latest_dependency, CrateName, Dependency, LocalManifest}; use std::collections::HashMap; use std::io::Write; use std::path::{Path, PathBuf}; use std::process; - -extern crate cargo_edit; -use cargo_edit::{find, get_latest_dependency, CrateName, Dependency, LocalManifest}; - -extern crate termcolor; use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; mod errors { - error_chain!{ + error_chain! { links { CargoEditLib(::cargo_edit::Error, ::cargo_edit::ErrorKind); CargoMetadata(::cargo_metadata::Error, ::cargo_metadata::ErrorKind); } } } -use errors::*; static USAGE: &'static str = r" Upgrade dependencies as specified in the local manifest file (i.e. Cargo.toml). @@ -119,8 +118,10 @@ impl Manifests { .find(|p| p.manifest_path == resolved_manifest_path) // If we have successfully got metadata, but our manifest path does not correspond to a // package, we must have been called against a virtual manifest. - .chain_err(|| "Found virtual manifest, but this command requires running against an \ - actual package in this workspace. Try adding `--all`.")?; + .chain_err(|| { + "Found virtual manifest, but this command requires running against an \ + actual package in this workspace. Try adding `--all`." + })?; Ok(Manifests(vec![(manifest, package.to_owned())])) } diff --git a/src/crate_name.rs b/src/crate_name.rs index b83add57fe..4f10f68fa6 100644 --- a/src/crate_name.rs +++ b/src/crate_name.rs @@ -1,9 +1,9 @@ //! Crate name parsing. use semver; -use errors::*; -use Dependency; -use {get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path}; +use crate::errors::*; +use crate::Dependency; +use crate::{get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path}; /// A crate specifier. This can be a plain name (e.g. `docopt`), a name and a versionreq (e.g. /// `docopt@^0.8`), a URL, or a path. diff --git a/src/errors.rs b/src/errors.rs index 2edfef135f..3780a16f92 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,4 @@ -error_chain!{ +error_chain! { errors { /// Failed to fetch crate from crates.io FetchVersionFailure { diff --git a/src/fetch.rs b/src/fetch.rs index ce92776ab2..6ba109a334 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,3 +1,4 @@ +use crate::{Dependency, Manifest}; use env_proxy; use regex::Regex; use reqwest; @@ -7,9 +8,8 @@ use std::env; use std::io::Read; use std::path::Path; use std::time::Duration; -use {Dependency, Manifest}; -use errors::*; +use crate::errors::*; const REGISTRY_HOST: &str = "https://crates.io"; @@ -97,7 +97,8 @@ fn get_latest_stable_version_from_json() { } ] }"#, - ).expect("crate version is correctly parsed"); + ) + .expect("crate version is correctly parsed"); assert_eq!( read_latest_version(&versions, false) @@ -125,7 +126,8 @@ fn get_latest_unstable_or_stable_version_from_json() { } ] }"#, - ).expect("crate version is correctly parsed"); + ) + .expect("crate version is correctly parsed"); assert_eq!( read_latest_version(&versions, true) @@ -153,7 +155,8 @@ fn get_latest_version_from_json_test() { } ] }"#, - ).expect("crate version is correctly parsed"); + ) + .expect("crate version is correctly parsed"); assert_eq!( read_latest_version(&versions, false) @@ -181,7 +184,8 @@ fn get_no_latest_version_from_json_when_all_are_yanked() { } ] }"#, - ).expect("crate version is correctly parsed"); + ) + .expect("crate version is correctly parsed"); assert!(read_latest_version(&versions, false).is_err()); } diff --git a/src/lib.rs b/src/lib.rs index 4cc05dc566..f85b90f0dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,24 +1,21 @@ //! Show and Edit Cargo's Manifest Files #![cfg_attr(test, allow(dead_code))] #![warn( - missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts, - trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces, + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces, unused_qualifications )] -extern crate cargo_metadata; -extern crate env_proxy; #[macro_use] extern crate error_chain; -extern crate regex; -extern crate reqwest; -extern crate semver; -extern crate serde; #[macro_use] extern crate serde_derive; -extern crate serde_json; -extern crate termcolor; -extern crate toml_edit; mod crate_name; mod dependency; @@ -26,9 +23,11 @@ mod errors; mod fetch; mod manifest; -pub use crate_name::CrateName; -pub use dependency::Dependency; -pub use errors::*; -pub use fetch::{get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path, - get_latest_dependency}; -pub use manifest::{find, LocalManifest, Manifest}; +pub use crate::crate_name::CrateName; +pub use crate::dependency::Dependency; +pub use crate::errors::*; +pub use crate::fetch::{ + get_crate_name_from_github, get_crate_name_from_gitlab, get_crate_name_from_path, + get_latest_dependency, +}; +pub use crate::manifest::{find, LocalManifest, Manifest}; diff --git a/src/manifest.rs b/src/manifest.rs index c2da8b113d..d1ae5c330f 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -7,8 +7,8 @@ use std::{env, str}; use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; use toml_edit; -use dependency::Dependency; -use errors::*; +use crate::dependency::Dependency; +use crate::errors::*; const MANIFEST_FILENAME: &str = "Cargo.toml"; @@ -52,7 +52,8 @@ fn search(dir: &Path) -> Result { } fn merge_inline_table(old_dep: &mut toml_edit::Item, new: &toml_edit::Item) { - for (k, v) in new.as_inline_table() + for (k, v) in new + .as_inline_table() .expect("expected an inline table") .iter() { @@ -127,7 +128,8 @@ fn print_upgrade_if_necessary( &mut buffer, "{} v{} -> v{}", crate_name, old_version, new_version, - ).chain_err(|| "Failed to write upgrade versions")?; + ) + .chain_err(|| "Failed to write upgrade versions")?; bufwtr .print(&buffer) .chain_err(|| "Failed to print upgrade message")?; @@ -198,7 +200,8 @@ impl Manifest { } // ... and in `target..(build-/dev-)dependencies`. - let target_sections = self.data + let target_sections = self + .data .as_table() .get("target") .and_then(toml_edit::Item::as_table_like) @@ -294,8 +297,6 @@ impl Manifest { /// # Examples /// /// ``` - /// # extern crate cargo_edit; - /// # extern crate toml_edit; /// # fn main() { /// use cargo_edit::{Dependency, Manifest}; /// use toml_edit; @@ -411,7 +412,7 @@ impl LocalManifest { #[cfg(test)] mod tests { use super::*; - use dependency::Dependency; + use crate::dependency::Dependency; use toml_edit; #[test] @@ -422,11 +423,9 @@ mod tests { let clone = manifest.clone(); let dep = Dependency::new("cargo-edit").set_version("0.1.0"); let _ = manifest.insert_into_table(&["dependencies".to_owned()], &dep); - assert!( - manifest - .remove_from_table("dependencies", &dep.name) - .is_ok() - ); + assert!(manifest + .remove_from_table("dependencies", &dep.name) + .is_ok()); assert_eq!(manifest.data.to_string(), clone.data.to_string()); } @@ -471,11 +470,9 @@ mod tests { data: toml_edit::Document::new(), }; let dep = Dependency::new("cargo-edit").set_version("0.1.0"); - assert!( - manifest - .remove_from_table("dependencies", &dep.name) - .is_err() - ); + assert!(manifest + .remove_from_table("dependencies", &dep.name) + .is_err()); } #[test] @@ -486,10 +483,8 @@ mod tests { let dep = Dependency::new("cargo-edit").set_version("0.1.0"); let other_dep = Dependency::new("other-dep").set_version("0.1.0"); let _ = manifest.insert_into_table(&["dependencies".to_owned()], &other_dep); - assert!( - manifest - .remove_from_table("dependencies", &dep.name) - .is_err() - ); + assert!(manifest + .remove_from_table("dependencies", &dep.name) + .is_err()); } } diff --git a/tests/cargo-add.rs b/tests/cargo-add.rs index b9611ab95f..756072c130 100644 --- a/tests/cargo-add.rs +++ b/tests/cargo-add.rs @@ -1,11 +1,9 @@ -extern crate assert_cli; #[macro_use] extern crate pretty_assertions; -extern crate toml_edit; use std::process; mod utils; -use utils::{clone_out_test, execute_command, get_toml}; +use crate::utils::{clone_out_test, execute_command, get_toml}; /// Some of the tests need to have a crate name that does not exist on crates.io. Hence this rather /// silly constant. Tests _will_ fail, though, if a crate is ever published with this name. @@ -14,7 +12,8 @@ const BOGUS_CRATE_NAME: &str = "tests-will-break-if-there-is-ever-a-real-package /// Check 'failure' deps are not present fn no_manifest_failures(manifest: &toml_edit::Item) -> bool { let no_failure_key_in = |section| manifest[section][BOGUS_CRATE_NAME].is_none(); - no_failure_key_in("dependencies") && no_failure_key_in("dev-dependencies") + no_failure_key_in("dependencies") + && no_failure_key_in("dev-dependencies") && no_failure_key_in("build-dependencies") } @@ -705,11 +704,12 @@ fn adds_dependency_normalized_name() { "add", "linked_hash_map", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .contains("WARN: Added `linked-hash-map` instead of `linked_hash_map`") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .contains("WARN: Added `linked-hash-map` instead of `linked_hash_map`") + .unwrap(); // dependency present afterwards let toml = get_toml(&manifest); @@ -808,7 +808,9 @@ version = "0.0.0" [lib] path = "dummy.rs" -"#.to_string() + expected; +"# + .to_string() + + expected; let expected_dep: toml_edit::Document = expected.parse().expect("toml parse error"); assert_eq!(expected_dep.to_string(), toml.to_string()); } @@ -921,11 +923,12 @@ fn add_prints_message() { "docopt", "--vers=0.6.0", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .is("Adding docopt v0.6.0 to dependencies") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .is("Adding docopt v0.6.0 to dependencies") + .unwrap(); } #[test] @@ -940,11 +943,12 @@ fn add_prints_message_with_section() { "--target=mytarget", "--vers=0.1.0", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .is("Adding clap v0.1.0 to optional dependencies for target `mytarget`") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .is("Adding clap v0.1.0 to optional dependencies for target `mytarget`") + .unwrap(); } #[test] @@ -959,11 +963,12 @@ fn add_prints_message_for_dev_deps() { "--vers", "0.8.0", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .is("Adding docopt v0.8.0 to dev-dependencies") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .is("Adding docopt v0.8.0 to dev-dependencies") + .unwrap(); } #[test] @@ -978,11 +983,12 @@ fn add_prints_message_for_build_deps() { "--vers", "0.1.0", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .is("Adding hello-world v0.1.0 to build-dependencies") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .is("Adding hello-world v0.1.0 to build-dependencies") + .unwrap(); } #[test] @@ -995,12 +1001,13 @@ fn add_typo() { "add", "lets_hope_nobody_ever_publishes_this_crate", &format!("--manifest-path={}", manifest), - ]).fails_with(1) - .and() - .stderr() - .contains( - "The crate `lets_hope_nobody_ever_publishes_this_crate` could not be found \ - on crates.io.", - ) - .unwrap(); + ]) + .fails_with(1) + .and() + .stderr() + .contains( + "The crate `lets_hope_nobody_ever_publishes_this_crate` could not be found \ + on crates.io.", + ) + .unwrap(); } diff --git a/tests/cargo-rm.rs b/tests/cargo-rm.rs index ae0ad285ea..791f003499 100644 --- a/tests/cargo-rm.rs +++ b/tests/cargo-rm.rs @@ -1,7 +1,5 @@ -extern crate assert_cli; - mod utils; -use utils::{clone_out_test, execute_command, get_toml}; +use crate::utils::{clone_out_test, execute_command, get_toml}; #[test] fn remove_existing_dependency() { diff --git a/tests/cargo-upgrade.rs b/tests/cargo-upgrade.rs index 06660c5ccc..66c4c78e9f 100644 --- a/tests/cargo-upgrade.rs +++ b/tests/cargo-upgrade.rs @@ -1,13 +1,10 @@ -extern crate assert_cli; #[macro_use] extern crate pretty_assertions; -extern crate tempdir; -extern crate toml_edit; use std::fs; mod utils; -use utils::{clone_out_test, execute_command, get_toml}; +use crate::utils::{clone_out_test, execute_command, get_toml}; /// Helper function that copies the workspace test into a temporary directory. pub fn copy_workspace_test() -> (tempdir::TempDir, String, Vec) { @@ -33,7 +30,8 @@ pub fn copy_workspace_test() -> (tempdir::TempDir, String, Vec) { fs::copy( format!("tests/fixtures/workspace/{}/{}", dir, file), &file_path, - ).unwrap_or_else(|err| panic!("could not copy test file: {}", err)); + ) + .unwrap_or_else(|err| panic!("could not copy test file: {}", err)); file_path }; @@ -233,14 +231,15 @@ fn detect_workspace() { "upgrade", "--manifest-path", &root_manifest, - ]).fails_with(1) - .and() - .stderr() - .is( - "Command failed due to unhandled error: Found virtual manifest, but this command \ - requires running against an actual package in this workspace. Try adding `--all`.", - ) - .unwrap(); + ]) + .fails_with(1) + .and() + .stderr() + .is( + "Command failed due to unhandled error: Found virtual manifest, but this command \ + requires running against an actual package in this workspace. Try adding `--all`.", + ) + .unwrap(); } #[test] @@ -252,11 +251,12 @@ fn invalid_manifest() { "upgrade", "--manifest-path", &manifest, - ]).fails_with(1) - .and() - .stderr() - .is( - "Command failed due to unhandled error: Unable to parse Cargo.toml + ]) + .fails_with(1) + .and() + .stderr() + .is( + "Command failed due to unhandled error: Unable to parse Cargo.toml Caused by: Manifest not valid TOML Caused by: TOML parse error at line 1, column 6 @@ -265,8 +265,8 @@ Caused by: TOML parse error at line 1, column 6 | ^ Unexpected `i` Expected `=`", - ) - .unwrap(); + ) + .unwrap(); } #[test] @@ -279,11 +279,12 @@ fn invalid_root_manifest() { "--all", "--manifest-path", &manifest, - ]).fails_with(1) - .and() - .stderr() - .contains("Command failed due to unhandled error: Failed to get workspace metadata") - .unwrap(); + ]) + .fails_with(1) + .and() + .stderr() + .contains("Command failed due to unhandled error: Failed to get workspace metadata") + .unwrap(); } #[test] @@ -310,9 +311,10 @@ fn upgrade_prints_messages() { "upgrade", "docopt", &format!("--manifest-path={}", manifest), - ]).succeeds() - .and() - .stdout() - .contains("docopt v0.8 -> v") - .unwrap(); + ]) + .succeeds() + .and() + .stdout() + .contains("docopt v0.8 -> v") + .unwrap(); } diff --git a/tests/test_manifest.rs b/tests/test_manifest.rs index a4c05e00b3..cd93b12dab 100644 --- a/tests/test_manifest.rs +++ b/tests/test_manifest.rs @@ -1,5 +1,3 @@ -extern crate assert_cli; - #[test] fn invalid_manifest() { assert_cli::Assert::command(&[ @@ -7,11 +5,12 @@ fn invalid_manifest() { "add", "foo", "--manifest-path=tests/fixtures/manifest-invalid/Cargo.toml.sample", - ]).fails_with(1) - .and() - .stderr() - .is( - r#"Command failed due to unhandled error: Unable to parse Cargo.toml + ]) + .fails_with(1) + .and() + .stderr() + .is( + r#"Command failed due to unhandled error: Unable to parse Cargo.toml Caused by: Manifest not valid TOML Caused by: TOML parse error at line 6, column 7 @@ -26,6 +25,6 @@ While parsing a Time While parsing a Date-Time While parsing a Float While parsing an Integer"#, - ) - .unwrap(); + ) + .unwrap(); } diff --git a/tests/utils.rs b/tests/utils.rs index 9df68dc205..4d177ac947 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -1,6 +1,3 @@ -extern crate tempdir; -extern crate toml_edit; - use std::ffi::OsStr; use std::io::prelude::*; use std::{fs, process};