VersionReq

star : VersionReq

The "*" version requirement, a.k.a. an empty list of comparators.

This will match any semver.

toStr : VersionReq -> Str

Render a version requirement to a string.

This will not always look like the source text a version requirement was parsed from, as we don't store ignored text, like whitespace.

versionReq = [
    Relation { operator: Compatible, version: MajorMinor 2 5 },
    Relation { operator: LessThan, version: Major 3 },
]

expect toStr versionReq == "^2.5, <3"

expect toStr [] == "*"

parse : Str -> Result VersionReq InvalidVersionReqError

Parse a version requirement from a string.

This fails if there is any leftover text after the string. Use the parseLazy twin function if you want to get the leftover text after parsing.

versionReq = parse "1.2.*, <1.2.5"

expect versionReq == Ok [
    Wildcard (MajorMinor 1 2),
    Relation { operator: LessThan, version: Full { major: 1, minor: 2, patch: 5, preRelease: [] } },
]

parseLazy : Str -> Result ( VersionReq, Str ) InvalidVersionReqError

Parse a semver from a string, returning the leftover text after parsing on success.

If you don't need the leftover text, use the parse twin function.

versionReq = parse "1.2.*, <1.2.5 ?"

expect versionReq == (
    Ok [
        Wildcard (MajorMinor 1 2),
        Relation { operator: LessThan, version: Full { major: 1, minor: 2, patch: 5, preRelease: [] } },
    ],
    " ?",
)

matches : VersionReq, Semver -> Bool

Check if a semver matches a version requirement.

The version must match all comparators, and if it has a pre-release, at least one of the comparators must have a pre-release and fully match the version's segments.

versionReq = [Relation { operator: Exact, version: Major 1 }]
semver = { major: 1, minor: 3, patch: 5, preRelease: [], build: [] }

expect versionReq |> matches semver