Play

Artifact [92b365e1e7]
Login

Artifact 92b365e1e7f66c7e59cf481927bcd732203dda9c9787a964a1d67bfa83bb594b:


module Play.Data.SemanticVersion exposing
    ( SemanticVersion
    , fromString
    )


type SemanticVersion
    = SemanticVersion Int Int Int


fromString : String -> Result () SemanticVersion
fromString str =
    case String.split "." str of
        [ major, minor, patch ] ->
            toInt major minor patch

        _ ->
            Err ()


toInt : String -> String -> String -> Result () SemanticVersion
toInt majorStr minorStr patchStr =
    let
        intVersions =
            [ majorStr, minorStr, patchStr ]
                |> List.filterMap String.toInt
    in
    case intVersions of
        [ major, minor, patch ] ->
            if major < 0 || minor < 0 || patch < 0 then
                Err ()

            else if major == 0 && minor == 0 && patch < 1 then
                Err ()

            else
                Ok <| SemanticVersion major minor patch

        _ ->
            Err ()