{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Internal module. Exposes the invariant-breaking 'UnsafePackageVersion'
-- constructor.
--
-- @since 0.1.0.0
module Data.Version.Package.Internal
  ( PackageVersion (MkPackageVersion, ..),
    ValidationError (..),
    ReadStringError (..),
    ReadFileError (..),
    mkPackageVersion,
    unPackageVersion,
    toText,
    prettyString,
  )
where

import Control.DeepSeq (NFData (..))
import Control.Exception.Safe (Exception (..))
import Data.Foldable qualified as F
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)
import GHC.Read qualified as RD
import Language.Haskell.TH.Syntax (Lift (..))
#if MIN_VERSION_prettyprinter(1, 7, 1)
import Prettyprinter (Pretty (..), defaultLayoutOptions, layoutSmart, (<+>))
import Prettyprinter.Render.String (renderString)
#else
import Data.Text.Prettyprint.Doc (Pretty (..), defaultLayoutOptions, layoutPretty, (<+>))
import Data.Text.Prettyprint.Doc.Render.String (renderString)
#endif
import Text.Read qualified as TR

-- | 'PackageVersion' represents [PVP](https://pvp.haskell.org/) version
-- numbers. It is similar to "Data.Version"'s 'Data.Version' (i.e. wraps a
-- @['Int']@) except:
--
-- 1. 'PackageVersion' has no 'Data.Version.versionTags'.
-- 2. We enforce PVP's "tags must be at least A" invariant via the
--    smart-constructor pattern.
-- 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 @['Int']@ (or 'String') representation.
--
-- ==== __Examples__
-- >>> UnsafePackageVersion [0,0,0,0] == UnsafePackageVersion [0,0,0]
-- True
--
-- >>> UnsafePackageVersion [4,0,0] > UnsafePackageVersion [1,2,0,0]
-- True
--
-- >>> UnsafePackageVersion [5,6,0] <> UnsafePackageVersion [9,0,0]
-- UnsafePackageVersion [9,0,0]
--
-- >>> UnsafePackageVersion [0,9] <> UnsafePackageVersion [0,9,0,0]
-- UnsafePackageVersion [0,9]
--
-- >>> TR.readEither @PackageVersion "UnsafePackageVersion [3,2,1]"
-- Right (UnsafePackageVersion [3,2,1])
--
-- >>> TR.readEither @PackageVersion "UnsafePackageVersion [-2]"
-- Left "Prelude.read: no parse"
--
-- >>> TR.readEither @PackageVersion "UnsafePackageVersion []"
-- Left "Prelude.read: no parse"
--
-- @since 0.1.0.0
newtype PackageVersion = UnsafePackageVersion [Int]
  deriving stock
    ( -- | @since 0.2
      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
$cto :: forall x. Rep PackageVersion x -> PackageVersion
$cfrom :: forall x. PackageVersion -> Rep PackageVersion x
Generic,
      -- | @since 0.1.0.0
      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
liftTyped :: forall (m :: * -> *).
Quote m =>
PackageVersion -> Code m PackageVersion
$cliftTyped :: forall (m :: * -> *).
Quote m =>
PackageVersion -> Code m PackageVersion
lift :: forall (m :: * -> *). Quote m => PackageVersion -> m Exp
$clift :: forall (m :: * -> *). Quote m => PackageVersion -> m Exp
Lift,
      -- | @since 0.1.0.0
      Int -> PackageVersion -> ShowS
[PackageVersion] -> ShowS
PackageVersion -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PackageVersion] -> ShowS
$cshowList :: [PackageVersion] -> ShowS
show :: PackageVersion -> String
$cshow :: PackageVersion -> String
showsPrec :: Int -> PackageVersion -> ShowS
$cshowsPrec :: Int -> PackageVersion -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.1.0.0
      PackageVersion -> ()
forall a. (a -> ()) -> NFData a
rnf :: PackageVersion -> ()
$crnf :: PackageVersion -> ()
NFData
    )

-- | @since 0.1.0.0
pattern MkPackageVersion :: [Int] -> PackageVersion
pattern $mMkPackageVersion :: forall {r}. PackageVersion -> ([Int] -> r) -> ((# #) -> r) -> r
MkPackageVersion v <- UnsafePackageVersion v

{-# COMPLETE MkPackageVersion #-}

-- | @since 0.3
unPackageVersion :: PackageVersion -> [Int]
unPackageVersion :: PackageVersion -> [Int]
unPackageVersion (UnsafePackageVersion [Int]
x) = [Int]
x
{-# INLINE unPackageVersion #-}

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

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

-- | @since 0.1.0.0
instance Semigroup PackageVersion where
  PackageVersion
pv1 <> :: PackageVersion -> PackageVersion -> PackageVersion
<> PackageVersion
pv2 =
    case PackageVersion
pv1 forall a. Ord a => a -> a -> Ordering
`compare` PackageVersion
pv2 of
      Ordering
LT -> PackageVersion
pv2
      Ordering
_ -> PackageVersion
pv1

-- | @since 0.1.0.0
instance Monoid PackageVersion where
  mempty :: PackageVersion
mempty = [Int] -> PackageVersion
UnsafePackageVersion [Int
0]

-- | @since 0.1.0.0
instance Read PackageVersion where
  readPrec :: ReadPrec PackageVersion
readPrec = forall a. ReadPrec a -> ReadPrec a
TR.parens forall a b. (a -> b) -> a -> b
$
    forall a. Int -> ReadPrec a -> ReadPrec a
TR.prec Int
10 forall a b. (a -> b) -> a -> b
$ do
      Lexeme -> ReadPrec ()
RD.expectP forall a b. (a -> b) -> a -> b
$ String -> Lexeme
TR.Ident String
"UnsafePackageVersion"
      [Int]
intList <- forall a. ReadPrec a -> ReadPrec a
TR.step forall a. Read a => ReadPrec a
RD.readPrec
      case [Int] -> Either ValidationError PackageVersion
mkPackageVersion [Int]
intList of
        Left ValidationError
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ forall a. Pretty a => a -> String
prettyString ValidationError
err
        Right PackageVersion
pv -> forall (f :: * -> *) a. Applicative f => a -> f a
pure PackageVersion
pv

  readListPrec :: ReadPrec [PackageVersion]
readListPrec = forall a. Read a => ReadPrec [a]
TR.readListPrecDefault

-- | @since 0.1.0.0
instance Pretty PackageVersion where
  pretty :: forall ann. PackageVersion -> Doc ann
pretty = forall a ann. Pretty a => a -> Doc ann
pretty forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageVersion -> Text
toText

dropTrailingZeroes :: (Eq a, Num a) => [a] -> [a]
dropTrailingZeroes :: forall a. (Eq a, Num a) => [a] -> [a]
dropTrailingZeroes [a]
xs = forall a. Int -> [a] -> [a]
take ([a] -> Int
lastNonZero [a]
xs) [a]
xs
  where
    lastNonZero :: [a] -> Int
lastNonZero = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' 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 forall a. Eq a => a -> a -> Bool
/= a
0 = (b
idx forall a. Num a => a -> a -> a
+ b
1, b
idx forall a. Num a => a -> a -> a
+ b
1)
      | Bool
otherwise = (b
idx 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ValidationError -> ValidationError -> Bool
$c/= :: ValidationError -> ValidationError -> Bool
== :: ValidationError -> ValidationError -> Bool
$c== :: ValidationError -> ValidationError -> Bool
Eq,
      -- | @since 0.1.0.0
      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
$cto :: forall x. Rep ValidationError x -> ValidationError
$cfrom :: forall x. ValidationError -> Rep ValidationError x
Generic,
      -- | @since 0.1.0.0
      Int -> ValidationError -> ShowS
[ValidationError] -> ShowS
ValidationError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ValidationError] -> ShowS
$cshowList :: [ValidationError] -> ShowS
show :: ValidationError -> String
$cshow :: ValidationError -> String
showsPrec :: Int -> ValidationError -> ShowS
$cshowsPrec :: Int -> ValidationError -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ValidationError -> ()
forall a. (a -> ()) -> NFData a
rnf :: ValidationError -> ()
$crnf :: ValidationError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Pretty ValidationError where
  pretty :: forall ann. ValidationError -> Doc ann
pretty ValidationError
ValidationErrorEmpty = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"PVP number cannot be empty"
  pretty (ValidationErrorNegative Int
i) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"PVP numbers cannot be negative:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty Int
i

-- | @since 0.1.0.0
instance Exception ValidationError where
  displayException :: ValidationError -> String
displayException = forall a. Pretty a => a -> String
prettyString

-- | 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ReadStringError -> ReadStringError -> Bool
$c/= :: ReadStringError -> ReadStringError -> Bool
== :: ReadStringError -> ReadStringError -> Bool
$c== :: ReadStringError -> ReadStringError -> Bool
Eq,
      -- | @since 0.1.0.0
      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
$cto :: forall x. Rep ReadStringError x -> ReadStringError
$cfrom :: forall x. ReadStringError -> Rep ReadStringError x
Generic,
      -- | @since 0.1.0.0
      Int -> ReadStringError -> ShowS
[ReadStringError] -> ShowS
ReadStringError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ReadStringError] -> ShowS
$cshowList :: [ReadStringError] -> ShowS
show :: ReadStringError -> String
$cshow :: ReadStringError -> String
showsPrec :: Int -> ReadStringError -> ShowS
$cshowsPrec :: Int -> ReadStringError -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ReadStringError -> ()
forall a. (a -> ()) -> NFData a
rnf :: ReadStringError -> ()
$crnf :: ReadStringError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Pretty ReadStringError where
  pretty :: forall ann. ReadStringError -> Doc ann
pretty (ReadStringErrorParse String
err) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"Read error:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty String
err
  pretty (ReadStringErrorValidate ValidationError
i) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"Validation error:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty ValidationError
i

-- | @since 0.1.0.0
instance Exception ReadStringError where
  displayException :: ReadStringError -> String
displayException = forall a. Pretty a => a -> String
prettyString

-- | 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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ReadFileError -> ReadFileError -> Bool
$c/= :: ReadFileError -> ReadFileError -> Bool
== :: ReadFileError -> ReadFileError -> Bool
$c== :: ReadFileError -> ReadFileError -> Bool
Eq,
      -- | @since 0.1.0.0
      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
$cto :: forall x. Rep ReadFileError x -> ReadFileError
$cfrom :: forall x. ReadFileError -> Rep ReadFileError x
Generic,
      -- | @since 0.1.0.0
      Int -> ReadFileError -> ShowS
[ReadFileError] -> ShowS
ReadFileError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ReadFileError] -> ShowS
$cshowList :: [ReadFileError] -> ShowS
show :: ReadFileError -> String
$cshow :: ReadFileError -> String
showsPrec :: Int -> ReadFileError -> ShowS
$cshowsPrec :: Int -> ReadFileError -> ShowS
Show
    )
  deriving anyclass
    ( -- | @since 0.2
      ReadFileError -> ()
forall a. (a -> ()) -> NFData a
rnf :: ReadFileError -> ()
$crnf :: ReadFileError -> ()
NFData
    )

-- | @since 0.1.0.0
instance Pretty ReadFileError where
  pretty :: forall ann. ReadFileError -> Doc ann
pretty (ReadFileErrorGeneral String
f) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"File not found:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty String
f
  pretty (ReadFileErrorVersionNotFound String
f) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"Version not found:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty String
f
  pretty (ReadFileErrorReadString ReadStringError
i) = forall a ann. Pretty a => a -> Doc ann
pretty @Text Text
"Read error:" forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall a ann. Pretty a => a -> Doc ann
pretty ReadStringError
i

-- | @since 0.1.0.0
instance Exception ReadFileError where
  displayException :: ReadFileError -> String
displayException = forall a. Pretty a => a -> String
prettyString

-- | Smart constructor for 'PackageVersion'. The length of the list must be
-- > 1 to match PVP's minimal A.B. Furthermore, all digits must be non-negative.
--
-- ==== __Examples__
--
-- >>> mkPackageVersion [1,2]
-- Right (UnsafePackageVersion [1,2])
--
-- >>> mkPackageVersion [2,87,7,1]
-- Right (UnsafePackageVersion [2,87,7,1])
--
-- >>> mkPackageVersion [1,2,-3,-4,5]
-- Left (ValidationErrorNegative (-3))
--
-- >>> mkPackageVersion [3]
-- Right (UnsafePackageVersion [3])
--
-- >>> mkPackageVersion []
-- Left ValidationErrorEmpty
--
-- @since 0.1.0.0
mkPackageVersion :: [Int] -> Either ValidationError PackageVersion
mkPackageVersion :: [Int] -> Either ValidationError PackageVersion
mkPackageVersion v :: [Int]
v@(Int
_ : [Int]
_) = case forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Ord a => a -> a -> Bool
< Int
0) [Int]
v of
  [] -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ [Int] -> PackageVersion
UnsafePackageVersion [Int]
v
  (Int
neg : [Int]
_) -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Int -> ValidationError
ValidationErrorNegative Int
neg
mkPackageVersion [] = forall a b. a -> Either a b
Left ValidationError
ValidationErrorEmpty

-- | Displays 'PackageVersion' in 'Text' format.
--
-- ==== __Examples__
-- >>> toText (UnsafePackageVersion [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
"." forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageVersion -> [Int]
unPackageVersion

-- | Renders a string via 'Pretty''s smart option + default layout.
--
-- @since 0.2
prettyString :: Pretty a => a -> String
prettyString :: forall a. Pretty a => a -> String
prettyString =
  forall ann. SimpleDocStream ann -> String
renderString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart LayoutOptions
defaultLayoutOptions
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a ann. Pretty a => a -> Doc ann
pretty