{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Safe #-}


{- |
Module      :  Lens.Micro.Platform.Internal
Copyright   :  (C) 2013-2016 Edward Kmett, 2015-2016 Artyom Kazak, 2018 Monadfix
License     :  BSD-style (see the file LICENSE)
-}
module Lens.Micro.Platform.Internal
(
  IsText(..),
)
where


import Lens.Micro

import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif


class IsText t where
  {- |
'packed' lets you convert between 'String' and @Text@ (strict or lazy). It can be used as a replacement for @pack@ or as a way to modify some 'String' if you have a function like @Text -> Text@.
  -}
  packed :: Lens' String t

  {- |
'unpacked' is like 'packed' but works in the opposite direction.
  -}
  unpacked :: Lens' t String

instance IsText String where
  packed :: Lens' String String
packed = forall a. a -> a
id
  {-# INLINE packed #-}
  unpacked :: Lens' String String
unpacked = forall a. a -> a
id
  {-# INLINE unpacked #-}

instance IsText T.Text where
  packed :: Lens' String Text
packed Text -> f Text
f String
s = Text -> String
T.unpack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f Text
f (String -> Text
T.pack String
s)
  {-# INLINE packed #-}
  unpacked :: Lens' Text String
unpacked String -> f String
f Text
s = String -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> f String
f (Text -> String
T.unpack Text
s)
  {-# INLINE unpacked #-}

instance IsText TL.Text where
  packed :: Lens' String Text
packed Text -> f Text
f String
s = Text -> String
TL.unpack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f Text
f (String -> Text
TL.pack String
s)
  {-# INLINE packed #-}
  unpacked :: Lens' Text String
unpacked String -> f String
f Text
s = String -> Text
TL.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> f String
f (Text -> String
TL.unpack Text
s)
  {-# INLINE unpacked #-}