CycloneDX / CycloneDX/cyclonedx-rust-cargo

Refactor `.new_unchecked()` on enums into `impl From<&str>`

Open
#684 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
178
Forks
66
PR merge metrics
No merged PRs in 30d

Description

We have functions with signature `pub(crate) fn new_unchecked>(value: A) -> Self` on a number of enums, where the function converts any string to an enum variant and unrecognized strings go to the `Unknown(String)` variant.

It seems it would be more convenient and idiomatic to just `impl From<&str>` or `impl FromStr` instead of using a custom function. The generic `A: AsRef` will be separately monomorphised for every type it is used with, increasing compilation time and binary size.

A potentially significant change is that trait implementations are automatically public, so there is no way to make them `pub(crate)`. But exposing it publicly actually sounds like a good idea to me.

I originally brought this up in the review of #681

Contributor guide

Open the contributing guide

Research direction

Search the repository for the pub(crate) new_unchecked entry points on enums and review the discussion in issue #681. Identify every affected enum and how its string conversion is used before choosing the trait approach. Done means the affected conversions use the requested public trait implementation, the old custom functions are no longer needed, and the project still builds and passes its existing checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.