text-class-2020.12.8: Extra helpers to convert data-types to and from Text
Copyright© 2018-2020 IOHK
LicenseApache-2.0
Safe HaskellNone
LanguageHaskell2010

Data.Text.Class

Description

Extend the Text module with an extra abstraction to encode and decode values safely to and from Text. It's very similar to FromJSON and ToJSON from Aeson.

Synopsis

Producing and consuming text from arbitrary types

class ToText a where Source #

Defines a textual encoding for a type.

Methods

toText :: a -> Text Source #

Encode the specified value as text.

Instances

Instances details
ToText Double Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Double -> Text Source #

ToText Int Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Int -> Text Source #

ToText Integer Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Integer -> Text Source #

ToText Natural Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Natural -> Text Source #

ToText Word32 Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Word32 -> Text Source #

ToText Word64 Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Word64 -> Text Source #

ToText String Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: String -> Text Source #

ToText Word31 Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Word31 -> Text Source #

ToText NominalDiffTime Source # 
Instance details

Defined in Data.Text.Class

ToText Text Source # 
Instance details

Defined in Data.Text.Class

Methods

toText :: Text -> Text Source #

class FromText a where Source #

Defines a textual decoding for a type.

Methods

fromText :: Text -> Either TextDecodingError a Source #

Decode the specified text as a value.

newtype TextDecodingError Source #

Indicates an error that occurred while decoding from text.

fromTextMaybe :: FromText a => Text -> Maybe a Source #

Decode the specified text with a Maybe result type.

Producing and consuming text from bounded enumeration types

data CaseStyle Source #

Represents a case style for multi-word strings.

Constructors

CamelCase

A string in the style of "doNotRepeatYourself"

PascalCase

A string in the style of DoNotRepeatYourself

KebabLowerCase

A string in the style of "do-not-repeat-yourself"

SnakeLowerCase

A string in the style of "do_not_repeat_yourself"

SnakeUpperCase

A string in the style of DO_NOT_REPEAT_YOURSELF

SpacedLowerCase

A string in the style of "do not repeat yourself"

Instances

Instances details
Bounded CaseStyle Source # 
Instance details

Defined in Data.Text.Class

Enum CaseStyle Source # 
Instance details

Defined in Data.Text.Class

Eq CaseStyle Source # 
Instance details

Defined in Data.Text.Class

Show CaseStyle Source # 
Instance details

Defined in Data.Text.Class

Generic CaseStyle Source # 
Instance details

Defined in Data.Text.Class

Associated Types

type Rep CaseStyle :: Type -> Type #

type Rep CaseStyle Source # 
Instance details

Defined in Data.Text.Class

type Rep CaseStyle = D1 ('MetaData "CaseStyle" "Data.Text.Class" "text-class-2020.12.8-2hUsEto18Rd40nDhFNhnF9" 'False) ((C1 ('MetaCons "CamelCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "PascalCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "KebabLowerCase" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "SnakeLowerCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SnakeUpperCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SpacedLowerCase" 'PrefixI 'False) (U1 :: Type -> Type))))

toTextFromBoundedEnum :: forall a. (Bounded a, Enum a, Show a) => CaseStyle -> a -> Text Source #

Converts the given value to text, according to the specified CaseStyle.

This function guarantees to satisfy the following property:

fromTextToBoundedEnum s (toTextFromBoundedEnum s a) == Right a

fromTextToBoundedEnum :: forall a. (Bounded a, Enum a, Show a) => CaseStyle -> Text -> Either TextDecodingError a Source #

Parses the given text to a value, according to the specified CaseStyle.

This function guarantees to satisfy the following property:

fromTextToBoundedEnum s (toTextFromBoundedEnum s a) == Right a

Helpers

showT :: ToText a => a -> String Source #

Show a data-type through its ToText instance

splitAtLastOccurrence :: Char -> Text -> Maybe (Text, Text) Source #

Splits the given Text into a prefix and a suffix using the last occurrence of the specified separator character as a splitting point. Evaluates to Nothing if the specified Text does not contain the separator character.