diff --git a/Cargo.toml b/Cargo.toml index 91f7ada..f7fa775 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ readme = "README.md" keywords = [] categories = ["development-tools"] license = "MIT/Apache-2.0" +edition = "2018" [badges] travis-ci = { repository = "DanielKeep/rust-build-helper" } @@ -26,4 +27,4 @@ members = [ ] [dependencies] -semver = "0.6.0" +semver = "0.9.0" diff --git a/src/lib.rs b/src/lib.rs index bca0d40..315b867 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,8 +53,7 @@ This should only be used on variables which are *guaranteed* to be defined by Ca */ macro_rules! env_var { ($name:expr) => { - ::std::env::var($name) - .expect(concat!($name, " environment variable is not set")) + ::std::env::var($name).expect(concat!($name, " environment variable is not set")) }; } @@ -65,8 +64,7 @@ This should only be used on variables which are *guaranteed* to be defined by Ca */ macro_rules! env_var_os { ($name:expr) => { - ::std::env::var_os($name) - .expect(concat!($name, " environment variable is not set")) + ::std::env::var_os($name).expect(concat!($name, " environment variable is not set")) }; } @@ -76,23 +74,22 @@ Reads, unwraps, and parses an environment variable. This should only be used on variables which are *guaranteed* to be defined by Cargo. */ macro_rules! parse_env_var { - (try: $name:expr, $ty_desc:expr) => { - { - ::std::env::var($name) - .ok() - .map(|v| v.parse() - .expect(&format!(concat!($name, " {:?} is not a valid ", $ty_desc), v)) - ) - } - }; - - ($name:expr, $ty_desc:expr) => { - { - let v = env_var!($name); - v.parse() - .expect(&format!(concat!($name, " {:?} is not a valid ", $ty_desc), v)) - } - }; + (try: $name:expr, $ty_desc:expr) => {{ + ::std::env::var($name).ok().map(|v| { + v.parse().expect(&format!( + concat!($name, " {:?} is not a valid ", $ty_desc), + v + )) + }) + }}; + + ($name:expr, $ty_desc:expr) => {{ + let v = env_var!($name); + v.parse().expect(&format!( + concat!($name, " {:?} is not a valid ", $ty_desc), + v + )) + }}; } /** @@ -150,7 +147,7 @@ impl FromStr for Atomic { if s == "ptr" { Ok(Atomic::Pointer) } else if let Ok(bits) = s.parse() { - Ok(Atomic::Integer { bits: bits }) + Ok(Atomic::Integer { bits }) } else { Err(InvalidInput(s.into())) } @@ -182,7 +179,7 @@ impl FromStr for Endianness { match s { "big" => Ok(Endianness::Big), "little" => Ok(Endianness::Little), - _ => Err(InvalidInput(s.into())) + _ => Err(InvalidInput(s.into())), } } } @@ -220,7 +217,7 @@ impl FromStr for LibKind { "static" => Ok(LibKind::Static), "dylib" => Ok(LibKind::DyLib), "framework" => Ok(LibKind::Framework), - _ => Err(InvalidInput(s.into())) + _ => Err(InvalidInput(s.into())), } } } @@ -250,7 +247,7 @@ impl FromStr for Profile { match s { "debug" => Ok(Profile::Debug), "release" => Ok(Profile::Release), - _ => Err(InvalidInput(s.into())) + _ => Err(InvalidInput(s.into())), } } } @@ -289,7 +286,7 @@ impl FromStr for SearchKind { "native" => Ok(SearchKind::Native), "framework" => Ok(SearchKind::Framework), "all" => Ok(SearchKind::All), - _ => Err(InvalidInput(s.into())) + _ => Err(InvalidInput(s.into())), } } } @@ -315,24 +312,29 @@ impl Triple { let os; { - let mut parts = triple.splitn(4, '-') - .map(|s| { - let off = subslice_offset(&triple, s); - off..(off + s.len()) - }); - - arch = parts.next().expect(&format!("could not find architecture in triple {:?}", triple)); - family = parts.next().expect(&format!("could not find family in triple {:?}", triple)); - os = parts.next().expect(&format!("could not find os in triple {:?}", triple)); + let mut parts = triple.splitn(4, '-').map(|s| { + let off = subslice_offset(&triple, s); + off..(off + s.len()) + }); + + arch = parts + .next() + .unwrap_or_else(|| panic!("could not find architecture in triple {:?}", triple)); + family = parts + .next() + .unwrap_or_else(|| panic!("could not find family in triple {:?}", triple)); + os = parts + .next() + .unwrap_or_else(|| panic!("could not find os in triple {:?}", triple)); env = parts.next(); } Triple { - triple: triple, - arch: arch, - env: env, - family: family, - os: os, + triple, + arch, + env, + family, + os, } } @@ -356,8 +358,7 @@ impl Triple { Values include `"gnu"`, `"msvc"`, `"musl"`, `"android"` *etc.* Value is `None` if the platform doesn't specify an environment. */ pub fn env(&self) -> Option<&str> { - self.env.as_ref() - .map(|s| &self.triple[s.clone()]) + self.env.as_ref().map(|s| &self.triple[s.clone()]) } /** @@ -428,7 +429,6 @@ pub mod cargo { Package features. */ pub mod features { - use std::ascii::AsciiExt; use std::env; /** @@ -459,9 +459,7 @@ pub mod cargo { Due to the name mangling used by Cargo, features are returned as all lower case, with hypens instead of underscores. */ pub fn all() -> Iter { - Iter { - iter: env::vars(), - } + Iter { iter: env::vars() } } /// Determine if a specific feature is enabled. @@ -475,7 +473,7 @@ pub mod cargo { .map(|c| match c { 'A'...'Z' => c.to_ascii_lowercase(), '_' => '-', - c => c + c => c, }) .collect() } @@ -485,7 +483,7 @@ pub mod cargo { .map(|c| match c { 'a'...'z' => c.to_ascii_uppercase(), '-' => '_', - c => c + c => c, }) .collect() } @@ -577,23 +575,29 @@ pub mod metadata { Functions for communicating with `rustc`. */ pub mod rustc { - use std::path::Path; - use ::{LibKind, SearchKind}; + use crate::{LibKind, SearchKind}; + use std::path::Path; /// Link a library into the output. pub fn link_lib>(link_kind: Option, name: P) { - println!("cargo:rustc-link-lib={}{}", - link_kind.map(|v| format!("{}=", v)) + println!( + "cargo:rustc-link-lib={}{}", + link_kind + .map(|v| format!("{}=", v)) .unwrap_or_else(|| "".into()), - name.as_ref().display()); + name.as_ref().display() + ); } /// Add a search directory. pub fn link_search>(link_kind: Option, path: P) { - println!("cargo:rustc-link-search={}{}", - link_kind.map(|v| format!("{}=", v)) + println!( + "cargo:rustc-link-search={}{}", + link_kind + .map(|v| format!("{}=", v)) .unwrap_or_else(|| "".into()), - path.as_ref().display()); + path.as_ref().display() + ); } /** @@ -649,15 +653,17 @@ pub mod target { */ #[cfg(feature = "nightly")] pub fn has_atomic() -> Option> { - env::var("CARGO_CFG_TARGET_HAS_ATOMIC") - .ok() - .map(|v| { - v.split(',') - .map(|s| s.parse() - .expect(&format!("CARGO_CFG_TARGET_HAS_ATOMIC \ - contained invalid atomic type {:?}", s))) - .collect() - }) + env::var("CARGO_CFG_TARGET_HAS_ATOMIC").ok().map(|v| { + v.split(',') + .map(|s| { + s.parse().expect(&format!( + "CARGO_CFG_TARGET_HAS_ATOMIC \ + contained invalid atomic type {:?}", + s + )) + }) + .collect() + }) } /** diff --git a/tests/pkgs.rs b/tests/pkgs.rs index c415aab..2f804c3 100644 --- a/tests/pkgs.rs +++ b/tests/pkgs.rs @@ -6,13 +6,21 @@ Licensed under the MIT license (see LICENSE or { - cargo!("build", "--manifest-path", concat!("tests/pkgs/", $name, "/Cargo.toml")) - .expect(concat!("failed to build command to build tests/pkgs/", $name)) - .succeeded(concat!("failed to build tests/pkgs/", $name)) + cargo!( + "build", + "--manifest-path", + concat!("tests/pkgs/", $name, "/Cargo.toml") + ) + .expect(concat!( + "failed to build command to build tests/pkgs/", + $name + )) + .succeeded(concat!("failed to build tests/pkgs/", $name)) }; } diff --git a/tests/pkgs/basic/build.rs b/tests/pkgs/basic/build.rs index 4118903..3483602 100644 --- a/tests/pkgs/basic/build.rs +++ b/tests/pkgs/basic/build.rs @@ -8,19 +8,17 @@ or distributed except according to those terms. */ extern crate build_helper; -use build_helper::*; use build_helper::semver::Version; +use build_helper::*; macro_rules! show { ($value:expr) => { println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $value); }; - ($value:expr, |$i:ident| $map:expr) => { - { - let $i = $value; - println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $map); - } - }; + ($value:expr, |$i:ident| $map:expr) => {{ + let $i = $value; + println!(concat!("DEBUG: ", stringify!($value), ": {:?}"), $map); + }}; } fn main() { @@ -28,15 +26,30 @@ fn main() { let _ = bin::cargo(); let _ = bin::rustc(); let _ = bin::rustdoc(); - assert_eq!(cargo::features::all().collect::>().sorted(), vec!["a", "default"]); + assert_eq!( + cargo::features::all().collect::>().sorted(), + vec!["a", "default"] + ); assert!(cargo::features::enabled("a")); assert!(!cargo::features::enabled("b")); assert!(cargo::features::enabled("default")); let _ = cargo::manifest::dir(); assert_eq!(cargo::manifest::links(), Some("awakening".into())); - assert_eq!(cargo::pkg::authors().sorted(), vec!["Daniel Keep ", "John Smith "]); - assert_eq!(cargo::pkg::description().unwrap_or("".into()), "A description of this crate."); - assert_eq!(cargo::pkg::homepage().unwrap_or("".into()), "http://example.org/basic.rs"); + assert_eq!( + cargo::pkg::authors().sorted(), + vec![ + "Daniel Keep ", + "John Smith " + ] + ); + assert_eq!( + cargo::pkg::description().unwrap_or("".into()), + "A description of this crate." + ); + assert_eq!( + cargo::pkg::homepage().unwrap_or("".into()), + "http://example.org/basic.rs" + ); assert_eq!(cargo::pkg::name(), "basic"); assert_eq!(cargo::pkg::version(), Version::parse("1.2.3-pre").unwrap()); let _ = debug(); diff --git a/tests/pkgs/cargo-env/build.rs b/tests/pkgs/cargo-env/build.rs index 34898cb..f4e4a54 100644 --- a/tests/pkgs/cargo-env/build.rs +++ b/tests/pkgs/cargo-env/build.rs @@ -6,11 +6,9 @@ Licensed under the MIT license (see LICENSE or = KNOWN_VARS.iter().cloned().collect(); diff --git a/tests/pkgs/empty.rs b/tests/pkgs/empty.rs index e69de29..8b13789 100644 --- a/tests/pkgs/empty.rs +++ b/tests/pkgs/empty.rs @@ -0,0 +1 @@ + diff --git a/tests/util/mod.rs b/tests/util.rs similarity index 96% rename from tests/util/mod.rs rename to tests/util.rs index e3d3d97..ed9696c 100644 --- a/tests/util/mod.rs +++ b/tests/util.rs @@ -6,6 +6,8 @@ Licensed under the MIT license (see LICENSE or { { @@ -22,7 +24,7 @@ macro_rules! cargo { cmd_str = format!("{:?}", cmd); cmd.output() - .map(::util::Output::from) + .map(crate::util::Output::from) }; println!("cargo cmd: {}", cmd_str);