Skip to content

Commit

Permalink
Clippy & optimisations (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook authored Apr 30, 2024
1 parent 8d6675a commit 5f03a23
Show file tree
Hide file tree
Showing 33 changed files with 297 additions and 337 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-playdate"
version = "0.4.8"
version = "0.4.9"
readme = "README.md"
description = "Build tool for neat yellow console."
keywords = ["playdate", "build", "cargo", "plugin", "cargo-subcommand"]
Expand Down
36 changes: 15 additions & 21 deletions cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub type AssetsArtifacts<'cfg> = HashMap<&'cfg Package, AssetsArtifact<'cfg>>;


pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
let bcx = LazyBuildContext::new(&config)?;
let bcx = LazyBuildContext::new(config)?;
let mut artifacts = AssetsArtifacts::new();

for (package, targets, ..) in config.possible_targets()? {
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
.map(|plan| (plan, AssetKind::Package))
.chain(plan.dev.as_ref().into_iter().map(|plan| (plan, AssetKind::Dev)))
{
let message = plan.printable_serializable(&package, kind);
let message = plan.printable_serializable(package, kind);
config.workspace.config().shell().print_json(&message)?;
}
}
Expand All @@ -127,7 +127,7 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
.chain(plan.dev.as_ref().into_iter())
{
shell.status("Assets", format!("build plan for {}", package.package_id()))?;
plan.pretty_print(shell, &config.workspace.root())?;
plan.pretty_print(shell, config.workspace.root())?;
}
}
Ok(())
Expand Down Expand Up @@ -174,11 +174,11 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {

for (package, plan) in plans.iter() {
if let Some(plan) = plan.main.as_ref() {
check_duplicates(*package, AssetKind::Package, plan.as_inner().targets());
check_duplicates(package, AssetKind::Package, plan.as_inner().targets());
}
if package.package_id() == target_pid {
if let Some(plan) = plan.dev.as_ref() {
check_duplicates(*package, AssetKind::Dev, plan.as_inner().targets());
check_duplicates(package, AssetKind::Dev, plan.as_inner().targets());
}
}
}
Expand Down Expand Up @@ -264,10 +264,8 @@ pub fn build<'cfg>(config: &'cfg Config) -> CargoResult<AssetsArtifacts<'cfg>> {
}
}

if report.has_errors() {
if !config.compile_options.build_config.keep_going {
bail!("Assets build failed.");
}
if report.has_errors() && !config.compile_options.build_config.keep_going {
bail!("Assets build failed.");
}


Expand Down Expand Up @@ -383,7 +381,7 @@ fn deps_tree_metadata<'cfg: 'r, 't: 'r, 'r>(package: &'cfg Package,
if let Some(deps) = root.and_then(|root| bcx.unit_graph.get(root)) {
// find all dependencies:
dependencies.extend(
deps.into_iter()
deps.iter()
.flat_map(|ud| bcx.unit_graph.get_key_value(&ud.unit).map(|(u, _)| u)),
);

Expand All @@ -395,11 +393,11 @@ fn deps_tree_metadata<'cfg: 'r, 't: 'r, 'r>(package: &'cfg Package,
let next = dependencies.iter()
.flat_map(|u| {
bcx.unit_graph.get(u).into_iter().flat_map(|deps| {
deps.into_iter().flat_map(|ud| {
bcx.unit_graph
deps.iter().flat_map(|ud| {
bcx.unit_graph
.get_key_value(&ud.unit)
.map(|(u, _)| u)
})
})
})
})
.filter(|u| !dependencies.contains(u))
Expand Down Expand Up @@ -436,8 +434,7 @@ fn deps_tree_metadata<'cfg: 'r, 't: 'r, 'r>(package: &'cfg Package,
dependencies.retain(|u| {
let key = (u.pkg.name(), u.pkg.package_id().source_id());
id_ver.get(&key)
.map(|vec| vec.last())
.flatten()
.and_then(|vec| vec.last())
.map(|(_, u)| *u)
.filter(|u1| u == u1)
.is_some()
Expand Down Expand Up @@ -466,11 +463,8 @@ fn deps_tree_metadata<'cfg: 'r, 't: 'r, 'r>(package: &'cfg Package,
pub fn playdate_metadata(package: &Package) -> Option<TomlMetadata> {
package.manifest()
.custom_metadata()
.map(|m| m.as_table().map(|t| t.get(METADATA_FIELD)))
.flatten()
.flatten()
.map(|v| v.to_owned().try_into::<TomlMetadata>().log_err().ok())
.flatten()
.map(|mut m| m.merge_opts().map(|_| m).log_err().ok())
.and_then(|m| m.as_table().map(|t| t.get(METADATA_FIELD)))
.flatten()
.and_then(|v| v.to_owned().try_into::<TomlMetadata>().log_err().ok())
.and_then(|mut m| m.merge_opts().map(|_| m).log_err().ok())
}
6 changes: 3 additions & 3 deletions cargo/src/assets/pdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn build(config: &Config,
let src = layout.assets_dev();
let build = layout.build_dev();
std::fs::create_dir(&build).ok();
(src.into(), build.into())
(src, build)
},
};

Expand All @@ -50,10 +50,10 @@ fn build_in(config: &Config, package: &Package, src: &Path, build: &Path, root:
.unwrap_or_default();
let dst = build.with_file_name(format!("{prefix}.build.pdx"));

soft_link_checked(&build, &dst, true, root)?;
soft_link_checked(build, &dst, true, root)?;

// prepare src for pdc:
std::fs::write(&src.join("pdex.bin"), &[])?;
std::fs::write(src.join("pdex.bin"), [])?;

let mut cmd = Command::new(config.sdk()?.pdc());
if config.skip_unknown {
Expand Down
11 changes: 5 additions & 6 deletions cargo/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn plan_for<'cfg, 'env, 'l>(config: &'cfg Config,
.ok_or(anyhow!("No parent of manifest-path"))?;

let main = if !metadata.assets.is_empty() {
let plan = assets_build_plan(&env, &metadata.assets, opts.as_ref(), Some(root))?;
let plan = assets_build_plan(env, &metadata.assets, opts.as_ref(), Some(root))?;

// main-assets plan:
let path = layout.as_inner().assets_plan_for(config, package);
Expand All @@ -107,7 +107,7 @@ pub fn plan_for<'cfg, 'env, 'l>(config: &'cfg Config,
// dev-assets plan:
let dev = if has_dev_assets && metadata.dev_assets.is_some() {
let assets = metadata.dev_assets.as_ref().unwrap();
let dev_plan = assets_build_plan(&env, assets, opts.as_ref(), Some(root))?;
let dev_plan = assets_build_plan(env, assets, opts.as_ref(), Some(root))?;

let path = layout.as_inner().assets_plan_for_dev(config, package);
let mut dev_cached = CachedPlan::new(path, dev_plan)?;
Expand Down Expand Up @@ -164,7 +164,7 @@ pub struct CachedPlan<'t, 'cfg> {


impl<'t, 'cfg> CachedPlan<'t, 'cfg> {
#[must_use]
#[must_use = "Cached plan must be used"]
fn new(path: PathBuf, plan: AssetsPlan<'t, 'cfg>) -> CargoResult<Self> {
let mut serializable = plan.serializable_flatten().collect::<Vec<_>>();
serializable.sort_by_key(|(_, (p, _))| p.to_string_lossy().to_string());
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'t, 'cfg> CachedPlan<'t, 'cfg> {
config: &Config)
-> CargoResult<BuildReport<'t, 'cfg>> {
let cache = self.serialized;
let report = apply_build_plan(self.plan, &dest, options)?;
let report = apply_build_plan(self.plan, dest, options)?;
// and finally save cache of just successfully applied plan:
// only if there is no errors
if !report.has_errors() {
Expand Down Expand Up @@ -240,8 +240,7 @@ impl<'t, 'cfg> CachedPlan<'t, 'cfg> {
use playdate::assets::plan::*;
use playdate::assets::resolver::*;

let title =
|&(ref left, ref right): &(Expr, Expr)| format!("rule: {} = {}", left.original(), right.original());
let title = |(left, right): &(Expr, Expr)| format!("rule: {} = {}", left.original(), right.original());
let row_columns = |target: &Path, source: &Path| {
(format!(" {}", target.as_relative_to(&root).display()),
source.as_relative_to(&root).display().to_string())
Expand Down
Loading

0 comments on commit 5f03a23

Please sign in to comment.