bottlerocket icon indicating copy to clipboard operation
bottlerocket copied to clipboard

modeled_types: Use FromStr instead of TryFrom

Open mchaker opened this issue 3 years ago • 7 comments

Issue number:

Closes #1061

Description of changes:

In order to clean up the code, this change replaces TryFrom<&str> with FromStr. This change has been made for all modeled_types.

Testing done:

  • [x] performed a complete build of aws-dev with no errors
  • [x] performed a complete build of metal-dev with no errors
  • [x] performed a complete build of vmware-dev with no errors

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

mchaker avatar Jul 25 '22 22:07 mchaker

Force push: fix try_into calls that escaped local testing

mchaker avatar Jul 25 '22 23:07 mchaker

Force push: fix more try_into calls that escaped local testing

mchaker avatar Jul 25 '22 23:07 mchaker

Force push: clear up rustfmt errors

mchaker avatar Jul 26 '22 00:07 mchaker

I'm a little curious about this motivation behind this change!

Also, assuming this all came up via compiler errors, but one nice thing about implementing TryFrom means you can use the reciprocal TryInto. Are you sure we're not calling try_into() in places that would require the TryFrom implementation?

zmrow avatar Jul 26 '22 19:07 zmrow

I'm a little curious about this motivation behind this change!

Thanks for your interest in this change! The two people who initially recommended this change are no longer on the team, so I will provide what I believe is the reason for this change.

I believe the reasons for this change are discussed in the following posts:

Essentially, it is more idiomatic to convert from strings using FromStr because doing so allows you to use the default string parsing syntax (string_variable.parse()). In fact, I'm in the middle of changing all of the from_str() calls in this PR to string_variable.parse() calls. :)

Also, assuming this all came up via compiler errors, but one nice thing about implementing TryFrom means you can use the reciprocal TryInto. Are you sure we're not calling try_into() in places that would require the TryFrom implementation?

I only replaced TryFrom<&str> calls that I was sure were conversions from strings, and left everything else untouched (e.g. conversions from custom types such as FriendlyVersion)

The TryFrom implementation that the macro creates for custom types in modeled_types is for the String type (permalink). That TryFrom<String> implementation actually just used to call TryFrom<&str> under the hood, and each modeled_types custom type implemented the TryFrom<&str> trait.

Now, each modeled_types custom type implements FromStr, which allows us to call .parse() from within the macro instead of TryFrom<&str>'s try_from().

mchaker avatar Jul 26 '22 23:07 mchaker

Force push: changed all ::from_str() calls to .parse::<Type>() calls.

mchaker avatar Jul 27 '22 21:07 mchaker

Force push: typo

mchaker avatar Jul 27 '22 21:07 mchaker