Add generic helpers for parsing range strings#1297
Conversation
It's not used outside of `input`, so let's keep it in here along with all the other input-related code.
There was a problem hiding this comment.
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.rsgeneric helpers (partition,parse_range,parse_range_parts) for inclusivestart..endstyle parsing. - Moved/rewrote year string parsing into
src/input/year.rsusing 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.
| // We depend on this to do a binary search below | ||
| assert!( | ||
| is_sorted_and_unique(valid_years), | ||
| "`valid_years` must be sorted and unique" | ||
| ); |
| pub mod simulation; | ||
| pub mod time_slice; | ||
| pub mod units; | ||
| pub mod year; | ||
|
|
| pub trait UnitType: | ||
| fmt::Debug | ||
| + Copy | ||
| + FromStr |
| #[derive( | ||
| Debug, | ||
| Clone, | ||
| Copy, | ||
| PartialEq, | ||
| PartialOrd, | ||
| Serialize, | ||
| derive_more::Add, | ||
| derive_more::Sub, | ||
| derive_more::FromStr, | ||
| )] | ||
| pub struct $name(pub f64); |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
556bd89 to
64f8f11
Compare
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
Key checklist
$ cargo test$ cargo docpresent in the previous release
Further checks