API reference
moonsver
Every public value, type and method of moonbitstack/moonsver. Import it as @moonsver. Signatures are the package interface, in declaration order.
Functions 1
Values that are not tied to a type.
fnsatisfies#
pub fn satisfies(String, String, syntax? : Syntax) -> Bool raise Refused
Convenience wrapper: parse two strings and check whether the version satisfies.
Both strings are read with the same grammar, the version through Version::parse.
Errors 1
One error type, raised by every parser.
suberrorRefused#
Why a version or a range was refused, and where.
at is the zero-based character index of the first offending character, or the input's length when it ended early; the message says what was expected.
pub(all) suberror Refused {- Empty(at~ : Int32, String)Empty input where the grammar wants a version or a range.
- Unexpected(at~ : Int32, String)A character that does not belong where it stands.
- Truncated(at~ : Int32, String)The input ended partway through.
- Number(at~ : Int32, String)A numeric component that is not a number.
- LeadingZero(at~ : Int32, String)A numeric component, or a numeric prerelease identifier, with a leading zero (SemVer 2.0.0 §2, §9).
- EmptyIdentifier(at~ : Int32, String)An empty identifier, as in
1.0.0-or1.0.0-a..b. - Identifier(at~ : Int32, String)An identifier with characters §9 and §10 do not allow.
- Unsupported(at~ : Int32, String)A spelling the chosen
Syntaxdoes not accept. - Range(at~ : Int32, String)A range that does not hang together, such as a concrete part after a wildcard.
Implements Show.
Types 11
The values the package works with.
structComparator#
One comparator: an operator and a full version.
Build metadata is dropped from comparators (>=1.2.3+build is >=1.2.3), since it takes no part in precedence.
pub(all) struct Comparator {- op : Op
- version : Version
Implements Eq, Show.
structComparatorSet#
A conjunction (AND) of comparators. An empty list means "any version".
pub(all) struct ComparatorSet {- comparators : Array[Comparator]
Implements Eq, Show.
enumDefaultOp#
What a bare version means when no operator is given.
pub(all) enum DefaultOp {- ExactExact:
1.2.3means=1.2.3, which is how npm and Ruby read it. - CaretCaret:
1.2.3means^1.2.3, Cargo's default. - TildeTilde:
1.2.3means~1.2.3.
Implements Eq.
enumIdentifier#
A version identifier.
SemVer 2.0.0 allows two kinds of identifier: all digits, or a string drawn from [0-9A-Za-z-]. Numeric ones are held as arbitrary-precision integers, so a value such as 18446744073709551616 does not overflow.
pub(all) enum Identifier {- Num(@bigint.BigInt)A numeric identifier, normalised to carry no leading zeroes.
- Alpha(String)An alphanumeric identifier, kept verbatim.
Implements Compare, Eq, Hash, Show.
pub fn Identifier::alpha(String) -> Self
pub fn Identifier::as_bigint(Self) -> @bigint.BigInt?
The integer value, or None for an alphanumeric identifier.
pub fn Identifier::as_int(Self) -> Int32?
The value as an Int, or None if it is alphanumeric or out of range.
pub fn Identifier::compare(Self, Self) -> Int32
A total order that sort uses: precedence first, then build metadata.
pub fn Identifier::is_numeric(Self) -> Bool
pub fn Identifier::numeric(Int32) -> Self
enumOp#
A comparison operator.
pub(all) enum Op {- EqEquals
= - NeNot equal
!=, which only grammars such asSyntax::ruby()accept - GtGreater than
> - GeGreater than or equal
>= - LtLess than
< - LeLess than or equal
<=
Implements Eq, Show.
enumPrereleasePolicy#
How prereleases are matched against a range.
The spec only orders prereleases against each other; it says nothing about how a range should treat them, so every ecosystem settled on its own convention.
pub(all) enum PrereleasePolicy {- SameTupleThe npm and Cargo convention: a prerelease can only satisfy a set when some
comparator carries a prerelease with the same
major.minor.patch. So>=1.2.3-alphatakes1.2.3-betabut not1.2.4-beta, and*takes no prerelease at all. - AlwaysTreat prereleases as ordinary versions (npm's
includePrerelease). - NeverPrereleases never match anything, for when only stable releases will do.
Implements Eq.
structRange#
A version range: a disjunction (OR) of comparator sets.
A Range is the parsed, normalised structure rather than the original string. ^1.2.3, for instance, becomes the set >=1.2.3, <2.0.0.
pub struct Range {Implements Eq, Show, ToJson.
pub fn Range::parse(String, syntax? : Syntax) -> Self raise Refused
Parse a version range.
npm's grammar is the default; pass syntax= for Cargo, Ruby or strict.
pub fn Range::from_json(Json, @json.JsonPath) -> Self raise @json.JsonDecodeError
Read back from a JSON string, through the same parser.
pub fn Range::max_satisfying(Self, Array[Version]) -> Version?
The highest satisfying version, by precedence, with build metadata breaking ties.
pub fn Range::min_satisfying(Self, Array[Version]) -> Version?
The lowest satisfying version.
pub fn Range::policy(Self) -> PrereleasePolicy
The prerelease policy this range was parsed with.
pub fn Range::satisfies(Self, Version) -> Bool
Does the version fall inside the range?
The prerelease policy comes from the Syntax used to parse the range.
pub fn Range::satisfies_with(Self, Version, PrereleasePolicy) -> Bool
Does the version fall inside the range, with the policy overridden for this call?
pub fn Range::satisfying(Self, Array[Version]) -> Array[Version]
Every satisfying version, sorted ascending by precedence.
enumRelease#
The kind of bump to apply.
Several corners are easy to miss: the major bump of 1.0.0-pre is 1.0.0.
pub(all) enum Release {- Major
1.2.3→2.0.0;1.0.0-pre→1.0.0 - Minor
1.2.3→1.3.0;1.2.0-pre→1.2.0 - Patch
1.2.3→1.2.4;1.2.3-pre→1.2.3 - PreMajor
1.2.3→2.0.0-0 - PreMinor
1.2.3→1.3.0-0 - PrePatch
1.2.3→1.2.4-0 - PrereleaseBump the last numeric identifier, or raise the patch and append
-0if there is none - StableDrop the prerelease, leaving the numbers alone
Implements Show.
structSyntax#
Range grammar settings.
A version has one spelling in SemVer 2.0.0, but ranges are not in the spec, and different ecosystems settled on different rules. Their differences are settings here, so one parser can read several grammars.
Switches that govern version literals:
allow_v_prefix: acceptv1.2.3allow_equals_prefix: accept=1.2.3as a version literalallow_build_metadata: accept a+buildsegmenttrim: allow whitespace around a version literal
Switches that govern range syntax:
default_op: what a bare version means (npmExact, CargoCaret)tilde_bump: how~finds its upper bound (npm/CargoPatch, RubyLastSpecified)allow_caret/allow_tilde: accept^/~(~>only underLastSpecified)allow_ne: accept!=allow_or: accept||allow_hyphen_range: accept1.2.3 - 2.0.0allow_xrange: accept the shorthand forms1.x,1.2.*and bare1.2allow_empty: whether an empty string means "any version"prerelease_policy: how prereleases are matched
pub(all) struct Syntax {- allow_v_prefix : Bool
- allow_equals_prefix : Bool
- allow_build_metadata : Bool
- trim : Bool
- default_op : DefaultOp
- tilde_bump : TildeBump
- allow_caret : Bool
- allow_tilde : Bool
- allow_ne : Bool
- allow_or : Bool
- allow_hyphen_range : Bool
- allow_xrange : Bool
- allow_empty : Bool
- prerelease_policy : PrereleasePolicy
Implements Default, Eq, Show.
pub fn Syntax::cargo() -> Self
Cargo grammar: a bare version carries a caret (1.2.3 is ^1.2.3), with , or space for conjunctions and * / 1.* wildcards; no ||, no hyphen ranges, no v.
pub fn Syntax::default() -> Self
pub fn Syntax::from_name(String) -> Self?
Look up a preset by name, for the CLI and config files.
pub fn Syntax::npm() -> Self
npm grammar: a bare version is exact, with ^, ~, ||, hyphen ranges, 1.x shorthand, a v prefix, and the empty string for "any".
pub fn Syntax::ruby() -> Self
Ruby grammar: ~> is the pessimistic operator (drop the last segment, then bump), and != is allowed; no ^, ||, hyphen ranges or shorthand.
pub fn Syntax::strict() -> Self
Strict SemVer 2.0.0 spelling: only = > >= < <=, joined by commas or spaces, with no ^, ~, ||, hyphen ranges, 1.x shorthand or v prefix.
The one exception is *, which always means "any version": it is what Range::to_string prints for an empty range, so refusing it would break the round trip.
pub fn Syntax::name(Self) -> String?
The preset name for this syntax, or None for a custom mix.
enumTildeBump#
How ~ and ~> work out their upper bound.
pub(all) enum TildeBump {- Patchnpm and Cargo: bump the minor if one was written, otherwise bump the major.
-
~1.2.3→>=1.2.3 <1.3.0-~1.2→>=1.2.0 <1.3.0-~1→>=1.0.0 <2.0.0 - LastSpecifiedRuby's
~>: drop the last segment, then bump whatever is left. -~> 1.2.3→>=1.2.3 <1.3.0-~> 1.2→>=1.2.0 <2.0.0-~> 1→>=1.0.0 <2.0.0
Implements Eq.
structVersion#
A SemVer 2.0.0 version.
The three numbers are arbitrary-precision integers, so 18446744073709551616.0.0 parses, prints and compares exactly, well past 64 bits.
The fields are private and only Version::new / Version::parse can build one, so a version never carries a leading zero or a malformed identifier list.
pub struct Version {Implements Compare, Eq, Hash, Show, ToJson.
pub fn Version::from_bigint(@bigint.BigInt, @bigint.BigInt, @bigint.BigInt) -> Self
Build a version from three arbitrary-precision integers.
pub fn Version::new(Int32, Int32, Int32) -> Self
Build a version from three Ints, with no prerelease and no build metadata.
pub fn Version::parse(String, syntax? : Syntax) -> Self raise Refused
Parse a version.
The npm grammar is the default, so v1.2.3, =1.2.3 and surrounding whitespace are all fine. Pass syntax=Syntax::strict() to hold to the spec exactly.
pub fn Version::build(Self) -> ArrayView[String]
The build metadata identifiers: ["build", "5"] for 1.2.3+build.5.
pub fn Version::compare(Self, Self) -> Int32
A total order that sort uses: precedence first, then build metadata.
pub fn Version::core(Self) -> Self
Keep the three numbers only, dropping prerelease and build metadata.
pub fn Version::core_string(Self) -> String
The major.minor.patch form, without prerelease or build metadata.
pub fn Version::from_json(Json, @json.JsonPath) -> Self raise @json.JsonDecodeError
Read back from a JSON string, through the same parser.
pub fn Version::is_prerelease(Self) -> Bool
Whether the version carries a prerelease.
pub fn Version::is_stable(Self) -> Bool
Whether this is a stable release. Build metadata does not make it unstable.
pub fn Version::precedence_cmp(Self, Self) -> Int32
Compare precedence as laid out in SemVer 2.0.0 §11.
Returns -1, 0 or 1. **Build metadata takes no part**: 1.0.0+a and 1.0.0+b share the same precedence.
pub fn Version::prerelease(Self) -> ArrayView[Identifier]
The prerelease identifiers: ["alpha", 1] for 1.2.3-alpha.1.
pub fn Version::same_core(Self, Self) -> Bool
Whether the three numbers match, ignoring prerelease and build metadata.