Provide a way to render lax-parsed SPDX expressions as valid SPDX
amy-keibler opened this issue · comments
Is your feature request related to a problem? Please describe.
Thank you for creating such a useful library. We're using it in the CycloneDX SBOM Rust implementation. One rough edge we've come across is that we want to be able to accept mostly-correct SPDX identifiers (e.g. the "MIT/Apache-2.0"
convention that's present in several crates), but we want to ensure that we're only producing valid SPDX expressions in our output, to maximize ecosystem tooling compatibility.
Describe the solution you'd like
It would be useful for the Expression
type that is generated via parse_mode
with ParseMode::LAX
to be able to output as a valid SPDX identifier. Currently, it stores the original string and just outputs that when to_string
is called. Changing the output to_string
would be a breaking change, so maybe there could be an additional function named render
or something similar?
Describe alternatives you've considered
Currently, we have a temporary solution that replaces /
with OR
, but a proper solution would be more robust.
Additional context
If this change is welcome, I might have time to implement this myself and submit a PR over the next few weeks.
I think a less invasive change would probably be to instead have a transform function exposed that can take a string and ParseMode
, and just fix the string based on the settings in the ParseMode
, eg. in your case ParseMode { allow_slash_as_or_operator: false, ..Default::default()
would mean an all /
would be replaced with OR
etc, and could either be executed at all times and the resulting string handed to Expression::parse
, or else attempted as a fallback if parsing fails. Right now all of the parsing errors are predicated on the original input string, it feels like adding an additional way to output the expression would make the API more cumbersome.