enum-text

A text rendering and parsing toolkit for enumerated types

https://github.com/cdornan/enum-text#readme

Version on this page:0.5.1.0
LTS Haskell 22.39:0.5.3.0
Stackage Nightly 2024-10-31:0.5.3.0
Latest on Hackage:0.5.3.0

See all snapshots enum-text appears in

BSD-3-Clause licensed by Chris Dornan
Maintained by [email protected]
This version can be pinned in stack with:enum-text-0.5.1.0@sha256:95057099886f125e84c29c293b80182ad1cc527d9fd2bd3b85116d2d140aa906,1270

Module documentation for 0.5.1.0

enum-text

A simple toolkit for rendering enumerated types into Text Builder (used by the fmt package) and parsing them back again into Text with the provided TextParsable type class.

To get the Buildable and TextParsable instances for an enumerated data type the following pattern can be used without any language extensions:

import Fmt
import Text.Enum.Text

data Foo = FOO_bar | FOO_bar_baz
  deriving (Bounded,Enum,Eq,Ord,Show)

instance EnumText     Foo
instance Buildable    Foo where build     = buildEnumText
instance TextParsable Foo where parseText = parseEnumText

With the DeriveAnyClass language extension you can list EnumText in the deriving clause, and with DerivingVia (available from GHC 8.6.1) you can derive via UsingEnumText as follows:

{-# LANGUAGE DeriveAnyClass    #-}
{-# LANGUAGE DerivingVia       #-}

import Fmt
import Text.Enum.Text

data Foo = FOO_bar | FOO_bar_baz
  deriving (Bounded,Enum,EnumText,Eq,Ord,Show)
  deriving (Buildable,TextParsable) via UsingEnumText Foo

This will use the default configuration for generating the text of each enumeration from the derived show text, namely:

  • removing the prefix upto and including the first underscore (_);
  • converting each subsequent underscore (_) into a dash (-).

See the Haddocks for details on how to override this default configuration for any given enumeration type.

Functions for rendering text, generating and parsing UTF-8 encoded ByteStrings (suitable for cassava) and Hashable functions are also provided EnumText.

Changes

0.5.1.0

  • add @UsingEnumText@ wrapper type and instances for deriving @Buildable@ and @TextParsable@ instances

0.5.0.0

  • add basic TextParsable instances and toolkit functions

0.1.0.0

  • first release