{-# LANGUAGE ScopedTypeVariables #-}
module Zinza.Class (
    Zinza (..),
    ) where

import Control.Exception (throw)
import Data.Foldable     (toList)
import Data.Proxy        (Proxy (..))

import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Lazy      as Map
import qualified Data.Set           as Set
import qualified Data.Text          as T
import qualified Data.Text.Lazy     as LT

import Zinza.Errors
import Zinza.Pos
import Zinza.Type
import Zinza.Value
--
-- $setup
-- >>> :set -XDeriveGeneric
-- >>> import Data.Proxy (Proxy (..))
-- >>> import GHC.Generics (Generic)
-- >>> import Zinza

-- | 'Zinza' class tells how to convert the type into template parameters,
-- and their types.
--
-- Class can be auto-derived for product types.
--
-- >>> data R = R { recFoo :: String, recBar :: Char } deriving Generic
-- >>> instance Zinza R where toType = genericToTypeSFP; toValue = genericToValueSFP; fromValue = genericFromValueSFP
-- >>> displayTy $ toType (Proxy :: Proxy R)
-- "{bar: String, foo: String}"
--
class Zinza a where
    toType     :: Proxy a -> Ty
    toTypeList :: Proxy a -> Ty
    toTypeList = Maybe Selector -> Ty -> Ty
TyList Maybe Selector
forall a. Maybe a
Nothing (Ty -> Ty) -> (Proxy a -> Ty) -> Proxy a -> Ty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toType

    toValue     :: a   -> Value
    toValueList :: [a] -> Value
    toValueList = [Value] -> Value
VList ([Value] -> Value) -> ([a] -> [Value]) -> [a] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Value) -> [a] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map a -> Value
forall a. Zinza a => a -> Value
toValue

    fromValue     :: Loc -> Value -> Either RuntimeError a
    fromValueList :: Loc -> Value -> Either RuntimeError [a]
    fromValueList Loc
l (VList [Value]
xs) = (Value -> Either RuntimeError a)
-> [Value] -> Either RuntimeError [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Loc -> Value -> Either RuntimeError a
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l) [Value]
xs
    fromValueList Loc
l Value
v          = RuntimeError -> Either RuntimeError [a]
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError [a])
-> RuntimeError -> Either RuntimeError [a]
forall a b. (a -> b) -> a -> b
$ Loc -> Ty -> RuntimeError
NotList Loc
l (Value -> Ty
valueType Value
v)

instance Zinza () where
    toType :: Proxy () -> Ty
toType Proxy ()
_    = Ty
tyUnit
    toValue :: () -> Value
toValue ()
_   = Map Selector Value -> Value
VRecord Map Selector Value
forall a. Monoid a => a
mempty

    -- we can be strict, but it's easy to just eat some errors.
    fromValue :: Loc -> Value -> Either RuntimeError ()
fromValue Loc
_ Value
_ = () -> Either RuntimeError ()
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

instance Zinza Bool where
    toType :: Proxy Bool -> Ty
toType Proxy Bool
_ = Ty
TyBool
    toValue :: Bool -> Value
toValue = Bool -> Value
VBool

    fromValue :: Loc -> Value -> Either RuntimeError Bool
fromValue Loc
_ (VBool Bool
b) = Bool -> Either RuntimeError Bool
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
b
    fromValue Loc
l Value
v         = RuntimeError -> Either RuntimeError Bool
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (Loc -> Ty -> RuntimeError
NotBool Loc
l (Value -> Ty
valueType Value
v))

instance Zinza Char where
    toType :: Proxy Char -> Ty
toType     Proxy Char
_ = Maybe Selector -> Ty
TyString (Selector -> Maybe Selector
forall a. a -> Maybe a
Just Selector
"return")
    toTypeList :: Proxy Char -> Ty
toTypeList Proxy Char
_ = Maybe Selector -> Ty
TyString Maybe Selector
forall a. Maybe a
Nothing

    toValue :: Char -> Value
toValue     = Selector -> Value
VString (Selector -> Value) -> (Char -> Selector) -> Char -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Selector
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return
    toValueList :: Selector -> Value
toValueList = Selector -> Value
VString

    fromValue :: Loc -> Value -> Either RuntimeError Char
fromValue Loc
_ (VString [Char
c]) = Char -> Either RuntimeError Char
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c
    fromValue Loc
l Value
v             = RuntimeError -> Either RuntimeError Char
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError Char)
-> RuntimeError -> Either RuntimeError Char
forall a b. (a -> b) -> a -> b
$ Loc -> Selector -> Ty -> RuntimeError
CustomError Loc
l Selector
"Not Char" (Value -> Ty
valueType Value
v)

    fromValueList :: Loc -> Value -> Either RuntimeError Selector
fromValueList Loc
_ (VString Selector
s) = Selector -> Either RuntimeError Selector
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return Selector
s
    fromValueList Loc
l Value
v           = RuntimeError -> Either RuntimeError Selector
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (Loc -> Ty -> RuntimeError
NotString Loc
l (Value -> Ty
valueType Value
v))

instance Zinza a => Zinza [a] where
    toType :: Proxy [a] -> Ty
toType Proxy [a]
_  = Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toTypeList (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
    toValue :: [a] -> Value
toValue   = [a] -> Value
forall a. Zinza a => [a] -> Value
toValueList
    fromValue :: Loc -> Value -> Either RuntimeError [a]
fromValue = Loc -> Value -> Either RuntimeError [a]
forall a. Zinza a => Loc -> Value -> Either RuntimeError [a]
fromValueList

instance (Zinza a, Zinza b) => Zinza (a, b) where
    toType :: Proxy (a, b) -> Ty
toType Proxy (a, b)
_ = Map Selector (Selector, Ty) -> Ty
TyRecord (Map Selector (Selector, Ty) -> Ty)
-> Map Selector (Selector, Ty) -> Ty
forall a b. (a -> b) -> a -> b
$ [(Selector, (Selector, Ty))] -> Map Selector (Selector, Ty)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
        [ (Selector
"fst", (Selector
"fst", Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)))
        , (Selector
"snd", (Selector
"snd", Proxy b -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy b
forall {k} (t :: k). Proxy t
Proxy :: Proxy b)))
        ]

    toValue :: (a, b) -> Value
toValue (a
a, b
b) = Map Selector Value -> Value
VRecord (Map Selector Value -> Value) -> Map Selector Value -> Value
forall a b. (a -> b) -> a -> b
$ [(Selector, Value)] -> Map Selector Value
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
        [ (Selector
"fst", a -> Value
forall a. Zinza a => a -> Value
toValue a
a)
        , (Selector
"snd", b -> Value
forall a. Zinza a => a -> Value
toValue b
b)
        ]

    fromValue :: Loc -> Value -> Either RuntimeError (a, b)
fromValue Loc
l (VRecord Map Selector Value
m)
        | [(Selector
"fst", Value
x), (Selector
"snd", Value
y)] <- Map Selector Value -> [(Selector, Value)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Selector Value
m
        = (,) (a -> b -> (a, b))
-> Either RuntimeError a -> Either RuntimeError (b -> (a, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Loc -> Value -> Either RuntimeError a
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l Value
x Either RuntimeError (b -> (a, b))
-> Either RuntimeError b -> Either RuntimeError (a, b)
forall a b.
Either RuntimeError (a -> b)
-> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Loc -> Value -> Either RuntimeError b
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l Value
y
    fromValue Loc
l Value
v = RuntimeError -> Either RuntimeError (a, b)
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError (a, b))
-> RuntimeError -> Either RuntimeError (a, b)
forall a b. (a -> b) -> a -> b
$ Loc -> Selector -> Ty -> RuntimeError
CustomError Loc
l Selector
"Not pair" (Value -> Ty
valueType Value
v)

-------------------------------------------------------------------------------
-- Functions
-------------------------------------------------------------------------------

-- | The 'fromValue' for function produces partial functions.
-- Use with care.
--
-- This means that higher order functions in templates might throw
-- pure exception. They wont, if they are well-typed.
--
instance (Zinza a, Zinza b) => Zinza (a -> b) where
    toType :: Proxy (a -> b) -> Ty
toType Proxy (a -> b)
_ = Ty -> Ty -> Ty
TyFun (Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a)) (Proxy b -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy b
forall {k} (t :: k). Proxy t
Proxy :: Proxy b))
    toValue :: (a -> b) -> Value
toValue a -> b
f = (Value -> Either RuntimeError Value) -> Value
VFun ((Value -> Either RuntimeError Value) -> Value)
-> (Value -> Either RuntimeError Value) -> Value
forall a b. (a -> b) -> a -> b
$ (a -> Value) -> Either RuntimeError a -> Either RuntimeError Value
forall a b.
(a -> b) -> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (b -> Value
forall a. Zinza a => a -> Value
toValue (b -> Value) -> (a -> b) -> a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) (Either RuntimeError a -> Either RuntimeError Value)
-> (Value -> Either RuntimeError a)
-> Value
-> Either RuntimeError Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Value -> Either RuntimeError a
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
zeroLoc
    fromValue :: Loc -> Value -> Either RuntimeError (a -> b)
fromValue Loc
l (VFun Value -> Either RuntimeError Value
f) = (a -> b) -> Either RuntimeError (a -> b)
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a -> b) -> Either RuntimeError (a -> b))
-> (a -> b) -> Either RuntimeError (a -> b)
forall a b. (a -> b) -> a -> b
$
        (RuntimeError -> b) -> (b -> b) -> Either RuntimeError b -> b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either RuntimeError -> b
forall a e. Exception e => e -> a
throw b -> b
forall a. a -> a
id (Either RuntimeError b -> b)
-> (a -> Either RuntimeError b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either RuntimeError Value
-> (Value -> Either RuntimeError b) -> Either RuntimeError b
forall a b.
Either RuntimeError a
-> (a -> Either RuntimeError b) -> Either RuntimeError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Loc -> Value -> Either RuntimeError b
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l) (Either RuntimeError Value -> Either RuntimeError b)
-> (a -> Either RuntimeError Value) -> a -> Either RuntimeError b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Either RuntimeError Value
f (Value -> Either RuntimeError Value)
-> (a -> Value) -> a -> Either RuntimeError Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
forall a. Zinza a => a -> Value
toValue
    fromValue Loc
l Value
v = RuntimeError -> Either RuntimeError (a -> b)
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError (a -> b))
-> RuntimeError -> Either RuntimeError (a -> b)
forall a b. (a -> b) -> a -> b
$ Loc -> Ty -> RuntimeError
NotFunction Loc
l (Value -> Ty
valueType Value
v)

-------------------------------------------------------------------------------
-- semigroups
-------------------------------------------------------------------------------

instance Zinza a => Zinza (NE.NonEmpty a) where
    toType :: Proxy (NonEmpty a) -> Ty
toType Proxy (NonEmpty a)
_ = Maybe Selector -> Ty -> Ty
TyList Maybe Selector
forall a. Maybe a
Nothing (Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a))
    toValue :: NonEmpty a -> Value
toValue  = [Value] -> Value
VList ([Value] -> Value)
-> (NonEmpty a -> [Value]) -> NonEmpty a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Value) -> [a] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map a -> Value
forall a. Zinza a => a -> Value
toValue ([a] -> [Value]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [Value]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
    fromValue :: Loc -> Value -> Either RuntimeError (NonEmpty a)
fromValue Loc
l Value
v = do
        [a]
xs <- Loc -> Value -> Either RuntimeError [a]
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l Value
v
        case [a]
xs of
            []     -> RuntimeError -> Either RuntimeError (NonEmpty a)
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError (NonEmpty a))
-> RuntimeError -> Either RuntimeError (NonEmpty a)
forall a b. (a -> b) -> a -> b
$ Loc -> Selector -> Ty -> RuntimeError
CustomError Loc
l Selector
"Not NonEmpty" (Value -> Ty
valueType Value
v)
            (a
y:[a]
ys) -> NonEmpty a -> Either RuntimeError (NonEmpty a)
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
y a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
NE.:| [a]
ys)

-------------------------------------------------------------------------------
-- containers
-------------------------------------------------------------------------------

instance (Zinza a, Ord a) => Zinza (Set.Set a) where
    toType :: Proxy (Set a) -> Ty
toType Proxy (Set a)
_    = Maybe Selector -> Ty -> Ty
TyList Maybe Selector
forall a. Maybe a
Nothing (Proxy a -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a))
    toValue :: Set a -> Value
toValue     = [Value] -> Value
VList ([Value] -> Value) -> (Set a -> [Value]) -> Set a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Value) -> [a] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map a -> Value
forall a. Zinza a => a -> Value
toValue ([a] -> [Value]) -> (Set a -> [a]) -> Set a -> [Value]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [a]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
    fromValue :: Loc -> Value -> Either RuntimeError (Set a)
fromValue Loc
l = ([a] -> Set a)
-> Either RuntimeError [a] -> Either RuntimeError (Set a)
forall a b.
(a -> b) -> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList (Either RuntimeError [a] -> Either RuntimeError (Set a))
-> (Value -> Either RuntimeError [a])
-> Value
-> Either RuntimeError (Set a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Value -> Either RuntimeError [a]
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l

-- | Pairs are encoded as @{ key: k, val: v }@
instance (Zinza k, Zinza v, Ord k) => Zinza (Map.Map k v) where
    toType :: Proxy (Map k v) -> Ty
toType Proxy (Map k v)
_ = Maybe Selector -> Ty -> Ty
TyList (Selector -> Maybe Selector
forall a. a -> Maybe a
Just Selector
"Map.toList") (Ty -> Ty) -> Ty -> Ty
forall a b. (a -> b) -> a -> b
$ Map Selector (Selector, Ty) -> Ty
TyRecord (Map Selector (Selector, Ty) -> Ty)
-> Map Selector (Selector, Ty) -> Ty
forall a b. (a -> b) -> a -> b
$ [(Selector, (Selector, Ty))] -> Map Selector (Selector, Ty)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
        [ (Selector
"key", (Selector
"fst", Proxy k -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy k
forall {k} (t :: k). Proxy t
Proxy :: Proxy k)))
        , (Selector
"val", (Selector
"snd", Proxy v -> Ty
forall a. Zinza a => Proxy a -> Ty
toType (Proxy v
forall {k} (t :: k). Proxy t
Proxy :: Proxy v)))
        ]

    toValue :: Map k v -> Value
toValue Map k v
m = [Value] -> Value
VList
        [ Map Selector Value -> Value
VRecord (Map Selector Value -> Value) -> Map Selector Value -> Value
forall a b. (a -> b) -> a -> b
$ [(Selector, Value)] -> Map Selector Value
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
            [ (Selector
"key", k -> Value
forall a. Zinza a => a -> Value
toValue k
k)
            , (Selector
"val", v -> Value
forall a. Zinza a => a -> Value
toValue v
v)
            ]
        | (k
k, v
v) <- Map k v -> [(k, v)]
forall k a. Map k a -> [(k, a)]
Map.toList Map k v
m
        ]

    fromValue :: Loc -> Value -> Either RuntimeError (Map k v)
fromValue Loc
l (VList [Value]
xs) = do
        [(k, v)]
kvs <- (Value -> Either RuntimeError (k, v))
-> [Value] -> Either RuntimeError [(k, v)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Value -> Either RuntimeError (k, v)
forall {a} {a}.
(Zinza a, Zinza a) =>
Value -> Either RuntimeError (a, a)
fromPair [Value]
xs
        Map k v -> Either RuntimeError (Map k v)
forall a. a -> Either RuntimeError a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(k, v)] -> Map k v
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(k, v)]
kvs)
      where
        fromPair :: Value -> Either RuntimeError (a, a)
fromPair (VRecord Map Selector Value
m)
            | [(Selector
"key", Value
x), (Selector
"val", Value
y)] <- Map Selector Value -> [(Selector, Value)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Selector Value
m
            = (,) (a -> a -> (a, a))
-> Either RuntimeError a -> Either RuntimeError (a -> (a, a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Loc -> Value -> Either RuntimeError a
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l Value
x Either RuntimeError (a -> (a, a))
-> Either RuntimeError a -> Either RuntimeError (a, a)
forall a b.
Either RuntimeError (a -> b)
-> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Loc -> Value -> Either RuntimeError a
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l Value
y
        fromPair Value
v = RuntimeError -> Either RuntimeError (a, a)
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError (a, a))
-> RuntimeError -> Either RuntimeError (a, a)
forall a b. (a -> b) -> a -> b
$ Loc -> Selector -> Ty -> RuntimeError
CustomError Loc
l Selector
"Not pair" (Value -> Ty
valueType Value
v)
    fromValue Loc
l Value
v = RuntimeError -> Either RuntimeError (Map k v)
forall a. RuntimeError -> Either RuntimeError a
forall (m :: * -> *) a. ThrowRuntime m => RuntimeError -> m a
throwRuntime (RuntimeError -> Either RuntimeError (Map k v))
-> RuntimeError -> Either RuntimeError (Map k v)
forall a b. (a -> b) -> a -> b
$ Loc -> Ty -> RuntimeError
NotList Loc
l (Value -> Ty
valueType Value
v)

-------------------------------------------------------------------------------
-- text
-------------------------------------------------------------------------------

instance Zinza T.Text where
    toType :: Proxy Text -> Ty
toType Proxy Text
_ = Maybe Selector -> Ty
TyString (Selector -> Maybe Selector
forall a. a -> Maybe a
Just Selector
"T.unpack")
    toValue :: Text -> Value
toValue  = Selector -> Value
VString (Selector -> Value) -> (Text -> Selector) -> Text -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Selector
T.unpack
    fromValue :: Loc -> Value -> Either RuntimeError Text
fromValue Loc
l = (Selector -> Text)
-> Either RuntimeError Selector -> Either RuntimeError Text
forall a b.
(a -> b) -> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Selector -> Text
T.pack (Either RuntimeError Selector -> Either RuntimeError Text)
-> (Value -> Either RuntimeError Selector)
-> Value
-> Either RuntimeError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Value -> Either RuntimeError Selector
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l

instance Zinza LT.Text where
    toType :: Proxy Text -> Ty
toType Proxy Text
_ = Maybe Selector -> Ty
TyString (Selector -> Maybe Selector
forall a. a -> Maybe a
Just Selector
"LT.unpack")
    toValue :: Text -> Value
toValue  = Selector -> Value
VString (Selector -> Value) -> (Text -> Selector) -> Text -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Selector
LT.unpack
    fromValue :: Loc -> Value -> Either RuntimeError Text
fromValue Loc
l = (Selector -> Text)
-> Either RuntimeError Selector -> Either RuntimeError Text
forall a b.
(a -> b) -> Either RuntimeError a -> Either RuntimeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Selector -> Text
LT.pack (Either RuntimeError Selector -> Either RuntimeError Text)
-> (Value -> Either RuntimeError Selector)
-> Value
-> Either RuntimeError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Value -> Either RuntimeError Selector
forall a. Zinza a => Loc -> Value -> Either RuntimeError a
fromValue Loc
l