{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Distribution.Types.AbiHash
  ( AbiHash, unAbiHash, mkAbiHash
  ) where

import Prelude ()
import Distribution.Compat.Prelude
import Distribution.Utils.ShortText

import qualified Distribution.Compat.CharParsing as P
import Distribution.Pretty
import Distribution.Parsec

import Text.PrettyPrint (text)

-- | ABI Hashes
--
-- Use 'mkAbiHash' and 'unAbiHash' to convert from/to a
-- 'String'.
--
-- This type is opaque since @Cabal-2.0@
--
-- @since 2.0.0.2
newtype AbiHash = AbiHash ShortText
    deriving (AbiHash -> AbiHash -> Bool
(AbiHash -> AbiHash -> Bool)
-> (AbiHash -> AbiHash -> Bool) -> Eq AbiHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AbiHash -> AbiHash -> Bool
$c/= :: AbiHash -> AbiHash -> Bool
== :: AbiHash -> AbiHash -> Bool
$c== :: AbiHash -> AbiHash -> Bool
Eq, Int -> AbiHash -> ShowS
[AbiHash] -> ShowS
AbiHash -> String
(Int -> AbiHash -> ShowS)
-> (AbiHash -> String) -> ([AbiHash] -> ShowS) -> Show AbiHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AbiHash] -> ShowS
$cshowList :: [AbiHash] -> ShowS
show :: AbiHash -> String
$cshow :: AbiHash -> String
showsPrec :: Int -> AbiHash -> ShowS
$cshowsPrec :: Int -> AbiHash -> ShowS
Show, ReadPrec [AbiHash]
ReadPrec AbiHash
Int -> ReadS AbiHash
ReadS [AbiHash]
(Int -> ReadS AbiHash)
-> ReadS [AbiHash]
-> ReadPrec AbiHash
-> ReadPrec [AbiHash]
-> Read AbiHash
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AbiHash]
$creadListPrec :: ReadPrec [AbiHash]
readPrec :: ReadPrec AbiHash
$creadPrec :: ReadPrec AbiHash
readList :: ReadS [AbiHash]
$creadList :: ReadS [AbiHash]
readsPrec :: Int -> ReadS AbiHash
$creadsPrec :: Int -> ReadS AbiHash
Read, (forall x. AbiHash -> Rep AbiHash x)
-> (forall x. Rep AbiHash x -> AbiHash) -> Generic AbiHash
forall x. Rep AbiHash x -> AbiHash
forall x. AbiHash -> Rep AbiHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AbiHash x -> AbiHash
$cfrom :: forall x. AbiHash -> Rep AbiHash x
Generic)

-- | Construct a 'AbiHash' from a 'String'
--
-- 'mkAbiHash' is the inverse to 'unAbiHash'
--
-- Note: No validations are performed to ensure that the resulting
-- 'AbiHash' is valid
--
-- @since 2.0.0.2
unAbiHash :: AbiHash -> String
unAbiHash :: AbiHash -> String
unAbiHash (AbiHash ShortText
h) = ShortText -> String
fromShortText ShortText
h

-- | Convert 'AbiHash' to 'String'
--
-- @since 2.0.0.2
mkAbiHash :: String -> AbiHash
mkAbiHash :: String -> AbiHash
mkAbiHash = ShortText -> AbiHash
AbiHash (ShortText -> AbiHash)
-> (String -> ShortText) -> String -> AbiHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShortText
toShortText

-- | 'mkAbiHash'
--
-- @since 2.0.0.2
instance IsString AbiHash where
    fromString :: String -> AbiHash
fromString = String -> AbiHash
mkAbiHash

instance Binary AbiHash

instance NFData AbiHash where rnf :: AbiHash -> ()
rnf = AbiHash -> ()
forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf

instance Pretty AbiHash where
    pretty :: AbiHash -> Doc
pretty = String -> Doc
text (String -> Doc) -> (AbiHash -> String) -> AbiHash -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AbiHash -> String
unAbiHash

instance Parsec AbiHash where
    parsec :: m AbiHash
parsec = (String -> AbiHash) -> m String -> m AbiHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> AbiHash
mkAbiHash ((Char -> Bool) -> m String
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m String
P.munch Char -> Bool
isAlphaNum)