summaryrefslogtreecommitdiff
path: root/src/Data/Representable.hs
blob: 81091795815daead9453c213d60f5a390e8ad03a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{-# LANGUAGE OverloadedStrings #-}

module Data.Representable where

import           Control.Applicative
import           Data.Text

-- | A class for small objects that can be printed and read back from Text.
--
-- > fromTextRepr . toTextRepr == pure
--
class Representable a where
  toTextRepr :: a -> Text
  fromTextRepr :: (Alternative f) => Text -> f a

instance (Representable a) => Representable (Maybe a) where
  toTextRepr Nothing  = ""
  toTextRepr (Just x) = toTextRepr x
  fromTextRepr "" = pure Nothing
  fromTextRepr t  = Just <$> fromTextRepr t

instance Representable Text where
  toTextRepr = id
  fromTextRepr = pure