Skip to content

Add generic helpers for parsing range strings#1297

Open
alexdewar wants to merge 6 commits into
mainfrom
refactor-range-code
Open

Add generic helpers for parsing range strings#1297
alexdewar wants to merge 6 commits into
mainfrom
refactor-range-code

Conversation

@alexdewar
Copy link
Copy Markdown
Member

Description

This PR is a reworking of #1287, with the change that allows #1183 removed. Other than that, things are mostly the same, except I slightly changed the functions to account for the fact that now not all range-strings allow a single value.

For more information, see PR description for #1287.

Hopefully this refactoring is worth it... There are some other places where I think the range syntax will crop up, so it seems like it makes sense to have generic helpers.

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 May 19, 2026 15:59
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

This PR refactors “range string” parsing by introducing generic range helpers in src/input/ and migrating year parsing to use them, while also updating availability parsing to use the shared helper and adjusting imports accordingly.

Changes:

  • Added src/input/range.rs generic helpers (partition, parse_range, parse_range_parts) for inclusive start..end style parsing.
  • Moved/rewrote year string parsing into src/input/year.rs using the new range helpers and supporting ;-separated ranges.
  • Updated availability parsing to use parse_range, and adjusted unit types to be parseable from strings.

Reviewed changes

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

Show a summary per file
File Description
src/year.rs Removed old top-level year parsing implementation (module deleted).
src/lib.rs Stops exporting the year module from the public crate API.
src/input.rs Wires in new range and year submodules and adds a generalized is_sorted_and_unique_with.
src/input/range.rs New generic parsing helpers for ..-delimited inclusive ranges.
src/input/year.rs New year parsing implementation built on the generic range helpers.
src/input/process/availability.rs Replaces bespoke availability parsing with parse_range + context.
src/input/** (agent/process/commodity files) Import rewrites to use crate::input::parse_year_str.
src/units.rs Adds FromStr support to unit newtypes (and to UnitType).
Cargo.toml Enables derive_more’s from_str feature.

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

Comment thread src/input/year.rs Outdated
Comment thread src/input/year.rs Outdated
Comment on lines +40 to +44
// We depend on this to do a binary search below
assert!(
is_sorted_and_unique(valid_years),
"`valid_years` must be sorted and unique"
);
Comment thread src/lib.rs
Comment on lines 26 to 29
pub mod simulation;
pub mod time_slice;
pub mod units;
pub mod year;

Comment thread src/units.rs
pub trait UnitType:
fmt::Debug
+ Copy
+ FromStr
Comment thread src/units.rs
Comment on lines 48 to 59
#[derive(
Debug,
Clone,
Copy,
PartialEq,
PartialOrd,
Serialize,
derive_more::Add,
derive_more::Sub,
derive_more::FromStr,
)]
pub struct $name(pub f64);
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

❌ Patch coverage is 98.13084% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.49%. Comparing base (0af4399) to head (64f8f11).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/input/process/availability.rs 50.00% 0 Missing and 1 partial ⚠️
src/input/range.rs 97.82% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1297      +/-   ##
==========================================
+ Coverage   89.46%   89.49%   +0.03%     
==========================================
  Files          57       58       +1     
  Lines        8361     8390      +29     
  Branches     8361     8390      +29     
==========================================
+ Hits         7480     7509      +29     
- Misses        581      582       +1     
+ Partials      300      299       -1     

☔ 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 force-pushed the refactor-range-code branch from 556bd89 to 64f8f11 Compare May 19, 2026 20:46
@alexdewar alexdewar added this to MUSE May 19, 2026
@alexdewar alexdewar moved this to 👀 In review in MUSE May 19, 2026
Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland left a comment

Choose a reason for hiding this comment

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

Happy with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

3 participants