Add generic helpers for parsing range strings#1297
Merged
Conversation
It's not used outside of `input`, so let's keep it in here along with all the other input-related code.
Contributor
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.
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 on lines
26
to
29
| pub mod simulation; | ||
| pub mod time_slice; | ||
| pub mod units; | ||
| pub mod year; | ||
|
|
| pub trait UnitType: | ||
| fmt::Debug | ||
| + Copy | ||
| + FromStr |
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 Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1297 +/- ##
==========================================
+ Coverage 89.65% 89.69% +0.03%
==========================================
Files 57 58 +1
Lines 8441 8470 +29
Branches 8441 8470 +29
==========================================
+ Hits 7568 7597 +29
- Misses 565 566 +1
+ Partials 308 307 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
556bd89 to
64f8f11
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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