{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Headroom.Types.EnumExtra where
import RIO
import qualified RIO.List as L
import qualified RIO.Text as T
class (Bounded a, Enum a, Eq a, Ord a, Show a) => a where
allValues :: [a]
allValues = [a
forall a. Bounded a => a
minBound ..]
allValuesToText :: Text
allValuesToText = Text -> [Text] -> Text
T.intercalate ", " ((a -> Text) -> [a] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Text
forall a. EnumExtra a => a -> Text
enumToText ([a]
forall a. EnumExtra a => [a]
allValues :: [a]))
enumToText :: a -> Text
enumToText = String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
textToEnum :: Text -> Maybe a
textToEnum text :: Text
text =
let enumValue :: a -> Bool
enumValue v :: a
v = (Text -> Text
T.toLower (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. EnumExtra a => a -> Text
enumToText (a -> Text) -> a -> Text
forall a b. (a -> b) -> a -> b
$ a
v) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Text
T.toLower Text
text
in (a -> Bool) -> [a] -> Maybe a
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find a -> Bool
forall a. EnumExtra a => a -> Bool
enumValue [a]
forall a. EnumExtra a => [a]
allValues