Skip to content

Commit

Permalink
internal: use safe get_package
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Nov 28, 2024
1 parent 8e6c163 commit 9ba99a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
12 changes: 10 additions & 2 deletions crates/moon/src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,16 @@ fn run_build_internal(
)?;

if let Some(bin_alias) = cmd.bin_alias.clone() {
let pkg = module.get_package_by_name_mut(cmd.package.as_ref().unwrap());
pkg.bin_name = Some(bin_alias);
let pkg = module.get_package_by_name_mut_safe(cmd.package.as_ref().unwrap());
match pkg {
Some(pkg) => {
pkg.bin_name = Some(bin_alias);
}
_ => anyhow::bail!(format!(
"package `{}` not found",
cmd.package.as_ref().unwrap()
)),
}
}

moonutil::common::set_native_backend_link_flags(
Expand Down
25 changes: 15 additions & 10 deletions crates/mooncake/src/pkg/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,21 @@ pub(crate) fn install_impl(

let full_pkg_name = format!("{bin_mod_to_install}/{pkg_name}");

let pkg = module_db.get_package_by_name(&full_pkg_name);
build_and_install_bin_package(
&moon_path,
&bin_mod_path,
&full_pkg_name,
&install_path,
pkg.bin_target.to_backend_ext(),
bin_alias,
verbose,
)?;
let pkg = module_db.get_package_by_name_safe(&full_pkg_name);
match pkg {
Some(pkg) => {
build_and_install_bin_package(
&moon_path,
&bin_mod_path,
&full_pkg_name,
&install_path,
pkg.bin_target.to_backend_ext(),
bin_alias,
verbose,
)?;
}
_ => anyhow::bail!(format!("package `{}` not found", full_pkg_name)),
}
}
} else {
for (full_pkg_name, pkg) in module_db
Expand Down
8 changes: 6 additions & 2 deletions crates/moonutil/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ impl ModuleDB {
self.packages.get(name).unwrap()
}

pub fn get_package_by_name_mut(&mut self, name: &str) -> &mut Package {
self.packages.get_mut(name).unwrap()
pub fn get_package_by_name_safe(&self, name: &str) -> Option<&Package> {
self.packages.get(name)
}

pub fn get_package_by_name_mut_safe(&mut self, name: &str) -> Option<&mut Package> {
self.packages.get_mut(name)
}

pub fn get_package_by_path(&self, path: &Path) -> Option<&Package> {
Expand Down

0 comments on commit 9ba99a7

Please sign in to comment.