Skip to content

Commit

Permalink
[Reformat] Apply cargo clippy lints and RustFmt
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBowyer committed Nov 6, 2020
1 parent 18a6288 commit 435f71c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 50 deletions.
20 changes: 8 additions & 12 deletions impl/src/bin/cargo-raze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn main() -> Result<()> {
if options.flag_verbose.unwrap_or(false) {
println!("Loaded override settings: {:#?}", settings);
}

let mut metadata_fetcher: Box<dyn MetadataFetcher> = match options.flag_cargo_bin_path {
Some(ref p) => Box::new(CargoMetadataFetcher::new(p, /*use_tempdir: */ true)),
None => Box::new(CargoMetadataFetcher::default()),
Expand Down Expand Up @@ -121,10 +121,11 @@ fn main() -> Result<()> {
GenMode::Remote => {
if !dry_run {
let remote_dir = render_details.path_prefix.as_path().join("remote");

// Clean out the "remote" directory and guarantee that it exists
if remote_dir.exists() {
for entry in glob::glob(&format!("{}/BUILD*.bazel", &remote_dir.display().to_string()))? {
let build_glob = format!("{}/BUILD*.bazel", remote_dir.display());
for entry in glob::glob(&build_glob)? {
if let Ok(path) = entry {
fs::remove_file(path)?;
}
Expand All @@ -137,24 +138,19 @@ fn main() -> Result<()> {
fs::create_dir_all(
render_details
.path_prefix
.clone()
.as_path()
.join("lockfiles"),
)?;
}
}

bazel_renderer.render_remote_planned_build(&render_details, &planned_build)?
}, /* exhaustive, we control the definition */
} /* exhaustive, we control the definition */
// There are no file outputs to produce if `genmode` is Unspecified
GenMode::Unspecified => Vec::new(),
};

for FileOutputs {
path,
contents,
} in bazel_file_outputs
{
for FileOutputs { path, contents } in bazel_file_outputs {
if dry_run {
println!("{}:\n{}", path.display(), contents);
} else {
Expand Down Expand Up @@ -186,7 +182,7 @@ fn calculate_workspace_root(
Some(output) => {
prefix_path.clear();
prefix_path.push(output);
},
}
None => {
if new_behavior {
if let Some(workspace_root) = find_bazel_workspace_root() {
Expand All @@ -199,7 +195,7 @@ fn calculate_workspace_root(
);
}
}
},
}
}

Ok(prefix_path)
Expand Down
8 changes: 4 additions & 4 deletions impl/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl CargoMetadataFetcher {
log::debug!("Cloning binary dependency: {}", &name);
let mut cloner = cargo_clone::Cloner::new();
cloner
.set_registry_url(url.to_string().trim_end_matches("/"))
.set_registry_url(url.to_string().trim_end_matches('/'))
.set_out_dir(dir);

cloner.clone(
Expand Down Expand Up @@ -199,14 +199,14 @@ pub fn fetch_crate_checksum(index_url: &str, name: &str, version: &str) -> Resul
let crate_index = index
.open_or_clone()?
.crate_(name)
.ok_or(anyhow!("Failed to find crate '{}' in index", name))?;
.ok_or_else(|| anyhow!("Failed to find crate '{}' in index", name))?;

let (_index, crate_version) = crate_index
.versions()
.iter()
.enumerate()
.find(|(_, ver)| ver.version() == version)
.ok_or(anyhow!(
.ok_or_else(|| anyhow!(
"Failed to find version {} for crate {}",
version,
name
Expand Down Expand Up @@ -237,7 +237,7 @@ pub fn gather_binary_dep_info(
let mut bin_crate_files: HashMap<String, CargoWorkspaceFiles> = HashMap::new();
let mut bin_metadatas: Vec<Metadata> = Vec::new();

for (package, info) in (binary_deps).into_iter() {
for (package, info) in (binary_deps).iter() {
// Install the package into a temp dir so we can run get it's metadata
let (_src_dir, bin_workspace_files) = metadata_fetcher.fetch_crate_src(
registry_url,
Expand Down
2 changes: 1 addition & 1 deletion impl/src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'fetcher> BuildPlanner for BuildPlannerImpl<'fetcher> {
Ok(path) => path.as_ref(),
Err(err) => {
return Err(anyhow!(err.to_string()));
},
}
},
)?,
_ => BinaryDependencyInfo {
Expand Down
4 changes: 2 additions & 2 deletions impl/src/planning/crate_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ impl CrateCatalogEntry {
* Not for use except during planning as path is local to run location.
*/
pub fn expected_vendored_path(&self, workspace_path: &str) -> String {
let mut dir = find_bazel_workspace_root().unwrap_or(PathBuf::from("."));
let mut dir = find_bazel_workspace_root().unwrap_or_else(|| PathBuf::from("."));

// Trim the absolute label identifier from the start of the workspace path
dir.push(workspace_path.trim_start_matches('/'));

dir.push(VENDOR_DIR);
dir.push(&self.package_ident);

return dir.display().to_string();
dir.display().to_string()
}

/** Yields the expected location of the build file (relative to execution path). */
Expand Down
2 changes: 1 addition & 1 deletion impl/src/planning/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl BazelSpdxLicense {

/** Breaks apart a cargo license string and yields the available license types. */
pub fn get_license_from_str(cargo_license_str: &str) -> LicenseData {
if cargo_license_str.len() == 0 {
if cargo_license_str.is_empty() {
return LicenseData::default();
}

Expand Down
26 changes: 14 additions & 12 deletions impl/src/planning/subplanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ impl<'planner> WorkspaceSubplanner<'planner> {
checks::check_resolve_matches_packages(&bin_dep.metadata)?;
packages.extend(bin_dep.metadata.packages.iter());
}
},
}
GenMode::Vendored => {
checks::check_all_vendored(self.crate_catalog.entries(), &self.settings.workspace_path)?;
},
_ => { /* No checks to perform */ },
}
_ => { /* No checks to perform */ }
}

checks::warn_unused_settings(&self.settings.crates, &packages);
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<'planner> WorkspaceSubplanner<'planner> {
}
}

// Additionally, the binary dependencies need to have their checksums added as well in
// Additionally, the binary dependencies need to have their checksums added as well in
// Remote GenMode configurations. Vendored GenMode relies on the behavior of `cargo vendor`
// and doesn't perform any special logic to fetch binary dependency crates.
if self.settings.genmode == GenMode::Remote {
Expand Down Expand Up @@ -662,14 +662,16 @@ impl<'planner> CrateSubplanner<'planner> {
.components()
.map(|c| c.as_os_str().to_str())
.try_fold("".to_owned(), |res, v| Some(format!("{}/{}", res, v?)))
.ok_or(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{:?} contains non UTF-8 characters and is not a legal path in Bazel",
&target.src_path
),
))?
.trim_start_matches("/")
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{:?} contains non UTF-8 characters and is not a legal path in Bazel",
&target.src_path
),
)
})?
.trim_start_matches('/')
.to_owned();

for kind in &target.kind {
Expand Down
14 changes: 4 additions & 10 deletions impl/src/rendering/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ impl BazelRenderer {
])
.unwrap();

Self {
internal_renderer,
}
Self { internal_renderer }
}

pub fn render_crate(
Expand Down Expand Up @@ -149,7 +147,7 @@ fn include_additional_build_file(
"{}\n# Additional content from {}\n{}",
existing_contents, file_path, additional_content
))
},
}

None => Ok(existing_contents),
}
Expand Down Expand Up @@ -374,9 +372,7 @@ mod tests {
edition: "2015".to_owned(),
}],
build_script_target: None,
source_details: SourceDetails {
git_data: None,
},
source_details: SourceDetails { git_data: None },
sha256: None,
registry_url: "https://crates.io/api/v1/crates/test-binary/1.1.1/download".to_string(),
lib_target_name: None,
Expand Down Expand Up @@ -407,9 +403,7 @@ mod tests {
edition: "2015".to_owned(),
}],
build_script_target: None,
source_details: SourceDetails {
git_data: None,
},
source_details: SourceDetails { git_data: None },
sha256: None,
registry_url: "https://crates.io/api/v1/crates/test-binary/1.1.1/download".to_string(),
lib_target_name: Some("test_library".to_owned()),
Expand Down
1 change: 0 additions & 1 deletion impl/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ fn validate_settings(settings: &mut RazeSettings) -> Result<(), RazeError> {
)
.to_owned(),
}
.into(),
);
}

Expand Down
10 changes: 3 additions & 7 deletions impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn is_bazel_supported_platform(target: &str) -> (bool, bool) {
// If the target expression cannot be parsed it is not considered a Bazel platform
Err(_) => {
return (false, false);
},
}
};

let mut is_supported = false;
Expand All @@ -100,10 +100,7 @@ pub fn is_bazel_supported_platform(target: &str) -> (bool, bool) {
if expression.eval(|pred| {
match pred {
Predicate::Target(tp) => tp.matches(target_info),
Predicate::KeyValue {
key,
val,
} => (*key == "target") && (*val == target_info.triple),
Predicate::KeyValue { key, val } => (*key == "target") && (*val == target_info.triple),
// For now there is no other kind of matching
_ => false,
}
Expand Down Expand Up @@ -184,10 +181,9 @@ pub fn find_bazel_workspace_root() -> Option<PathBuf> {
};
}

return None;
None
}


pub struct PlatformDetails {
target_triple: String,
attrs: Vec<Cfg>,
Expand Down

0 comments on commit 435f71c

Please sign in to comment.