-- Module      : Data.SemVer
-- Copyright   : (c) 2014-2019 Brendan Hay <brendan.g.hay@gmail.com>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)

-- | An implementation of the Semantic Versioning specification located at
-- <http://semver.org>.
--
-- A canonical 'Version' type and functions representing behaviour as outlined
-- in the specification are defined alongside additional lenses, traversals,
-- common manipulations, and serialisation primitives.
module Data.SemVer
    (
    -- * Version
      Version
    -- ** Constructors
    , version
    , initial
    -- ** Lenses
    , major
    , minor
    , patch
    , release
    , metadata
    -- ** Incrementing
    -- $incrementing
    , incrementMajor
    , incrementMinor
    , incrementPatch
    -- ** Predicates
    , isDevelopment
    , isPublic
    -- ** Encoding
    , toString
    , toText
    , toLazyText
    , toBuilder
    -- ** Decoding
    , fromText
    , fromLazyText
    , parser

    -- * Identifiers
    , Identifier
    -- ** Constructors
    , numeric
    , textual
    -- ** Prisms
    , _Numeric
    , _Textual
    ) where

import           Control.Applicative
import           Data.Attoparsec.Text
import qualified Data.SemVer.Delimited  as Delim
import           Data.SemVer.Internal
import           Data.Text              (Text)
import qualified Data.Text              as Text
import qualified Data.Text.Lazy         as LText
import           Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as Build

-- | Smart constructor fully specifying all available version components.
version :: Int          -- ^ Major version component.
        -> Int          -- ^ Minor version component.
        -> Int          -- ^ Patch version component.
        -> [Identifier] -- ^ Release identifiers.
        -> [Identifier] -- ^ Metadata identifiers.
        -> Version
version :: Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
version = Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
Version
{-# INLINE version #-}

-- | A default 'Version' which can be used to signify initial development.
--
-- Note: Equivalent to @0.0.0@
initial :: Version
initial :: Version
initial = Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
version Int
0 Int
0 Int
0 [] []

-- | Lens for the major version component.
major :: Functor f => (Int -> f Int) -> Version -> f Version
major :: (Int -> f Int) -> Version -> f Version
major Int -> f Int
f Version
x = (\Int
y -> Version
x { _versionMajor :: Int
_versionMajor = Int
y }) (Int -> Version) -> f Int -> f Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f Int
f (Version -> Int
_versionMajor Version
x)
{-# INLINE major #-}

-- | Lens for minor version component.
minor :: Functor f => (Int -> f Int) -> Version -> f Version
minor :: (Int -> f Int) -> Version -> f Version
minor Int -> f Int
f Version
x = (\Int
y -> Version
x { _versionMinor :: Int
_versionMinor = Int
y }) (Int -> Version) -> f Int -> f Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f Int
f (Version -> Int
_versionMinor Version
x)
{-# INLINE minor #-}

-- | Lens for the patch version component.
patch :: Functor f => (Int -> f Int) -> Version -> f Version
patch :: (Int -> f Int) -> Version -> f Version
patch Int -> f Int
f Version
x = (\Int
y -> Version
x { _versionPatch :: Int
_versionPatch = Int
y }) (Int -> Version) -> f Int -> f Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f Int
f (Version -> Int
_versionPatch Version
x)
{-# INLINE patch #-}

-- | Lens for the list of release identifiers.
release :: Functor f
        => ([Identifier] -> f [Identifier])
        -> Version
        -> f Version
release :: ([Identifier] -> f [Identifier]) -> Version -> f Version
release [Identifier] -> f [Identifier]
f Version
x = (\[Identifier]
y -> Version
x { _versionRelease :: [Identifier]
_versionRelease = [Identifier]
y }) ([Identifier] -> Version) -> f [Identifier] -> f Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Identifier] -> f [Identifier]
f (Version -> [Identifier]
_versionRelease Version
x)
{-# INLINE release #-}

-- | Lens for the list of metadata identifiers.
metadata :: Functor f
         => ([Identifier] -> f [Identifier])
         -> Version
         -> f Version
metadata :: ([Identifier] -> f [Identifier]) -> Version -> f Version
metadata [Identifier] -> f [Identifier]
f Version
x = (\[Identifier]
y -> Version
x { _versionMeta :: [Identifier]
_versionMeta = [Identifier]
y }) ([Identifier] -> Version) -> f [Identifier] -> f Version
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Identifier] -> f [Identifier]
f (Version -> [Identifier]
_versionMeta Version
x)
{-# INLINE metadata #-}

-- $incrementing
--
-- The following increment functions are used to ensure that the related
-- version components are reset according to the specification.
--
-- See the individual function documentation for specifics regarding each
-- version component.

-- | Increment the major component of a 'Version' by 1, resetting the minor
-- and patch components.
--
-- * Major version X (X.y.z | X > 0) MUST be incremented if any backwards
-- incompatible changes are introduced to the public API.
--
-- * It MAY include minor and patch level changes.
--
-- * Patch and minor version MUST be reset to 0 when major version
-- is incremented.
incrementMajor :: Version -> Version
incrementMajor :: Version -> Version
incrementMajor Version
v = Version
v
    { _versionMajor :: Int
_versionMajor = Version -> Int
_versionMajor Version
v Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    , _versionMinor :: Int
_versionMinor = Int
0
    , _versionPatch :: Int
_versionPatch = Int
0
    }
{-# INLINE incrementMajor #-}

-- | Increment the minor component of a 'Version' by 1, resetting the
-- patch component.
--
-- * Minor version Y (x.Y.z | x > 0) MUST be incremented if new, backwards
-- compatible functionality is introduced to the public API.
--
-- * It MUST be incremented if any public API functionality is marked
-- as deprecated.
--
-- * It MAY be incremented if substantial new functionality or improvements
-- are introduced within the private code.
--
-- * It MAY include patch level changes.
--
-- * Patch version MUST be reset to 0 when minor version is incremented.
incrementMinor :: Version -> Version
incrementMinor :: Version -> Version
incrementMinor Version
v = Version
v
    { _versionMinor :: Int
_versionMinor = Version -> Int
_versionMinor Version
v Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    , _versionPatch :: Int
_versionPatch = Int
0
    }
{-# INLINE incrementMinor #-}

-- | Increment the patch component of a 'Version' by 1.
--
-- * Patch version Z (x.y.Z | x > 0) MUST be incremented if only backwards
-- compatible bug fixes are introduced.
--
-- * A bug fix is defined as an internal change that fixes incorrect behavior.
incrementPatch :: Version -> Version
incrementPatch :: Version -> Version
incrementPatch Version
v = Version
v
    { _versionPatch :: Int
_versionPatch = Version -> Int
_versionPatch Version
v Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    }
{-# INLINE incrementPatch #-}

-- | Check if the 'Version' is considered unstable.
--
-- * Major version zero (0.y.z) is for initial development.
--
-- * Anything may change at any time.
--
-- * The public API should not be considered stable.
isDevelopment :: Version -> Bool
isDevelopment :: Version -> Bool
isDevelopment = (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (Int -> Bool) -> (Version -> Int) -> Version -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Int
_versionMajor
{-# INLINE isDevelopment #-}

-- | Check if the 'Version' is considered stable.
--
-- Version 1.0.0 defines the public API. The way in which the version number
-- is incremented after this release is dependent on this public API and how
-- it changes.
isPublic :: Version -> Bool
isPublic :: Version -> Bool
isPublic = (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1) (Int -> Bool) -> (Version -> Int) -> Version -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Int
_versionMajor
{-# INLINE isPublic #-}

-- | Convert a 'Version' to it's readable 'String' representation.
--
-- Note: This is optimised for cases where you require 'String' output, and
-- as such is faster than the semantically equivalent @unpack . toLazyText@.
toString :: Version -> String
toString :: Version -> String
toString = (Char -> String)
-> (Int -> String)
-> (Text -> String)
-> Delimiters
-> Version
-> String
forall m.
Monoid m =>
(Char -> m)
-> (Int -> m) -> (Text -> m) -> Delimiters -> Version -> m
toMonoid (Char -> String -> String
forall a. a -> [a] -> [a]
:[]) Int -> String
forall a. Show a => a -> String
show Text -> String
Text.unpack Delimiters
Delim.semantic
{-# INLINE toString #-}

-- | Convert a 'Version' to a strict 'Text' representation.
--
-- Note: Equivalent to @toStrict . toLazyText@
toText :: Version -> Text
toText :: Version -> Text
toText = Text -> Text
LText.toStrict (Text -> Text) -> (Version -> Text) -> Version -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Text
toLazyText
{-# INLINE toText #-}

-- | Convert a 'Version' to a 'LText.Text' representation.
--
-- Note: This uses a lower 'Builder' buffer size optimised for commonly
-- found version formats. If you have particuarly long version numbers
-- using 'toBuilder' and 'Build.toLazyTextWith' to control the buffer size
-- is recommended.
toLazyText :: Version -> LText.Text
toLazyText :: Version -> Text
toLazyText = Int -> Builder -> Text
Build.toLazyTextWith Int
24 (Builder -> Text) -> (Version -> Builder) -> Version -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Version -> Builder
toBuilder
{-# INLINE toLazyText #-}

-- | Convert a 'Version' to a 'Builder'.
toBuilder :: Version -> Builder
toBuilder :: Version -> Builder
toBuilder = Delimiters -> Version -> Builder
Delim.toBuilder Delimiters
Delim.semantic
{-# INLINE toBuilder #-}

-- | Parse a 'Version' from 'Text', returning an attoparsec error message
-- in the 'Left' case on failure.
fromText :: Text -> Either String Version
fromText :: Text -> Either String Version
fromText = Parser Version -> Text -> Either String Version
forall a. Parser a -> Text -> Either String a
parseOnly Parser Version
parser
{-# INLINE fromText #-}

-- | Parse a 'Version' from 'LText.Text', returning an attoparsec error message
-- in the 'Left' case on failure.
--
-- Note: The underlying attoparsec 'Parser' is based on 'Text' and this is
-- equivalent to @fromText . toStrict@
fromLazyText :: LText.Text -> Either String Version
fromLazyText :: Text -> Either String Version
fromLazyText = Text -> Either String Version
fromText (Text -> Either String Version)
-> (Text -> Text) -> Text -> Either String Version
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
LText.toStrict
{-# INLINE fromLazyText #-}

-- | A greedy attoparsec 'Parser' which requires the entire 'Text'
-- input to match.
parser :: Parser Version
parser :: Parser Version
parser = Delimiters -> Bool -> Parser Version
Delim.parser Delimiters
Delim.semantic Bool
True
{-# INLINE parser #-}

-- | Safely construct a numeric identifier.
numeric :: Int -> Identifier
numeric :: Int -> Identifier
numeric = Int -> Identifier
INum
{-# INLINE numeric #-}

-- | Construct an identifier from the given 'Text', returning 'Nothing' if
-- neither a numeric or valid textual input is supplied.
textual :: Text -> Maybe Identifier
textual :: Text -> Maybe Identifier
textual = (String -> Maybe Identifier)
-> (Text -> Maybe Identifier)
-> Either String Text
-> Maybe Identifier
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Identifier -> String -> Maybe Identifier
forall a b. a -> b -> a
const Maybe Identifier
forall a. Maybe a
Nothing) (Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just (Identifier -> Maybe Identifier)
-> (Text -> Identifier) -> Text -> Maybe Identifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Identifier
IText)
    (Either String Text -> Maybe Identifier)
-> (Text -> Either String Text) -> Text -> Maybe Identifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Text -> Text -> Either String Text
forall a. Parser a -> Text -> Either String a
parseOnly (Parser () -> Parser Text
textualParser Parser ()
forall t. Chunk t => Parser t ()
endOfInput Parser Text -> Parser () -> Parser Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
forall t. Chunk t => Parser t ()
endOfInput)
{-# INLINE textual #-}

-- | A prism into the numeric branch of an 'Identifier'.
_Numeric :: Applicative f => (Int -> f Int) -> Identifier -> f Identifier
_Numeric :: (Int -> f Int) -> Identifier -> f Identifier
_Numeric Int -> f Int
f (INum Int
x) = Int -> Identifier
INum (Int -> Identifier) -> f Int -> f Identifier
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f Int
f Int
x
_Numeric Int -> f Int
_ Identifier
x        = Identifier -> f Identifier
forall (f :: * -> *) a. Applicative f => a -> f a
pure Identifier
x
{-# INLINE _Numeric #-}

-- | A prism into the textual branch of an 'Identifier'.
_Textual :: Applicative f => (Text -> f Text) -> Identifier -> f (Maybe Identifier)
_Textual :: (Text -> f Text) -> Identifier -> f (Maybe Identifier)
_Textual Text -> f Text
f (IText Text
x) = Text -> Maybe Identifier
textual (Text -> Maybe Identifier) -> f Text -> f (Maybe Identifier)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f Text
f Text
x
_Textual Text -> f Text
_ Identifier
x         = Maybe Identifier -> f (Maybe Identifier)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Identifier -> Maybe Identifier
forall a. a -> Maybe a
Just Identifier
x)
{-# INLINE _Textual #-}