Skip to content

Strict ID types#487

Merged
tsmbland merged 17 commits intomainfrom
id_types
Apr 16, 2025
Merged

Strict ID types#487
tsmbland merged 17 commits intomainfrom
id_types

Conversation

@tsmbland
Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland commented Apr 15, 2025

Description

Have fun

Fixes #144

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

Further checks

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

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 15, 2025

Codecov Report

Attention: Patch coverage is 96.69967% with 10 lines in your changes missing coverage. Please review.

Project coverage is 95.12%. Comparing base (0cdcf2e) to head (842e79d).
Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
src/id.rs 84.61% 3 Missing and 1 partial ⚠️
src/input.rs 90.00% 1 Missing ⚠️
src/input/agent/search_space.rs 85.71% 1 Missing ⚠️
src/input/commodity.rs 66.66% 0 Missing and 1 partial ⚠️
src/input/process.rs 95.00% 0 Missing and 1 partial ⚠️
src/input/process/flow.rs 92.85% 0 Missing and 1 partial ⚠️
src/input/process/parameter.rs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
- Coverage   95.13%   95.12%   -0.01%     
==========================================
  Files          36       36              
  Lines        4930     5002      +72     
  Branches     4930     5002      +72     
==========================================
+ Hits         4690     4758      +68     
- Misses        120      123       +3     
- Partials      120      121       +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.

@tsmbland tsmbland marked this pull request as ready for review April 15, 2025 22:37
@tsmbland tsmbland requested a review from alexdewar April 15, 2025 22:37
Copy link
Copy Markdown
Member

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

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

Good work! This will definitely make life easier in the long run.

I've requested changes because I think you should qualify types fully inside the macro definition (std::fmt::Display etc.) so you don't need to import them everywhere you're using that. Besides that, I've just added some other small comments.

I'm in two minds about whether or not it's a good idea to have ID types implement Deserialize, because I can imagine down the line someone wanting to add a new CSV file for something agent-related (or whatever) and sticking an AgentID field in a struct directly rather than getting it from the set of agent IDs, which would kind of circumvent the validation. We'll be converting from *Raw structs anyway so we can construct ID types manually at that point if needed. I suppose by a similar logic, maybe we don't want to implement From<&str> etc. as that's another potential footgun (we could enable it for testing only). I'm probably overthinking it now... Anyway these are always things we can change later if we feel like it.

Comment thread src/id.rs Outdated
Comment thread src/id.rs Outdated
Comment on lines +119 to +126
fn get_id(&self, id: &str) -> Result<ID> {
let found = self
.get(id)
.with_context(|| format!("Unknown ID {id} found"))?;
Ok(Rc::clone(id))
Ok(found.clone())
}

fn check_id(&self, id: &ID) -> Result<ID> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering about the naming, because check_id also returns an ID. How about renaming check_id to get_id and get_id to get_id_by_str or something?

On a separate note, I'm in two minds about whether or not it was a good idea to have get_id() clone the IDs when returning them. Callers can always do that separately if they want. But I guess both ways are basically fine and changing it will mean more churn (though it would make it consistent with your new implementation for HasID).

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.

I've renamed the functions as suggested. As for cloning the IDs, you're probably right but I can't face changing this right now 😂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fair enough! Maybe open an issue for it and we can do it some way down the road if we can be bothered

Comment thread src/id.rs
@tsmbland tsmbland requested a review from alexdewar April 16, 2025 10:48
Copy link
Copy Markdown
Member

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

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

Looks good! Let's merge. Onwards to MUSE's glorious typesafe future 🚀

Comment thread src/id.rs
impl<T> IDLike for T where
T: Eq + std::hash::Hash + std::borrow::Borrow<str> + Clone + std::fmt::Display
{
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not a fan of what the formatter's done here. Yuck

@tsmbland tsmbland merged commit 944f6ed into main Apr 16, 2025
7 checks passed
@tsmbland tsmbland deleted the id_types branch April 16, 2025 11:11
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.

Use dedicated types to refer to different sorts of ID

2 participants