[−][src]Trait lenient_semver_parser::VersionBuilder
Trait to abstract over version building.
The methods to implement in this trait represent the components of semver::Version
,
but allow for parsing into a custom type.
The trait is generic over the lifetime of the input string, so that one could parse into a version without having to allocate.
Most methods have a default implementation that does nothing and ignores the input. This can be used to implement some form of validation without needing to keep the result.
Example
struct IsPreRelease(bool); impl<'input> VersionBuilder<'input> for IsPreRelease { type Out = bool; fn new() -> Self { IsPreRelease(false) } fn add_pre_release(&mut self, _input: &'input str) { self.0 = true; } fn build(self) -> Self::Out { self.0 } } fn is_pre_release(v: &str) -> bool { lenient_semver_parser::parse::<IsPreRelease>(v).unwrap_or_default() } assert!(is_pre_release("1.2.3-pre")); assert!(!is_pre_release("1.2.3")); assert!(!is_pre_release("1.2.3+build"));
Associated Types
type Out
The return type of the final version.
Required methods
fn new() -> Self
Construct a new version builder.
The function must not fail and the version (if returned from VersionBuilder::build
at this point)
should represent something akin to "0.0.0"
fn build(self) -> Self::Out
Construct the final version.
Provided methods
fn set_major(&mut self, major: u64)
Set the major version component.
This method is the only required component and will be called
before VersionBuilder::build
.
fn set_minor(&mut self, minor: u64)
Set the minor version component.
This component is optional and might not be called
before VersionBuilder::build
.
fn set_patch(&mut self, patch: u64)
Set the patch version component.
This component is optional and might not be called
before VersionBuilder::build
.
fn add_additional(&mut self, num: u64)
Add additional numeric components following patch and preceding pre-release.
For a version like 1.2.3.4.5
, this would call add_additional with 4
and 5
.
For strict semver versions, those values are invalid. For lenient semver, those values are better represented as build than pre-release, although they might be "in the same block" as pre-release. In terms of comparing versions, the values added here should still have an impact.
This component is optional and might not be called
before VersionBuilder::build
.
fn add_pre_release(&mut self, pre_release: &'input str)
Add a pre-release identifier.
The string might represent any alpha-numeric identifier, including numbers with or without leading zeroes. It is up to the implementor to parse those into more specific identifiers, if required.
This component is optional and might not be called
before VersionBuilder::build
.
This method might be called multiple times.
fn add_build(&mut self, build: &'input str)
Add a build identifier.
The string might represent any alpha-numeric identifier, including numbers with or without leading zeroes. It is up to the implementor to parse those into more specific identifiers, if required.
This component is optional and might not be called
before VersionBuilder::build
.
This method might be called multiple times.