{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
module Data.Text.Lens
( IsText(..)
, unpacked
, _Text
, pattern Text
) where
import Control.Lens.Type
import Control.Lens.Getter
import Control.Lens.Review
import Control.Lens.Iso
import Control.Lens.Traversal
import qualified Data.Text as Strict
import qualified Data.Text.Strict.Lens as Strict
import qualified Data.Text.Lazy as Lazy
import qualified Data.Text.Lazy.Lens as Lazy
import Data.Text.Lazy.Builder (Builder)
class IsText t where
packed :: Iso' String t
builder :: Iso' t Builder
text :: IndexedTraversal' Int t Char
text = unpacked . traversed
{-# INLINE text #-}
instance IsText String where
packed = id
{-# INLINE packed #-}
text = traversed
{-# INLINE text #-}
builder = Lazy.packed . builder
{-# INLINE builder #-}
unpacked :: IsText t => Iso' t String
unpacked = from packed
{-# INLINE unpacked #-}
_Text :: IsText t => Iso' t String
_Text = from packed
{-# INLINE _Text #-}
pattern Text :: IsText s => String -> s
pattern Text a <- (view _Text -> a) where
Text a = review _Text a
instance IsText Strict.Text where
packed = Strict.packed
{-# INLINE packed #-}
builder = Strict.builder
{-# INLINE builder #-}
text = Strict.text
{-# INLINE text #-}
instance IsText Lazy.Text where
packed = Lazy.packed
{-# INLINE packed #-}
builder = Lazy.builder
{-# INLINE builder #-}
text = Lazy.text
{-# INLINE text #-}