content: {-# LANGUAGE DataKinds #-}
  {-# LANGUAGE DeriveGeneric #-}
  {-# LANGUAGE TypeApplications #-}
  module Cardano.Wallet.Primitive.Types.StakePoolMetadata
      ( StakePoolMetadataHash (..)
      , StakePoolMetadataUrl (..)
      )
  where
  
  import Prelude
  
  import Cardano.Wallet.Primitive.Types.Hash
      ( Hash (Hash, getHash)
      , hashFromText
      )
  import Control.DeepSeq
      ( NFData
      )
  import Data.ByteString
      ( ByteString
      )
  import Data.Text
      ( Text
      )
  import Data.Text.Class
      ( FromText (..)
      , ToText (..)
      )
  import Fmt
      ( Buildable (..)
      )
  import GHC.Generics
      ( Generic
      )
  
  -- | A newtype to wrap metadata hash.
  --
  -- NOTE: not using the 'Hash' type as this newtype is primarily for database
  -- interop which doesn't quite like DataKinds.
  newtype StakePoolMetadataHash = StakePoolMetadataHash ByteString
      deriving (Eq, Ord, Show, Generic)
  
  instance NFData StakePoolMetadataHash
  
  instance ToText StakePoolMetadataHash where
      toText (StakePoolMetadataHash bytes) = toText (Hash bytes)
  
  instance FromText StakePoolMetadataHash where
      fromText = fmap (StakePoolMetadataHash . getHash @"_") . hashFromText 32
  
  instance Buildable StakePoolMetadataHash where
      build (StakePoolMetadataHash hash) = build (Hash hash)
  
  -- | A newtype to wrap metadata Url, mostly needed for database lookups and
  -- signature clarity.
  newtype StakePoolMetadataUrl = StakePoolMetadataUrl Text
      deriving (Eq, Ord, Show, Generic)
  
  instance NFData StakePoolMetadataUrl
  
  instance ToText StakePoolMetadataUrl where
      toText (StakePoolMetadataUrl url) = url
  
  instance FromText StakePoolMetadataUrl where
      fromText = pure . StakePoolMetadataUrl
  