From 59000ef17b17e25d7e8418b32c1c85b5dd2e3a63 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sat, 15 Jun 2024 23:12:31 -0400 Subject: [PATCH] More small cleanups, hopefully last round --- src/command.rs | 41 ++++++++++++++++------------------------- src/lib.rs | 6 +++--- src/main.rs | 10 ++++++++-- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/command.rs b/src/command.rs index d5a5394..35f2c32 100644 --- a/src/command.rs +++ b/src/command.rs @@ -301,24 +301,14 @@ impl CargoCmd { /// /// - `cargo 3ds build` and other "build" commands will use their callbacks to build the final `.3dsx` file and link it. /// - `cargo 3ds new` and other generic commands will use their callbacks to make 3ds-specific changes to the environment. - pub fn run_callbacks(&self, messages: &[Message], metadata: &Option) { - let max_artifact_count = if let Some(metadata) = metadata { - metadata.packages.iter().map(|pkg| pkg.targets.len()).sum() - } else { - 0 - }; - - let mut configs = Vec::with_capacity(max_artifact_count); - - // Process the metadata only for commands that have it/use it - if self.should_build_3dsx() { - // unwrap: we should always have metadata if the command should compile something - configs = self.build_callbacks(messages, metadata.as_ref().unwrap()); - } + pub fn run_callbacks(&self, messages: &[Message], metadata: Option<&Metadata>) { + let configs = metadata + .map(|metadata| self.build_callbacks(messages, metadata)) + .unwrap_or_default(); let config = match self { // If we produced one executable, we will attempt to run that one - _ if configs.len() == 1 => configs.remove(0), + _ if configs.len() == 1 => configs.into_iter().next().unwrap(), // --no-run may produce any number of executables, and we skip the callback Self::Test(Test { no_run: true, .. }) => return, @@ -353,7 +343,8 @@ impl CargoCmd { /// Generate a .3dsx for every executable artifact within the workspace that /// was built by the cargo command. fn build_callbacks(&self, messages: &[Message], metadata: &Metadata) -> Vec { - let mut configs = Vec::new(); + let max_artifact_count = metadata.packages.iter().map(|pkg| pkg.targets.len()).sum(); + let mut configs = Vec::with_capacity(max_artifact_count); for message in messages { let Message::CompilerArtifact(artifact) = message else { @@ -377,25 +368,25 @@ impl CargoCmd { configs } - fn inner_callbacks(&self) -> Option<&dyn Callbacks> { - Some(match self { - Self::Build(cmd) => cmd, - Self::Run(cmd) => cmd, - Self::Test(cmd) => cmd, - _ => return None, - }) + fn inner_callback(&self) -> Option<&dyn Callbacks> { + match self { + Self::Build(cmd) => Some(cmd), + Self::Run(cmd) => Some(cmd), + Self::Test(cmd) => Some(cmd), + _ => None, + } } } impl Callbacks for CargoCmd { fn build_callback(&self, config: &CTRConfig) { - if let Some(cb) = self.inner_callbacks() { + if let Some(cb) = self.inner_callback() { cb.build_callback(config); } } fn run_callback(&self, config: &CTRConfig) { - if let Some(cb) = self.inner_callbacks() { + if let Some(cb) = self.inner_callback() { cb.run_callback(config); } } diff --git a/src/lib.rs b/src/lib.rs index 4f6026f..43d2753 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,7 +266,7 @@ pub fn get_artifact_config(package: Package, artifact: Artifact) -> CTRConfig { _ => artifact.target.name, }; - // TODO: need to break down by target kind and name, e.g. + // TODO(#62): need to break down by target kind and name, e.g. // [package.metadata.cargo-3ds.example.hello-world] // Probably fall back to top level as well. let config = package @@ -413,8 +413,8 @@ impl CTRConfig { Self::DEFAULT_AUTHOR.to_string() }; - let icon_path = self.icon_path().unwrap_or_else(|err| { - eprintln!("Icon at {err} does not exist"); + let icon_path = self.icon_path().unwrap_or_else(|err_path| { + eprintln!("Icon at {err_path} does not exist"); process::exit(1); }); diff --git a/src/main.rs b/src/main.rs index c6011eb..63140ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,13 @@ fn main() { }; let metadata = if input.cmd.should_build_3dsx() { - cargo_metadata::MetadataCommand::new().no_deps().exec().ok() + match cargo_metadata::MetadataCommand::new().no_deps().exec() { + Ok(metadata) => Some(metadata), + Err(err) => { + eprintln!("Warning: failed to gather cargo metadata for the project: {err}"); + None + } + } } else { None }; @@ -30,5 +36,5 @@ fn main() { process::exit(status.code().unwrap_or(1)); } - input.cmd.run_callbacks(&messages, &metadata); + input.cmd.run_callbacks(&messages, metadata.as_ref()); }