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

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

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

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

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
$c== :: AbiHash -> AbiHash -> Bool
== :: AbiHash -> AbiHash -> Bool
$c/= :: AbiHash -> AbiHash -> Bool
/= :: 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
$cshowsPrec :: Int -> AbiHash -> ShowS
showsPrec :: Int -> AbiHash -> ShowS
$cshow :: AbiHash -> String
show :: AbiHash -> String
$cshowList :: [AbiHash] -> ShowS
showList :: [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
$creadsPrec :: Int -> ReadS AbiHash
readsPrec :: Int -> ReadS AbiHash
$creadList :: ReadS [AbiHash]
readList :: ReadS [AbiHash]
$creadPrec :: ReadPrec AbiHash
readPrec :: ReadPrec AbiHash
$creadListPrec :: ReadPrec [AbiHash]
readListPrec :: ReadPrec [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
$cfrom :: forall x. AbiHash -> Rep AbiHash x
from :: forall x. AbiHash -> Rep AbiHash x
$cto :: forall x. Rep AbiHash x -> AbiHash
to :: forall x. Rep AbiHash x -> AbiHash
Generic, Typeable)

-- | Convert 'AbiHash' to 'String'
--
-- @since 2.0.0.2
unAbiHash :: AbiHash -> String
unAbiHash :: AbiHash -> String
unAbiHash (AbiHash ShortText
h) = ShortText -> String
fromShortText ShortText
h

-- | 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
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 Structured 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 :: forall (m :: * -> *). CabalParsing m => m AbiHash
parsec = (String -> AbiHash) -> m String -> m AbiHash
forall a b. (a -> b) -> m a -> m b
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)