enum-text: A text rendering and parsing toolkit for enumerated types

[ bsd3, library, text ] [ Propose Tags ]

A text rendering and parsing toolkit for enumerated types. Please see the README on GitHub at https://github.com/cdornan/enum-text#readme


[Skip to Readme]

Modules

[Last Documentation]

  • Text
    • Enum
      • Text.Enum.Text

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.5.0.0, 0.5.1.0, 0.5.2.0, 0.5.2.1, 0.5.3.0
Change log ChangeLog.md
Dependencies array, base (>=4.9 && <10), bytestring, fmt, hashable, possibly, text, time, unordered-containers [details]
License BSD-3-Clause
Copyright 2019 Chris Dornan
Author Chris Dornan
Maintainer chris@chrisdornan.com
Category Text
Home page https://github.com/cdornan/enum-text#readme
Bug tracker https://github.com/cdornan/enum-text/issues
Source repo head: git clone https://github.com/cdornan/enum-text
Uploaded by ChrisDornan at 2020-03-31T16:17:51Z
Distributions LTSHaskell:0.5.3.0, Stackage:0.5.3.0
Reverse Dependencies 4 direct, 1 indirect [details]
Downloads 2158 total (30 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2020-03-31 [all 2 reports]

Readme for enum-text-0.5.2.0

[back to package description]

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.