    {-# LANGUAGE CPP #-}
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE ViewPatterns #-}
    module Hpack.Syntax.DependencyVersion (
      githubBaseUrl
    , GitRef
    , GitUrl

    , VersionConstraint(..)
    , versionConstraint
    , anyVersion
    , versionRange

    , DependencyVersion(..)
    , withDependencyVersion
    , dependencyVersion

    , SourceDependency(..)
    , objectDependency

    , versionConstraintFromCabal

    , scientificToVersion
    , cabalParse
    ) where

    import           Imports

    import qualified Control.Monad.Fail as Fail
    import           Data.Maybe
    import           Data.Scientific
    import qualified Data.Text as T
    import qualified Data.Aeson.Config.KeyMap as KeyMap
    import           Text.PrettyPrint (renderStyle, Style(..), Mode(..))

    import qualified Distribution.Version as D

    import qualified Distribution.Parsec as D
    import qualified Distribution.Pretty as D
    import qualified Distribution.Types.VersionRange.Internal as D

    import           Data.Aeson.Config.FromValue

    githubBaseUrl :: String
    githubBaseUrl :: [Char]
    githubBaseUrl = [Char]
    "https://github.com/"

    type GitUrl = String
    type GitRef = String

    data VersionConstraint = AnyVersion | VersionRange String
      deriving (VersionConstraint -> VersionConstraint -> Bool
    (VersionConstraint -> VersionConstraint -> Bool)
    -> (VersionConstraint -> VersionConstraint -> Bool)
    -> Eq VersionConstraint
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: VersionConstraint -> VersionConstraint -> Bool
    == :: VersionConstraint -> VersionConstraint -> Bool
    $c/= :: VersionConstraint -> VersionConstraint -> Bool
    /= :: VersionConstraint -> VersionConstraint -> Bool
    Eq, Eq VersionConstraint
    Eq VersionConstraint
    -> (VersionConstraint -> VersionConstraint -> Ordering)
    -> (VersionConstraint -> VersionConstraint -> Bool)
    -> (VersionConstraint -> VersionConstraint -> Bool)
    -> (VersionConstraint -> VersionConstraint -> Bool)
    -> (VersionConstraint -> VersionConstraint -> Bool)
    -> (VersionConstraint -> VersionConstraint -> VersionConstraint)
    -> (VersionConstraint -> VersionConstraint -> VersionConstraint)
    -> Ord VersionConstraint
    VersionConstraint -> VersionConstraint -> Bool
    VersionConstraint -> VersionConstraint -> Ordering
    VersionConstraint -> VersionConstraint -> VersionConstraint
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: VersionConstraint -> VersionConstraint -> Ordering
    compare :: VersionConstraint -> VersionConstraint -> Ordering
    $c< :: VersionConstraint -> VersionConstraint -> Bool
    < :: VersionConstraint -> VersionConstraint -> Bool
    $c<= :: VersionConstraint -> VersionConstraint -> Bool
    <= :: VersionConstraint -> VersionConstraint -> Bool
    $c> :: VersionConstraint -> VersionConstraint -> Bool
    > :: VersionConstraint -> VersionConstraint -> Bool
    $c>= :: VersionConstraint -> VersionConstraint -> Bool
    >= :: VersionConstraint -> VersionConstraint -> Bool
    $cmax :: VersionConstraint -> VersionConstraint -> VersionConstraint
    max :: VersionConstraint -> VersionConstraint -> VersionConstraint
    $cmin :: VersionConstraint -> VersionConstraint -> VersionConstraint
    min :: VersionConstraint -> VersionConstraint -> VersionConstraint
    Ord, Int -> VersionConstraint -> ShowS
    [VersionConstraint] -> ShowS
    VersionConstraint -> [Char]
    (Int -> VersionConstraint -> ShowS)
    -> (VersionConstraint -> [Char])
    -> ([VersionConstraint] -> ShowS)
    -> Show VersionConstraint
    forall a.
    (Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> VersionConstraint -> ShowS
    showsPrec :: Int -> VersionConstraint -> ShowS
    $cshow :: VersionConstraint -> [Char]
    show :: VersionConstraint -> [Char]
    $cshowList :: [VersionConstraint] -> ShowS
    showList :: [VersionConstraint] -> ShowS
    Show)

    instance FromValue VersionConstraint where
      fromValue :: Value -> Parser VersionConstraint
    fromValue = Value -> Parser VersionConstraint
    versionConstraint

    versionConstraint :: Value -> Parser VersionConstraint
    versionConstraint :: Value -> Parser VersionConstraint
    versionConstraint Value
    v = case Value
    v of
      Value
    Null -> VersionConstraint -> Parser VersionConstraint
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return VersionConstraint
    AnyVersion
      Number Scientific
    n -> VersionConstraint -> Parser VersionConstraint
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Scientific -> VersionConstraint
    numericVersionConstraint Scientific
    n)
      String Text
    s -> Text -> Parser VersionConstraint
    stringVersionConstraint Text
    s
      Value
    _ -> [Char] -> Value -> Parser VersionConstraint
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Null, Number, or String" Value
    v

    anyVersion :: DependencyVersion
    anyVersion :: DependencyVersion
    anyVersion = Maybe SourceDependency -> VersionConstraint -> DependencyVersion
    DependencyVersion Maybe SourceDependency
    forall a. Maybe a
    Nothing VersionConstraint
    AnyVersion

    versionRange :: String -> DependencyVersion
    versionRange :: [Char] -> DependencyVersion
    versionRange = Maybe SourceDependency -> VersionConstraint -> DependencyVersion
    DependencyVersion Maybe SourceDependency
    forall a. Maybe a
    Nothing (VersionConstraint -> DependencyVersion)
    -> ([Char] -> VersionConstraint) -> [Char] -> DependencyVersion
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Char] -> VersionConstraint
    VersionRange

    data DependencyVersion = DependencyVersion (Maybe SourceDependency) VersionConstraint
      deriving (DependencyVersion -> DependencyVersion -> Bool
    (DependencyVersion -> DependencyVersion -> Bool)
    -> (DependencyVersion -> DependencyVersion -> Bool)
    -> Eq DependencyVersion
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: DependencyVersion -> DependencyVersion -> Bool
    == :: DependencyVersion -> DependencyVersion -> Bool
    $c/= :: DependencyVersion -> DependencyVersion -> Bool
    /= :: DependencyVersion -> DependencyVersion -> Bool
    Eq, Eq DependencyVersion
    Eq DependencyVersion
    -> (DependencyVersion -> DependencyVersion -> Ordering)
    -> (DependencyVersion -> DependencyVersion -> Bool)
    -> (DependencyVersion -> DependencyVersion -> Bool)
    -> (DependencyVersion -> DependencyVersion -> Bool)
    -> (DependencyVersion -> DependencyVersion -> Bool)
    -> (DependencyVersion -> DependencyVersion -> DependencyVersion)
    -> (DependencyVersion -> DependencyVersion -> DependencyVersion)
    -> Ord DependencyVersion
    DependencyVersion -> DependencyVersion -> Bool
    DependencyVersion -> DependencyVersion -> Ordering
    DependencyVersion -> DependencyVersion -> DependencyVersion
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: DependencyVersion -> DependencyVersion -> Ordering
    compare :: DependencyVersion -> DependencyVersion -> Ordering
    $c< :: DependencyVersion -> DependencyVersion -> Bool
    < :: DependencyVersion -> DependencyVersion -> Bool
    $c<= :: DependencyVersion -> DependencyVersion -> Bool
    <= :: DependencyVersion -> DependencyVersion -> Bool
    $c> :: DependencyVersion -> DependencyVersion -> Bool
    > :: DependencyVersion -> DependencyVersion -> Bool
    $c>= :: DependencyVersion -> DependencyVersion -> Bool
    >= :: DependencyVersion -> DependencyVersion -> Bool
    $cmax :: DependencyVersion -> DependencyVersion -> DependencyVersion
    max :: DependencyVersion -> DependencyVersion -> DependencyVersion
    $cmin :: DependencyVersion -> DependencyVersion -> DependencyVersion
    min :: DependencyVersion -> DependencyVersion -> DependencyVersion
    Ord, Int -> DependencyVersion -> ShowS
    [DependencyVersion] -> ShowS
    DependencyVersion -> [Char]
    (Int -> DependencyVersion -> ShowS)
    -> (DependencyVersion -> [Char])
    -> ([DependencyVersion] -> ShowS)
    -> Show DependencyVersion
    forall a.
    (Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> DependencyVersion -> ShowS
    showsPrec :: Int -> DependencyVersion -> ShowS
    $cshow :: DependencyVersion -> [Char]
    show :: DependencyVersion -> [Char]
    $cshowList :: [DependencyVersion] -> ShowS
    showList :: [DependencyVersion] -> ShowS
    Show)

    withDependencyVersion
      :: (DependencyVersion -> a)
      -> (Object -> DependencyVersion -> Parser a)
      -> Value
      -> Parser a
    withDependencyVersion :: forall a.
    (DependencyVersion -> a)
    -> (Object -> DependencyVersion -> Parser a) -> Value -> Parser a
    withDependencyVersion DependencyVersion -> a
    k Object -> DependencyVersion -> Parser a
    obj Value
    v = case Value
    v of
      Value
    Null -> a -> Parser a
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a -> Parser a) -> a -> Parser a
    forall a b. (a -> b) -> a -> b
    $ DependencyVersion -> a
    k DependencyVersion
    anyVersion
      Object Object
    o -> Object -> Parser DependencyVersion
    objectDependency Object
    o Parser DependencyVersion
    -> (DependencyVersion -> Parser a) -> Parser a
    forall a b. Parser a -> (a -> Parser b) -> Parser b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Object -> DependencyVersion -> Parser a
    obj Object
    o
      Number Scientific
    n -> a -> Parser a
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a -> Parser a) -> a -> Parser a
    forall a b. (a -> b) -> a -> b
    $ DependencyVersion -> a
    k (Maybe SourceDependency -> VersionConstraint -> DependencyVersion
    DependencyVersion Maybe SourceDependency
    forall a. Maybe a
    Nothing (VersionConstraint -> DependencyVersion)
    -> VersionConstraint -> DependencyVersion
    forall a b. (a -> b) -> a -> b
    $ Scientific -> VersionConstraint
    numericVersionConstraint Scientific
    n)
      String Text
    s -> DependencyVersion -> a
    k (DependencyVersion -> a)
    -> (VersionConstraint -> DependencyVersion)
    -> VersionConstraint
    -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe SourceDependency -> VersionConstraint -> DependencyVersion
    DependencyVersion Maybe SourceDependency
    forall a. Maybe a
    Nothing (VersionConstraint -> a) -> Parser VersionConstraint -> Parser a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Parser VersionConstraint
    stringVersionConstraint Text
    s
      Value
    _ -> [Char] -> Value -> Parser a
    forall a. [Char] -> Value -> Parser a
    typeMismatch [Char]
    "Null, Object, Number, or String" Value
    v

    dependencyVersion :: Value -> Parser DependencyVersion
    dependencyVersion :: Value -> Parser DependencyVersion
    dependencyVersion = (DependencyVersion -> DependencyVersion)
    -> (Object -> DependencyVersion -> Parser DependencyVersion)
    -> Value
    -> Parser DependencyVersion
    forall a.
    (DependencyVersion -> a)
    -> (Object -> DependencyVersion -> Parser a) -> Value -> Parser a
    withDependencyVersion DependencyVersion -> DependencyVersion
    forall a. a -> a
    id ((DependencyVersion -> Parser DependencyVersion)
    -> Object -> DependencyVersion -> Parser DependencyVersion
    forall a b. a -> b -> a
    const DependencyVersion -> Parser DependencyVersion
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return)

    data SourceDependency = GitRef GitUrl GitRef (Maybe FilePath) | Local FilePath
      deriving (SourceDependency -> SourceDependency -> Bool
    (SourceDependency -> SourceDependency -> Bool)
    -> (SourceDependency -> SourceDependency -> Bool)
    -> Eq SourceDependency
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: SourceDependency -> SourceDependency -> Bool
    == :: SourceDependency -> SourceDependency -> Bool
    $c/= :: SourceDependency -> SourceDependency -> Bool
    /= :: SourceDependency -> SourceDependency -> Bool
    Eq, Eq SourceDependency
    Eq SourceDependency
    -> (SourceDependency -> SourceDependency -> Ordering)
    -> (SourceDependency -> SourceDependency -> Bool)
    -> (SourceDependency -> SourceDependency -> Bool)
    -> (SourceDependency -> SourceDependency -> Bool)
    -> (SourceDependency -> SourceDependency -> Bool)
    -> (SourceDependency -> SourceDependency -> SourceDependency)
    -> (SourceDependency -> SourceDependency -> SourceDependency)
    -> Ord SourceDependency
    SourceDependency -> SourceDependency -> Bool
    SourceDependency -> SourceDependency -> Ordering
    SourceDependency -> SourceDependency -> SourceDependency
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: SourceDependency -> SourceDependency -> Ordering
    compare :: SourceDependency -> SourceDependency -> Ordering
    $c< :: SourceDependency -> SourceDependency -> Bool
    < :: SourceDependency -> SourceDependency -> Bool
    $c<= :: SourceDependency -> SourceDependency -> Bool
    <= :: SourceDependency -> SourceDependency -> Bool
    $c> :: SourceDependency -> SourceDependency -> Bool
    > :: SourceDependency -> SourceDependency -> Bool
    $c>= :: SourceDependency -> SourceDependency -> Bool
    >= :: SourceDependency -> SourceDependency -> Bool
    $cmax :: SourceDependency -> SourceDependency -> SourceDependency
    max :: SourceDependency -> SourceDependency -> SourceDependency
    $cmin :: SourceDependency -> SourceDependency -> SourceDependency
    min :: SourceDependency -> SourceDependency -> SourceDependency
    Ord, Int -> SourceDependency -> ShowS
    [SourceDependency] -> ShowS
    SourceDependency -> [Char]
    (Int -> SourceDependency -> ShowS)
    -> (SourceDependency -> [Char])
    -> ([SourceDependency] -> ShowS)
    -> Show SourceDependency
    forall a.
    (Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> SourceDependency -> ShowS
    showsPrec :: Int -> SourceDependency -> ShowS
    $cshow :: SourceDependency -> [Char]
    show :: SourceDependency -> [Char]
    $cshowList :: [SourceDependency] -> ShowS
    showList :: [SourceDependency] -> ShowS
    Show)

    objectDependency :: Object -> Parser DependencyVersion
    objectDependency :: Object -> Parser DependencyVersion
    objectDependency Object
    o = let
        version :: Parser VersionConstraint
        version :: Parser VersionConstraint
    version = VersionConstraint -> Maybe VersionConstraint -> VersionConstraint
    forall a. a -> Maybe a -> a
    fromMaybe VersionConstraint
    AnyVersion (Maybe VersionConstraint -> VersionConstraint)
    -> Parser (Maybe VersionConstraint) -> Parser VersionConstraint
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Object
    o Object -> Key -> Parser (Maybe VersionConstraint)
    forall a. FromValue a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "version")

        local :: Parser SourceDependency
        local :: Parser SourceDependency
    local = [Char] -> SourceDependency
    Local ([Char] -> SourceDependency)
    -> Parser [Char] -> Parser SourceDependency
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    o Object -> Key -> Parser [Char]
    forall a. FromValue a => Object -> Key -> Parser a
    .: Key
    "path"

        git :: Parser SourceDependency
        git :: Parser SourceDependency
    git = [Char] -> [Char] -> Maybe [Char] -> SourceDependency
    GitRef ([Char] -> [Char] -> Maybe [Char] -> SourceDependency)
    -> Parser [Char]
    -> Parser ([Char] -> Maybe [Char] -> SourceDependency)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Parser [Char]
    url Parser ([Char] -> Maybe [Char] -> SourceDependency)
    -> Parser [Char] -> Parser (Maybe [Char] -> SourceDependency)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Parser [Char]
    ref Parser (Maybe [Char] -> SourceDependency)
    -> Parser (Maybe [Char]) -> Parser SourceDependency
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Parser (Maybe [Char])
    subdir

        url :: Parser String
        url :: Parser [Char]
    url =
              (([Char]
    githubBaseUrl [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++) ShowS -> Parser [Char] -> Parser [Char]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    o Object -> Key -> Parser [Char]
    forall a. FromValue a => Object -> Key -> Parser a
    .: Key
    "github")
          Parser [Char] -> Parser [Char] -> Parser [Char]
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> (Object
    o Object -> Key -> Parser [Char]
    forall a. FromValue a => Object -> Key -> Parser a
    .: Key
    "git")
          Parser [Char] -> Parser [Char] -> Parser [Char]
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> [Char] -> Parser [Char]
    forall a. [Char] -> Parser a
    forall (m :: * -> *) a. MonadFail m => [Char] -> m a
    fail [Char]
    "neither key \"git\" nor key \"github\" present"

        ref :: Parser String
        ref :: Parser [Char]
    ref = Object
    o Object -> Key -> Parser [Char]
    forall a. FromValue a => Object -> Key -> Parser a
    .: Key
    "ref"

        subdir :: Parser (Maybe FilePath)
        subdir :: Parser (Maybe [Char])
    subdir = Object
    o Object -> Key -> Parser (Maybe [Char])
    forall a. FromValue a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "subdir"

        source :: Parser (Maybe SourceDependency)
        source :: Parser (Maybe SourceDependency)
    source
          | (Key -> Bool) -> [Key] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (Key -> Object -> Bool
    forall a. Key -> KeyMap a -> Bool
    `KeyMap.member` Object
    o) [Key
    "path", Key
    "git", Key
    "github", Key
    "ref", Key
    "subdir"] = SourceDependency -> Maybe SourceDependency
    forall a. a -> Maybe a
    Just (SourceDependency -> Maybe SourceDependency)
    -> Parser SourceDependency -> Parser (Maybe SourceDependency)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Parser SourceDependency
    local Parser SourceDependency
    -> Parser SourceDependency -> Parser SourceDependency
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Parser SourceDependency
    git)
          | Bool
    otherwise = Maybe SourceDependency -> Parser (Maybe SourceDependency)
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe SourceDependency
    forall a. Maybe a
    Nothing

        in Maybe SourceDependency -> VersionConstraint -> DependencyVersion
    DependencyVersion (Maybe SourceDependency -> VersionConstraint -> DependencyVersion)
    -> Parser (Maybe SourceDependency)
    -> Parser (VersionConstraint -> DependencyVersion)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Parser (Maybe SourceDependency)
    source Parser (VersionConstraint -> DependencyVersion)
    -> Parser VersionConstraint -> Parser DependencyVersion
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Parser VersionConstraint
    version

    numericVersionConstraint :: Scientific -> VersionConstraint
    numericVersionConstraint :: Scientific -> VersionConstraint
    numericVersionConstraint Scientific
    n = [Char] -> VersionConstraint
    VersionRange ([Char]
    "==" [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    version)
      where
        version :: [Char]
    version = Scientific -> [Char]
    scientificToVersion Scientific
    n

    stringVersionConstraint :: Text -> Parser VersionConstraint
    stringVersionConstraint :: Text -> Parser VersionConstraint
    stringVersionConstraint Text
    s = [Char] -> Parser VersionConstraint
    forall (m :: * -> *). MonadFail m => [Char] -> m VersionConstraint
    parseVersionRange ([Char]
    "== " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    input) Parser VersionConstraint
    -> Parser VersionConstraint -> Parser VersionConstraint
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> [Char] -> Parser VersionConstraint
    forall (m :: * -> *). MonadFail m => [Char] -> m VersionConstraint
    parseVersionRange [Char]
    input
      where
        input :: [Char]
    input = Text -> [Char]
    T.unpack Text
    s

    scientificToVersion :: Scientific -> String
    scientificToVersion :: Scientific -> [Char]
    scientificToVersion Scientific
    n = [Char]
    version
      where
        version :: [Char]
    version = FPFormat -> Maybe Int -> Scientific -> [Char]
    formatScientific FPFormat
    Fixed (Int -> Maybe Int
    forall a. a -> Maybe a
    Just Int
    decimalPlaces) Scientific
    n
        decimalPlaces :: Int
    decimalPlaces
          | Int
    e Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0 = Int -> Int
    forall a. Num a => a -> a
    abs Int
    e
          | Bool
    otherwise = Int
    0
        e :: Int
    e = Scientific -> Int
    base10Exponent Scientific
    n

    parseVersionRange :: Fail.MonadFail m => String -> m VersionConstraint
    parseVersionRange :: forall (m :: * -> *). MonadFail m => [Char] -> m VersionConstraint
    parseVersionRange = (VersionRange -> VersionConstraint)
    -> m VersionRange -> m VersionConstraint
    forall a b. (a -> b) -> m a -> m b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap VersionRange -> VersionConstraint
    versionConstraintFromCabal (m VersionRange -> m VersionConstraint)
    -> ([Char] -> m VersionRange) -> [Char] -> m VersionConstraint
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Char] -> m VersionRange
    forall (m :: * -> *). MonadFail m => [Char] -> m VersionRange
    parseCabalVersionRange

    parseCabalVersionRange :: Fail.MonadFail m => String -> m D.VersionRange
    parseCabalVersionRange :: forall (m :: * -> *). MonadFail m => [Char] -> m VersionRange
    parseCabalVersionRange = [Char] -> [Char] -> m VersionRange
    forall (m :: * -> *) a.
    (MonadFail m, Parsec a) =>
    [Char] -> [Char] -> m a
    cabalParse [Char]
    "constraint"

    cabalParse :: (Fail.MonadFail m, D.Parsec a) => String -> String -> m a
    cabalParse :: forall (m :: * -> *) a.
    (MonadFail m, Parsec a) =>
    [Char] -> [Char] -> m a
    cabalParse [Char]
    subject [Char]
    s = case [Char] -> Either [Char] a
    forall a. Parsec a => [Char] -> Either [Char] a
    D.eitherParsec [Char]
    s of
      Right a
    d -> a -> m a
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return a
    d
      Left [Char]
    _ ->[Char] -> m a
    forall a. [Char] -> m a
    forall (m :: * -> *) a. MonadFail m => [Char] -> m a
    fail ([Char] -> m a) -> [Char] -> m a
    forall a b. (a -> b) -> a -> b
    $ [[Char]] -> [Char]
    unwords [[Char]
    "invalid",  [Char]
    subject, ShowS
    forall a. Show a => a -> [Char]
    show [Char]
    s]

    renderVersionRange :: D.VersionRange -> String
    renderVersionRange :: VersionRange -> [Char]
    renderVersionRange = \ case
      D.IntersectVersionRanges (D.OrLaterVersion Version
    x) (D.EarlierVersion Version
    y) | (Version, Version) -> Bool
    differByOneInLeastPosition (Version
    x, Version
    y) -> [Char]
    "==" [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Version -> [Char]
    forall a. Pretty a => a -> [Char]
    render Version
    x [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    ".*"
      VersionRange
    v -> VersionRange -> [Char]
    forall a. Pretty a => a -> [Char]
    render VersionRange
    v
      where
        differByOneInLeastPosition :: (Version, Version) -> Bool
    differByOneInLeastPosition = \ case
          ([Int] -> [Int]
    forall a. [a] -> [a]
    reverse ([Int] -> [Int]) -> (Version -> [Int]) -> Version -> [Int]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Version -> [Int]
    D.versionNumbers -> Int
    x : [Int]
    xs, [Int] -> [Int]
    forall a. [a] -> [a]
    reverse ([Int] -> [Int]) -> (Version -> [Int]) -> Version -> [Int]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Version -> [Int]
    D.versionNumbers -> Int
    y : [Int]
    ys) -> [Int]
    xs [Int] -> [Int] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Int]
    ys Bool -> Bool -> Bool
    && Int -> Int
    forall a. Enum a => a -> a
    succ Int
    x Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    y
          (Version, Version)
    _ -> Bool
    False

    render :: D.Pretty a => a -> String
    render :: forall a. Pretty a => a -> [Char]
    render = Style -> Doc -> [Char]
    renderStyle (Mode -> Int -> Float -> Style
    Style Mode
    OneLineMode Int
    0 Float
    0) (Doc -> [Char]) -> (a -> Doc) -> a -> [Char]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> Doc
    forall a. Pretty a => a -> Doc
    D.pretty

    versionConstraintFromCabal :: D.VersionRange -> VersionConstraint
    versionConstraintFromCabal :: VersionRange -> VersionConstraint
    versionConstraintFromCabal VersionRange
    range
      | VersionRange -> Bool
    D.isAnyVersion VersionRange
    range = VersionConstraint
    AnyVersion
      | Bool
    otherwise = [Char] -> VersionConstraint
    VersionRange ([Char] -> VersionConstraint)
    -> (VersionRange -> [Char]) -> VersionRange -> VersionConstraint
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . VersionRange -> [Char]
    renderVersionRange (VersionRange -> VersionConstraint)
    -> VersionRange -> VersionConstraint
    forall a b. (a -> b) -> a -> b
    $ VersionRange -> VersionRange
    toPreCabal2VersionRange VersionRange
    range
      where
        toPreCabal2VersionRange :: D.VersionRange -> D.VersionRange
        toPreCabal2VersionRange :: VersionRange -> VersionRange
    toPreCabal2VersionRange = VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange (VersionRangeF VersionRange -> VersionRange)
    -> (VersionRange -> VersionRangeF VersionRange)
    -> VersionRange
    -> VersionRange
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (VersionRangeF (VersionRangeF VersionRange)
     -> VersionRangeF VersionRange)
    -> VersionRange -> VersionRangeF VersionRange
    forall a. (VersionRangeF a -> a) -> VersionRange -> a
    D.cataVersionRange VersionRangeF (VersionRangeF VersionRange)
    -> VersionRangeF VersionRange
    f
          where
            f :: D.VersionRangeF (D.VersionRangeF D.VersionRange) -> D.VersionRangeF D.VersionRange
            f :: VersionRangeF (VersionRangeF VersionRange)
    -> VersionRangeF VersionRange
    f = \ case
              D.MajorBoundVersionF Version
    v -> VersionRange -> VersionRange -> VersionRangeF VersionRange
    forall a. a -> a -> VersionRangeF a
    D.IntersectVersionRangesF (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    forall {a}. VersionRangeF a
    lower) (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    forall {a}. VersionRangeF a
    upper)
                where
                  lower :: VersionRangeF a
    lower = Version -> VersionRangeF a
    forall a. Version -> VersionRangeF a
    D.OrLaterVersionF Version
    v
                  upper :: VersionRangeF a
    upper = Version -> VersionRangeF a
    forall a. Version -> VersionRangeF a
    D.EarlierVersionF (Version -> Version
    D.majorUpperBound Version
    v)

              D.ThisVersionF Version
    v -> Version -> VersionRangeF VersionRange
    forall a. Version -> VersionRangeF a
    D.ThisVersionF Version
    v
              D.LaterVersionF Version
    v -> Version -> VersionRangeF VersionRange
    forall a. Version -> VersionRangeF a
    D.LaterVersionF Version
    v
              D.OrLaterVersionF Version
    v -> Version -> VersionRangeF VersionRange
    forall a. Version -> VersionRangeF a
    D.OrLaterVersionF Version
    v
              D.EarlierVersionF Version
    v -> Version -> VersionRangeF VersionRange
    forall a. Version -> VersionRangeF a
    D.EarlierVersionF Version
    v
              D.OrEarlierVersionF Version
    v -> Version -> VersionRangeF VersionRange
    forall a. Version -> VersionRangeF a
    D.OrEarlierVersionF Version
    v
              D.UnionVersionRangesF VersionRangeF VersionRange
    a VersionRangeF VersionRange
    b -> VersionRange -> VersionRange -> VersionRangeF VersionRange
    forall a. a -> a -> VersionRangeF a
    D.UnionVersionRangesF (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    a) (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    b)
              D.IntersectVersionRangesF VersionRangeF VersionRange
    a VersionRangeF VersionRange
    b -> VersionRange -> VersionRange -> VersionRangeF VersionRange
    forall a. a -> a -> VersionRangeF a
    D.IntersectVersionRangesF (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    a) (VersionRangeF VersionRange -> VersionRange
    D.embedVersionRange VersionRangeF VersionRange
    b)
    #if !MIN_VERSION_Cabal(3,4,0)
              D.WildcardVersionF v -> D.WildcardVersionF v
              D.VersionRangeParensF a -> D.VersionRangeParensF (D.embedVersionRange a)
              D.AnyVersionF -> D.AnyVersionF
    #endif