(GH-538) Define TypeVersion as newtype
#1350
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
This change follows the Rust "parse, don't validate pattern" by defining a
TypeVersionenum with theSemanticandStringvariants. If the version can be parsed as a semantic version, the type creates it as an instance ofTypeVersion::Semanticand otherwise creates it asTypeVersion::String.The
TypeVersionenum implements several conversion and comparison traits to make using the newtype more ergonomic. It also defines helper methodsis_semver()andmatches_semver_req()for easier usage.When comparing an instance of
TypeVersion:TypeVersion::Stringinstances toTypeVersion::Semanticand tosemver::Version.Fooandfooare equal butFoois greater thanBar.TypeVersionto string slices,Stringinstances, andsemver::Versioninstances in addition to other instances ofTypeVersion.==,>, and<operators for easier reading.The newtype overhauls the JSON Schema for versions to help users get better validation and IntelliSense when authoring manifests.
Finally, this change adds comprehensive integration tests for the newtype and its implementations as well as documentation for the type and its public methods.
PR Context
Prior to this change, the version fields for DSC used an arbitrary
Stringfor both resources and extensions. The generated schema for those types then reports that any string is valid and DSC must check every time it needs to process the version for whether the version is semantic or an arbitrary string.