{- HLINT ignore "Use max" -}

-- | Internal module.
--
-- @since 0.1.0.0
module Data.Version.Package.Internal
  ( PackageVersion (..),
    ValidationError (..),
    ReadStringError (..),
    ReadFileError (..),
    mkPackageVersion,
    toText,
  )
where

import Control.DeepSeq (NFData)
import Control.Exception (Exception (displayException))
import Data.Foldable qualified as F
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.NonEmpty qualified as NE
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)
import Language.Haskell.TH.Syntax (Lift)

-- $setup
-- >>> :set -XOverloadedLists

-- | 'PackageVersion' represents [PVP](https://pvp.haskell.org/) version
-- numbers. It is similar to "Data.Version"'s 'Data.Version' except:
--
-- 1. 'PackageVersion' has no 'Data.Version.versionTags'.
-- 2. We enforce PVP invariants i.e.
--
--      * Tags must have at least one component.
--      * All components >= 0.
--
-- 3. Trailing zeroes are ignored in 'Eq', 'Ord', 'Semigroup', and 'Monoid'.
--
-- That is, we declare an equivalence class up to trailing zeroes.
-- In particular, the 'Monoid' identity is
--
-- @
-- [0] = { [0], [0,0], [0,0,0], ... }
-- @
--
-- and its 'Semigroup' instance takes the greatest version (based on 'Ord').
--
-- Note: Because we export the underlying list in various ways,
-- (e.g. 'show'), 'Eq'\'s extensionality law,
--
-- @
-- x == y ==> f x == f y
-- @
--
-- can be broken. Take care that you do not rely on this law if you are
-- using its underlying @NonEmpty 'Word'@ (or 'String') representation.
--
-- ==== __Examples__
-- >>> MkPackageVersion [0,0,0,0] == MkPackageVersion [0,0,0]
-- True
--
-- >>> MkPackageVersion [4,0,0] > MkPackageVersion [1,2,0,0]
-- True
--
-- >>> MkPackageVersion [5,6,0] <> MkPackageVersion [9,0,0]
-- MkPackageVersion {unPackageVersion = 9 :| [0,0]}
--
-- >>> MkPackageVersion [0,9] <> MkPackageVersion [0,9,0,0]
-- MkPackageVersion {unPackageVersion = 0 :| [9]}
--
-- @since 0.1.0.0
newtype PackageVersion = MkPackageVersion
  { -- | @since 0.4
    PackageVersion -> NonEmpty Word
unPackageVersion :: NonEmpty Word
  }
  deriving stock
    ( -- | @since 0.2
      (forall x. PackageVersion -> Rep PackageVersion x)
-> (forall x. Rep PackageVersion x -> PackageVersion)
-> Generic PackageVersion
forall x. Rep PackageVersion x -> PackageVersion
forall x. PackageVersion -> Rep PackageVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PackageVersion -> Rep PackageVersion x
from :: forall x. PackageVersion -> Rep PackageVersion x
$cto :: forall x. Rep PackageVersion x -> PackageVersion
to :: forall x. Rep PackageVersion x -> PackageVersion
Generic,
      -- | @since 0.1.0.0
      (forall (m :: * -> *). Quote m => PackageVersion -> m Exp)
-> (forall (m :: * -> *).
    Quote m =>
    PackageVersion -> Code m PackageVersion)
-> Lift PackageVersion
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => PackageVersion -> m Exp
forall (m :: * -> *).
Quote m =>
PackageVersion -> Code m PackageVersion
$clift :: forall (m :: * -> *). Quote m => PackageVersion -> m Exp
lift :: forall (m :: * -> *). Quote m => PackageVersion -> m Exp
$cliftTyped :: forall (m :: * -> *).
Quote m =>
PackageVersion -> Code m PackageVersion
liftTyped :: forall (m :: * -> *).
Quote m =>
PackageVersion -> Code m PackageVersion
Lift,
      -- | @since 0.1.0.0
      Int -> PackageVersion -> ShowS
[PackageVersion] -> ShowS
PackageVersion -> String
(Int -> PackageVersion -> ShowS)
-> (PackageVersion -> String)
-> ([PackageVersion] -> ShowS)
-> Show PackageVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackageVersion -> ShowS
showsPrec :: Int -> PackageVersion -> ShowS
$cshow :: PackageVersion -> String
show :: PackageVersion -> String
$cshowList :: [PackageVersion] -> ShowS
showList :: [PackageVersion] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.1.0.0
      PackageVersion -> ()
(PackageVersion -> ()) -> NFData PackageVersion
forall a. (a -> ()) -> NFData a
$crnf :: PackageVersion -> ()
rnf :: PackageVersion -> ()
NFData
    )

-- | @since 0.1.0.0
instance Eq PackageVersion where
  MkPackageVersion NonEmpty Word
v1 == :: PackageVersion -> PackageVersion -> Bool
== MkPackageVersion NonEmpty Word
v2 =
    NonEmpty Word -> [Word]
forall a. (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes NonEmpty Word
v1 [Word] -> [Word] -> Bool
forall a. Eq a => a -> a -> Bool
== NonEmpty Word -> [Word]
forall a. (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes NonEmpty Word
v2

-- | @since 0.1.0.0
instance Ord PackageVersion where
  MkPackageVersion NonEmpty Word
v1 compare :: PackageVersion -> PackageVersion -> Ordering
`compare` MkPackageVersion NonEmpty Word
v2 =
    NonEmpty Word -> [Word]
forall a. (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes NonEmpty Word
v1 [Word] -> [Word] -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` NonEmpty Word -> [Word]
forall a. (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes NonEmpty Word
v2

-- | @since 0.1.0.0
instance Semigroup PackageVersion where
  PackageVersion
x <> :: PackageVersion -> PackageVersion -> PackageVersion
<> PackageVersion
y =
    -- manual over using max so that we are left-biased
    if PackageVersion
x PackageVersion -> PackageVersion -> Bool
forall a. Ord a => a -> a -> Bool
>= PackageVersion
y
      then PackageVersion
x
      else PackageVersion
y

-- | @since 0.1.0.0
instance Monoid PackageVersion where
  mempty :: PackageVersion
mempty = NonEmpty Word -> PackageVersion
MkPackageVersion (Word
0 Word -> [Word] -> NonEmpty Word
forall a. a -> [a] -> NonEmpty a
:| [])

dropTrailingZeroes :: (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes :: forall a. (Eq a, Num a) => NonEmpty a -> [a]
dropTrailingZeroes NonEmpty a
xs = Int -> NonEmpty a -> [a]
forall a. Int -> NonEmpty a -> [a]
NE.take (NonEmpty a -> Int
lastNonZero NonEmpty a
xs) NonEmpty a
xs
  where
    lastNonZero :: NonEmpty a -> Int
lastNonZero = (Int, Int) -> Int
forall a b. (a, b) -> b
snd ((Int, Int) -> Int)
-> (NonEmpty a -> (Int, Int)) -> NonEmpty a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Int) -> a -> (Int, Int))
-> (Int, Int) -> NonEmpty a -> (Int, Int)
forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (Int, Int) -> a -> (Int, Int)
forall {a} {b}. (Eq a, Num a, Num b) => (b, b) -> a -> (b, b)
go (Int
0, Int
0)
    go :: (b, b) -> a -> (b, b)
go (!b
idx, !b
acc) a
x
      | a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 = (b
idx b -> b -> b
forall a. Num a => a -> a -> a
+ b
1, b
idx b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
      | Bool
otherwise = (b
idx b -> b -> b
forall a. Num a => a -> a -> a
+ b
1, b
acc)

-- | Errors that can occur when validating PVP version numbers.
--
-- @since 0.1.0.0
data ValidationError
  = -- | PVP version number cannot be empty.
    --
    -- @since 0.3
    ValidationErrorEmpty
  | -- | PVP version numbers cannot be negative.
    --
    -- @since 0.2
    ValidationErrorNegative Int
  deriving stock
    ( -- | @since 0.1.0.0
      ValidationError -> ValidationError -> Bool
(ValidationError -> ValidationError -> Bool)
-> (ValidationError -> ValidationError -> Bool)
-> Eq ValidationError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ValidationError -> ValidationError -> Bool
== :: ValidationError -> ValidationError -> Bool
$c/= :: ValidationError -> ValidationError -> Bool
/= :: ValidationError -> ValidationError -> Bool
Eq,
      -- | @since 0.1.0.0
      (forall x. ValidationError -> Rep ValidationError x)
-> (forall x. Rep ValidationError x -> ValidationError)
-> Generic ValidationError
forall x. Rep ValidationError x -> ValidationError
forall x. ValidationError -> Rep ValidationError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ValidationError -> Rep ValidationError x
from :: forall x. ValidationError -> Rep ValidationError x
$cto :: forall x. Rep ValidationError x -> ValidationError
to :: forall x. Rep ValidationError x -> ValidationError
Generic,
      -- | @since 0.1.0.0
      Int -> ValidationError -> ShowS
[ValidationError] -> ShowS
ValidationError -> String
(Int -> ValidationError -> ShowS)
-> (ValidationError -> String)
-> ([ValidationError] -> ShowS)
-> Show ValidationError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ValidationError -> ShowS
showsPrec :: Int -> ValidationError -> ShowS
$cshow :: ValidationError -> String
show :: ValidationError -> String
$cshowList :: [ValidationError] -> ShowS
showList :: [ValidationError] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ValidationError -> ()
(ValidationError -> ()) -> NFData ValidationError
forall a. (a -> ()) -> NFData a
$crnf :: ValidationError -> ()
rnf :: ValidationError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Exception ValidationError where
  displayException :: ValidationError -> String
displayException ValidationError
ValidationErrorEmpty = String
"PVP number cannot be empty"
  displayException (ValidationErrorNegative Int
i) =
    String
"PVP numbers cannot be negative: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
i

-- | Errors that can occur when reading PVP version numbers.
--
-- @since 0.1.0.0
data ReadStringError
  = -- | Error when parsing a string.
    --
    -- @since 0.2
    ReadStringErrorParse String
  | -- | Validation error.
    --
    -- @since 0.2
    ReadStringErrorValidate ValidationError
  deriving stock
    ( -- | @since 0.1.0.0
      ReadStringError -> ReadStringError -> Bool
(ReadStringError -> ReadStringError -> Bool)
-> (ReadStringError -> ReadStringError -> Bool)
-> Eq ReadStringError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReadStringError -> ReadStringError -> Bool
== :: ReadStringError -> ReadStringError -> Bool
$c/= :: ReadStringError -> ReadStringError -> Bool
/= :: ReadStringError -> ReadStringError -> Bool
Eq,
      -- | @since 0.1.0.0
      (forall x. ReadStringError -> Rep ReadStringError x)
-> (forall x. Rep ReadStringError x -> ReadStringError)
-> Generic ReadStringError
forall x. Rep ReadStringError x -> ReadStringError
forall x. ReadStringError -> Rep ReadStringError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ReadStringError -> Rep ReadStringError x
from :: forall x. ReadStringError -> Rep ReadStringError x
$cto :: forall x. Rep ReadStringError x -> ReadStringError
to :: forall x. Rep ReadStringError x -> ReadStringError
Generic,
      -- | @since 0.1.0.0
      Int -> ReadStringError -> ShowS
[ReadStringError] -> ShowS
ReadStringError -> String
(Int -> ReadStringError -> ShowS)
-> (ReadStringError -> String)
-> ([ReadStringError] -> ShowS)
-> Show ReadStringError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReadStringError -> ShowS
showsPrec :: Int -> ReadStringError -> ShowS
$cshow :: ReadStringError -> String
show :: ReadStringError -> String
$cshowList :: [ReadStringError] -> ShowS
showList :: [ReadStringError] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ReadStringError -> ()
(ReadStringError -> ()) -> NFData ReadStringError
forall a. (a -> ()) -> NFData a
$crnf :: ReadStringError -> ()
rnf :: ReadStringError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Exception ReadStringError where
  displayException :: ReadStringError -> String
displayException (ReadStringErrorParse String
err) = String
"Read error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err
  displayException (ReadStringErrorValidate ValidationError
i) =
    String
"Validation error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ValidationError -> String
forall e. Exception e => e -> String
displayException ValidationError
i

-- | Errors that can occur when reading PVP version numbers from a file.
--
-- @since 0.1.0.0
data ReadFileError
  = -- | General error when reading a file.
    --
    -- @since 0.2
    ReadFileErrorGeneral String
  | -- | Error for missing version.
    --
    -- @since 0.2
    ReadFileErrorVersionNotFound FilePath
  | -- | Read/Validation error.
    --
    -- @since 0.2
    ReadFileErrorReadString ReadStringError
  deriving stock
    ( -- | @since 0.1.0.0
      ReadFileError -> ReadFileError -> Bool
(ReadFileError -> ReadFileError -> Bool)
-> (ReadFileError -> ReadFileError -> Bool) -> Eq ReadFileError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReadFileError -> ReadFileError -> Bool
== :: ReadFileError -> ReadFileError -> Bool
$c/= :: ReadFileError -> ReadFileError -> Bool
/= :: ReadFileError -> ReadFileError -> Bool
Eq,
      -- | @since 0.1.0.0
      (forall x. ReadFileError -> Rep ReadFileError x)
-> (forall x. Rep ReadFileError x -> ReadFileError)
-> Generic ReadFileError
forall x. Rep ReadFileError x -> ReadFileError
forall x. ReadFileError -> Rep ReadFileError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ReadFileError -> Rep ReadFileError x
from :: forall x. ReadFileError -> Rep ReadFileError x
$cto :: forall x. Rep ReadFileError x -> ReadFileError
to :: forall x. Rep ReadFileError x -> ReadFileError
Generic,
      -- | @since 0.1.0.0
      Int -> ReadFileError -> ShowS
[ReadFileError] -> ShowS
ReadFileError -> String
(Int -> ReadFileError -> ShowS)
-> (ReadFileError -> String)
-> ([ReadFileError] -> ShowS)
-> Show ReadFileError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReadFileError -> ShowS
showsPrec :: Int -> ReadFileError -> ShowS
$cshow :: ReadFileError -> String
show :: ReadFileError -> String
$cshowList :: [ReadFileError] -> ShowS
showList :: [ReadFileError] -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ReadFileError -> ()
(ReadFileError -> ()) -> NFData ReadFileError
forall a. (a -> ()) -> NFData a
$crnf :: ReadFileError -> ()
rnf :: ReadFileError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Exception ReadFileError where
  displayException :: ReadFileError -> String
displayException (ReadFileErrorGeneral String
f) = String
"File not found: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
f
  displayException (ReadFileErrorVersionNotFound String
f) = String
"Version not found: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
f
  displayException (ReadFileErrorReadString ReadStringError
i) = String
"Read error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ReadStringError -> String
forall e. Exception e => e -> String
displayException ReadStringError
i

-- | Constructs a 'PackageVersion' from an 'Int' list. The list must be
-- non-empty to match PVP's minimal A. Furthermore, all digits must be
-- non-negative.
--
-- ==== __Examples__
--
-- >>> mkPackageVersion [1,2]
-- Right (MkPackageVersion {unPackageVersion = 1 :| [2]})
--
-- >>> mkPackageVersion [2,87,7,1]
-- Right (MkPackageVersion {unPackageVersion = 2 :| [87,7,1]})
--
-- >>> mkPackageVersion [1,2,-3,-4,5]
-- Left (ValidationErrorNegative (-3))
--
-- >>> mkPackageVersion [3]
-- Right (MkPackageVersion {unPackageVersion = 3 :| []})
--
-- >>> mkPackageVersion []
-- Left ValidationErrorEmpty
--
-- @since 0.1.0.0
mkPackageVersion :: [Int] -> Either ValidationError PackageVersion
mkPackageVersion :: [Int] -> Either ValidationError PackageVersion
mkPackageVersion vers :: [Int]
vers@(Int
v : [Int]
vs) = case (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) [Int]
vers of
  [] -> PackageVersion -> Either ValidationError PackageVersion
forall a b. b -> Either a b
Right (PackageVersion -> Either ValidationError PackageVersion)
-> PackageVersion -> Either ValidationError PackageVersion
forall a b. (a -> b) -> a -> b
$ NonEmpty Word -> PackageVersion
MkPackageVersion (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
v Word -> [Word] -> NonEmpty Word
forall a. a -> [a] -> NonEmpty a
:| (Int -> Word) -> [Int] -> [Word]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral [Int]
vs)
  (Int
neg : [Int]
_) -> ValidationError -> Either ValidationError PackageVersion
forall a b. a -> Either a b
Left (ValidationError -> Either ValidationError PackageVersion)
-> ValidationError -> Either ValidationError PackageVersion
forall a b. (a -> b) -> a -> b
$ Int -> ValidationError
ValidationErrorNegative Int
neg
mkPackageVersion [] = ValidationError -> Either ValidationError PackageVersion
forall a b. a -> Either a b
Left ValidationError
ValidationErrorEmpty

-- | Displays 'PackageVersion' in 'Text' format.
--
-- ==== __Examples__
-- >>> toText (MkPackageVersion [2,7,10,0])
-- "2.7.10.0"
--
-- @since 0.1.0.0
toText :: PackageVersion -> Text
toText :: PackageVersion -> Text
toText =
  Text -> [Text] -> Text
T.intercalate Text
"."
    ([Text] -> Text)
-> (PackageVersion -> [Text]) -> PackageVersion -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word -> Text) -> [Word] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
T.pack (String -> Text) -> (Word -> String) -> Word -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> String
forall a. Show a => a -> String
show)
    ([Word] -> [Text])
-> (PackageVersion -> [Word]) -> PackageVersion -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty Word -> [Word]
forall a. NonEmpty a -> [a]
NE.toList
    (NonEmpty Word -> [Word])
-> (PackageVersion -> NonEmpty Word) -> PackageVersion -> [Word]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageVersion -> NonEmpty Word
unPackageVersion