Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FromStr for Val #16926

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

VictorKoenders
Copy link

@VictorKoenders VictorKoenders commented Dec 22, 2024

Objective

This PR implements FromStr for Val, so developers can parse values like 10px and 50%

Testing

Added tests for this. I think they cover pretty much everything, and it's a fairly simple unit test.

Limitations

Currently the following float values are not parsed:

  • inf, -inf, +infinity, NaN
  • 2.5E10, 2.5e10, 2.5E-10

For my use case this is perfectly fine but other developers might want to support these values

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

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

A sensible addition IMO. I would expect many bsn-like alternatives would have a similar implementation anyway, so it might as well be first-party. I would like this to be const though, just for that extra utility.

impl core::str::FromStr for Val {
type Err = ValParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would love to see this pulled out as a const function if it's compatible. Not a blocker if it's not a simple change though.

Copy link
Author

Choose a reason for hiding this comment

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

I checked and every single function that's being called is non-const 😆

Copy link
Author

@VictorKoenders VictorKoenders Dec 22, 2024

Choose a reason for hiding this comment

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

Specifically: str::trim, str::bytes(), iterator::position, str::split_at, ?, f32::parse are not const

Copy link
Contributor

Choose a reason for hiding this comment

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

Damn, thanks for clarifying!

@bushrat011899 bushrat011899 added C-Feature A new feature, making something new possible A-UI Graphical user interfaces, styles, layouts, and widgets X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 22, 2024
return Ok(Val::Auto);
}

let Some(end_of_number) = s
Copy link
Contributor

Choose a reason for hiding this comment

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

Only small nit with this PR is that this doesn't accommodate all of rust's valid f32 parsing inputs; I don't think people will often want to make values of 1e5 px or whatever, but it's possible that values like that may show up in automatically-formatted float output, for example.

Copy link
Author

Choose a reason for hiding this comment

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

This doesn't support: NaN, inf, infinity, +inf, 1E4 and 1e5. I think most developers won't need this, and it can be added later

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed. That's why I approved it :)

@mweatherley mweatherley added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants