module Data.SemVer
(
Version
, version
, initial
, major
, minor
, patch
, release
, metadata
, incrementMajor
, incrementMinor
, incrementPatch
, isDevelopment
, isPublic
, toString
, toText
, toLazyText
, toBuilder
, fromText
, fromLazyText
, parser
, Identifier
, numeric
, textual
, _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
version :: Int
-> Int
-> Int
-> [Identifier]
-> [Identifier]
-> Version
version :: Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
version = Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
Version
{-# INLINE version #-}
initial :: Version
initial :: Version
initial = Int -> Int -> Int -> [Identifier] -> [Identifier] -> Version
version Int
0 Int
0 Int
0 [] []
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
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 #-}
toBuilder :: Version -> Builder
toBuilder :: Version -> Builder
toBuilder = Delimiters -> Version -> Builder
Delim.toBuilder Delimiters
Delim.semantic
{-# INLINE toBuilder #-}
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 #-}
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 #-}
parser :: Parser Version
parser :: Parser Version
parser = Delimiters -> Bool -> Parser Version
Delim.parser Delimiters
Delim.semantic Bool
True
{-# INLINE parser #-}
numeric :: Int -> Identifier
numeric :: Int -> Identifier
numeric = Int -> Identifier
INum
{-# INLINE numeric #-}
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 #-}
_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 #-}
_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 #-}