packages feed

morley-1.16.3: src/Morley/Util/Label.hs

-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Definition of the Label type and utilities
module Morley.Util.Label
  ( -- * Definitions
    Label (..)

  -- * Utilities
  , labelToText

  -- * Re-exports
  , IsLabel (..)
  ) where

import Fmt (Buildable(..), pretty)
import Text.Show (Show(..))

import Morley.Util.TypeLits

--------------------------------------------------------------------------------
-- Definitions
--------------------------------------------------------------------------------

-- | Proxy for a label type that includes the 'KnownSymbol' constraint
data Label (name :: Symbol) where
  Label :: KnownSymbol name => Label name

deriving stock instance Eq (Label name)

instance Show (Label name) where
  show label = "Label " <> pretty label

instance (KnownSymbol name, s ~ name) => IsLabel s (Label name) where
  fromLabel = Label

instance Buildable (Label name) where
  build Label = build $ symbolVal (Proxy @name)

--------------------------------------------------------------------------------
-- Utilities
--------------------------------------------------------------------------------

-- | Utility function to get the t'Text' representation of a 'Label'
labelToText :: Label name -> Text
labelToText = pretty