Skip to content

Split up asset.rs#1158

Merged
alexdewar merged 5 commits intomainfrom
split-up-asset.rs
Mar 3, 2026
Merged

Split up asset.rs#1158
alexdewar merged 5 commits intomainfrom
split-up-asset.rs

Conversation

@alexdewar
Copy link
Copy Markdown
Collaborator

Description

The file is getting rather long, so it makes sense to break it up. I've put everything related to AssetPool and AssetCapacity into separate submodules. No functional change intended.

Unfortunately, even after this refactoring, asset.rs is still the longest file in the repo (>1600 lines). I think that probably the cleanest way to break it up further would be to turn some of the methods into standalone functions which take an Asset as an argument and put these into submodules. For example, anything related to costs could be in its own module. This would be a more invasive set of changes though, so I think we should leave it for later (or we can delegate to an LLM 😄).

Closes #1117.

Type of change

  • Bug fix (non-breaking change to fix an issue)
  • New feature (non-breaking change to add functionality)
  • Refactoring (non-breaking, non-functional change to improve maintainability)
  • Optimization (non-breaking change to speed up the code)
  • Breaking change (whatever its nature)
  • Documentation (improve or add documentation)

Key checklist

  • All tests pass: $ cargo test
  • The documentation builds and looks OK: $ cargo doc
  • Update release notes for the latest release if this PR adds a new feature or fixes a bug
    present in the previous release

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copilot AI review requested due to automatic review settings March 2, 2026 15:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the asset module by extracting the AssetPool and AssetCapacity implementations (and their tests) out of the monolithic src/asset.rs into dedicated submodules, while keeping the public API via re-exports.

Changes:

  • Moved AssetPool into src/asset/pool.rs and re-exported it from src/asset.rs.
  • Moved AssetCapacity into src/asset/capacity.rs and re-exported it from src/asset.rs.
  • Added a reusable asset_divisible test fixture in src/fixture.rs to support the extracted tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/asset.rs Declares capacity/pool submodules and re-exports AssetCapacity/AssetPool; removes inlined implementations/tests.
src/asset/pool.rs New module containing AssetPool implementation and its tests (moved from asset.rs).
src/asset/capacity.rs New module containing AssetCapacity implementation and its tests (moved from asset.rs).
src/fixture.rs Adds asset_divisible fixture used by tests that moved into asset/pool.rs (and still used in asset.rs tests).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +610 to +615
// Decomission unused assets
let mut decommissioned = Vec::new();
asset_pool.decommission_mothballed(2025, mothball_years, &mut decommissioned);

// Only the removed asset should be decommissioned (since it's not in active pool)
assert_eq!(asset_pool.assets.len(), 1); // Active pool unchanged
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments don’t match the test setup: the asset is still in the active pool (it was mothballed, not removed), and decommission_mothballed removes it from asset_pool.assets. Please update the commentary to reflect what is actually being asserted.

Suggested change
// Decomission unused assets
let mut decommissioned = Vec::new();
asset_pool.decommission_mothballed(2025, mothball_years, &mut decommissioned);
// Only the removed asset should be decommissioned (since it's not in active pool)
assert_eq!(asset_pool.assets.len(), 1); // Active pool unchanged
// Decommission assets that have been mothballed for at least `mothball_years`
let mut decommissioned = Vec::new();
asset_pool.decommission_mothballed(2025, mothball_years, &mut decommissioned);
// The mothballed asset should be removed from the active pool and added to `decommissioned`
assert_eq!(asset_pool.assets.len(), 1); // One active asset remains

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 2, 2026 15:14
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@alexdewar alexdewar requested review from Aurashk and tsmbland March 2, 2026 15:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +117 to +118
if match asset.state {
AssetState::Commissioned { .. } => !self.assets.contains(&asset),
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match asset.state attempts to move AssetState out of an Asset behind an Rc (via AssetRef's Deref), which won’t compile for non-Copy AssetState. Match on a reference instead (e.g. match &asset.state / matches!(&asset.state, ...)) to avoid moving out of the shared asset.

Suggested change
if match asset.state {
AssetState::Commissioned { .. } => !self.assets.contains(&asset),
if match &asset.state {
&AssetState::Commissioned { .. } => !self.assets.contains(&asset),

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it is already accessing state by reference (AssetRef implements Deref for Asset).

That said, I don't think the if match is v readable, so I'll tweak it.

Comment on lines +52 to +69
impl Eq for AssetCapacity {}

impl PartialOrd for AssetCapacity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for AssetCapacity {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(AssetCapacity::Continuous(a), AssetCapacity::Continuous(b)) => a.total_cmp(b),
(AssetCapacity::Discrete(units1, size1), AssetCapacity::Discrete(units2, size2)) => {
Self::check_same_unit_size(*size1, *size2);
units1.cmp(units2)
}
_ => panic!("Cannot compare different types of AssetCapacity ({self:?} and {other:?})"),
}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssetCapacity derives PartialEq (float == semantics) but implements Ord using total_cmp, and also declares Eq. This violates the required consistency between Eq/Ord and equality (e.g., 0.0 == -0.0 but total_cmp orders them differently; NaN also breaks reflexivity). Consider implementing PartialEq/Eq manually to align with total_cmp (or drop Ord/Eq if total ordering isn’t required).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot is on the money here... this could bite us later. I've opened an issue for it: #1162

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 2, 2026

Codecov Report

❌ Patch coverage is 94.44444% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.48%. Comparing base (9dd37de) to head (2f5fbde).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/asset/pool.rs 93.84% 6 Missing and 2 partials ⚠️
src/asset/capacity.rs 95.58% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1158   +/-   ##
=======================================
  Coverage   87.48%   87.48%           
=======================================
  Files          55       57    +2     
  Lines        7660     7661    +1     
  Branches     7660     7661    +1     
=======================================
+ Hits         6701     6702    +1     
  Misses        657      657           
  Partials      302      302           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alexdewar alexdewar enabled auto-merge March 3, 2026 13:34
@alexdewar alexdewar merged commit 2a93c16 into main Mar 3, 2026
8 of 9 checks passed
@alexdewar alexdewar deleted the split-up-asset.rs branch March 3, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split up assets.rs

3 participants