{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE IncoherentInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-missing-signatures #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

-- | Although there are a lot of instances in this file, really it's just a
--   combinatorial explosion of the following combinations:
--
--   - Several Haskell types being converted to/from Nix wrappers
--   - Several types of Nix wrappers
--   - Whether to be shallow or deep while unwrapping

module Nix.Convert where

import           Control.Monad.Free
import           Data.ByteString
import qualified Data.HashMap.Lazy             as M
import           Data.Maybe
import           Data.Text                      ( Text )
import qualified Data.Text                     as Text
import           Data.Text.Encoding             ( encodeUtf8
                                                , decodeUtf8
                                                )
import           Nix.Atoms
import           Nix.Effects
import           Nix.Expr.Types
import           Nix.Expr.Types.Annotated
import           Nix.Frames
import           Nix.String
import           Nix.Value
import           Nix.Value.Monad
import           Nix.Thunk
import           Nix.Utils

newtype Deeper a = Deeper { Deeper a -> a
getDeeper :: a }
  deriving (Typeable, a -> Deeper b -> Deeper a
(a -> b) -> Deeper a -> Deeper b
(forall a b. (a -> b) -> Deeper a -> Deeper b)
-> (forall a b. a -> Deeper b -> Deeper a) -> Functor Deeper
forall a b. a -> Deeper b -> Deeper a
forall a b. (a -> b) -> Deeper a -> Deeper b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Deeper b -> Deeper a
$c<$ :: forall a b. a -> Deeper b -> Deeper a
fmap :: (a -> b) -> Deeper a -> Deeper b
$cfmap :: forall a b. (a -> b) -> Deeper a -> Deeper b
Functor, Deeper a -> Bool
(a -> m) -> Deeper a -> m
(a -> b -> b) -> b -> Deeper a -> b
(forall m. Monoid m => Deeper m -> m)
-> (forall m a. Monoid m => (a -> m) -> Deeper a -> m)
-> (forall m a. Monoid m => (a -> m) -> Deeper a -> m)
-> (forall a b. (a -> b -> b) -> b -> Deeper a -> b)
-> (forall a b. (a -> b -> b) -> b -> Deeper a -> b)
-> (forall b a. (b -> a -> b) -> b -> Deeper a -> b)
-> (forall b a. (b -> a -> b) -> b -> Deeper a -> b)
-> (forall a. (a -> a -> a) -> Deeper a -> a)
-> (forall a. (a -> a -> a) -> Deeper a -> a)
-> (forall a. Deeper a -> [a])
-> (forall a. Deeper a -> Bool)
-> (forall a. Deeper a -> Int)
-> (forall a. Eq a => a -> Deeper a -> Bool)
-> (forall a. Ord a => Deeper a -> a)
-> (forall a. Ord a => Deeper a -> a)
-> (forall a. Num a => Deeper a -> a)
-> (forall a. Num a => Deeper a -> a)
-> Foldable Deeper
forall a. Eq a => a -> Deeper a -> Bool
forall a. Num a => Deeper a -> a
forall a. Ord a => Deeper a -> a
forall m. Monoid m => Deeper m -> m
forall a. Deeper a -> Bool
forall a. Deeper a -> Int
forall a. Deeper a -> [a]
forall a. (a -> a -> a) -> Deeper a -> a
forall m a. Monoid m => (a -> m) -> Deeper a -> m
forall b a. (b -> a -> b) -> b -> Deeper a -> b
forall a b. (a -> b -> b) -> b -> Deeper a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Deeper a -> a
$cproduct :: forall a. Num a => Deeper a -> a
sum :: Deeper a -> a
$csum :: forall a. Num a => Deeper a -> a
minimum :: Deeper a -> a
$cminimum :: forall a. Ord a => Deeper a -> a
maximum :: Deeper a -> a
$cmaximum :: forall a. Ord a => Deeper a -> a
elem :: a -> Deeper a -> Bool
$celem :: forall a. Eq a => a -> Deeper a -> Bool
length :: Deeper a -> Int
$clength :: forall a. Deeper a -> Int
null :: Deeper a -> Bool
$cnull :: forall a. Deeper a -> Bool
toList :: Deeper a -> [a]
$ctoList :: forall a. Deeper a -> [a]
foldl1 :: (a -> a -> a) -> Deeper a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldr1 :: (a -> a -> a) -> Deeper a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Deeper a -> a
foldl' :: (b -> a -> b) -> b -> Deeper a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldl :: (b -> a -> b) -> b -> Deeper a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Deeper a -> b
foldr' :: (a -> b -> b) -> b -> Deeper a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldr :: (a -> b -> b) -> b -> Deeper a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Deeper a -> b
foldMap' :: (a -> m) -> Deeper a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
foldMap :: (a -> m) -> Deeper a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Deeper a -> m
fold :: Deeper m -> m
$cfold :: forall m. Monoid m => Deeper m -> m
Foldable, Functor Deeper
Foldable Deeper
(Functor Deeper, Foldable Deeper) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Deeper a -> f (Deeper b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Deeper (f a) -> f (Deeper a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Deeper a -> m (Deeper b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Deeper (m a) -> m (Deeper a))
-> Traversable Deeper
(a -> f b) -> Deeper a -> f (Deeper b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
sequence :: Deeper (m a) -> m (Deeper a)
$csequence :: forall (m :: * -> *) a. Monad m => Deeper (m a) -> m (Deeper a)
mapM :: (a -> m b) -> Deeper a -> m (Deeper b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Deeper a -> m (Deeper b)
sequenceA :: Deeper (f a) -> f (Deeper a)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Deeper (f a) -> f (Deeper a)
traverse :: (a -> f b) -> Deeper a -> f (Deeper b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Deeper a -> f (Deeper b)
$cp2Traversable :: Foldable Deeper
$cp1Traversable :: Functor Deeper
Traversable)

{-

IMPORTANT NOTE

We used to have Text instances of FromValue, ToValue, FromNix, and ToNix.
However, we're removing these instances because they are dangerous due to the
fact that they hide the way string contexts are handled. It's better to have to
explicitly handle string context in a way that is appropriate for the situation.

Do not add these instances back!

-}

{-----------------------------------------------------------------------
   FromValue
 -----------------------------------------------------------------------}

class FromValue a m v where
    fromValue    :: v -> m a
    fromValueMay :: v -> m (Maybe a)

type Convertible e t f m
  = (Framed e m, MonadDataErrorContext t f m, MonadThunk t m (NValue t f m))

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , FromValue a m (NValue' t f m (NValue t f m))
         )
  => FromValue a m (NValue t f m) where
  fromValueMay :: NValue t f m -> m (Maybe a)
fromValueMay = (NValue t f m -> (NValue t f m -> m (Maybe a)) -> m (Maybe a))
-> (NValue t f m -> m (Maybe a)) -> NValue t f m -> m (Maybe a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip NValue t f m -> (NValue t f m -> m (Maybe a)) -> m (Maybe a)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand ((NValue t f m -> m (Maybe a)) -> NValue t f m -> m (Maybe a))
-> (NValue t f m -> m (Maybe a)) -> NValue t f m -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ \case
    Pure t :: t
t -> t -> (NValue t f m -> m (Maybe a)) -> m (Maybe a)
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay
    Free v :: NValue' t f m (NValue t f m)
v -> NValue' t f m (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v
  fromValue :: NValue t f m -> m a
fromValue = (NValue t f m -> (NValue t f m -> m a) -> m a)
-> (NValue t f m -> m a) -> NValue t f m -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip NValue t f m -> (NValue t f m -> m a) -> m a
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand ((NValue t f m -> m a) -> NValue t f m -> m a)
-> (NValue t f m -> m a) -> NValue t f m -> m a
forall a b. (a -> b) -> a -> b
$ \case
    Pure t :: t
t -> t -> (NValue t f m -> m a) -> m a
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t NValue t f m -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue
    Free v :: NValue' t f m (NValue t f m)
v -> NValue' t f m (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue NValue' t f m (NValue t f m)
v

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , FromValue a m (Deeper (NValue' t f m (NValue t f m)))
         )
  => FromValue a m (Deeper (NValue t f m)) where
  fromValueMay :: Deeper (NValue t f m) -> m (Maybe a)
fromValueMay (Deeper v :: NValue t f m
v) = NValue t f m -> (NValue t f m -> m (Maybe a)) -> m (Maybe a)
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m (Maybe a)) -> m (Maybe a))
-> (NValue t f m -> m (Maybe a)) -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ \case
    Pure t :: t
t -> t -> (NValue t f m -> m (Maybe a)) -> m (Maybe a)
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t (Deeper (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (Deeper (NValue t f m) -> m (Maybe a))
-> (NValue t f m -> Deeper (NValue t f m))
-> NValue t f m
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper)
    Free v :: NValue' t f m (NValue t f m)
v -> Deeper (NValue' t f m (NValue t f m)) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper NValue' t f m (NValue t f m)
v)
  fromValue :: Deeper (NValue t f m) -> m a
fromValue (Deeper v :: NValue t f m
v) = NValue t f m -> (NValue t f m -> m a) -> m a
forall v (m :: * -> *) r. MonadValue v m => v -> (v -> m r) -> m r
demand NValue t f m
v ((NValue t f m -> m a) -> m a) -> (NValue t f m -> m a) -> m a
forall a b. (a -> b) -> a -> b
$ \case
    Pure t :: t
t -> t -> (NValue t f m -> m a) -> m a
forall t (m :: * -> *) a r.
MonadThunk t m a =>
t -> (a -> m r) -> m r
force t
t (Deeper (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (Deeper (NValue t f m) -> m a)
-> (NValue t f m -> Deeper (NValue t f m)) -> NValue t f m -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NValue t f m -> Deeper (NValue t f m)
forall a. a -> Deeper a
Deeper)
    Free v :: NValue' t f m (NValue t f m)
v -> Deeper (NValue' t f m (NValue t f m)) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue () m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe ())
fromValueMay = \case
    NVConstant' NNull -> Maybe () -> m (Maybe ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe () -> m (Maybe ())) -> Maybe () -> m (Maybe ())
forall a b. (a -> b) -> a -> b
$ () -> Maybe ()
forall a. a -> Maybe a
Just ()
    _                 -> Maybe () -> m (Maybe ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ()
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m ()
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe ())
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe ()) -> (Maybe () -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: ()
b -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
b
    _      -> ValueFrame t f m -> m ()
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m ()) -> ValueFrame t f m -> m ()
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TNull (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue Bool m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Bool)
fromValueMay = \case
    NVConstant' (NBool b :: Bool
b) -> Maybe Bool -> m (Maybe Bool)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Bool -> m (Maybe Bool)) -> Maybe Bool -> m (Maybe Bool)
forall a b. (a -> b) -> a -> b
$ Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
b
    _                     -> Maybe Bool -> m (Maybe Bool)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Bool
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m Bool
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe Bool)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe Bool) -> (Maybe Bool -> m Bool) -> m Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: Bool
b -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
b
    _      -> ValueFrame t f m -> m Bool
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m Bool) -> ValueFrame t f m -> m Bool
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TBool (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue Int m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Int)
fromValueMay = \case
    NVConstant' (NInt b :: Integer
b) -> Maybe Int -> m (Maybe Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Int -> m (Maybe Int)) -> Maybe Int -> m (Maybe Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int
forall a. a -> Maybe a
Just (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
b)
    _                    -> Maybe Int -> m (Maybe Int)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m Int
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe Int)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe Int) -> (Maybe Int -> m Int) -> m Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: Int
b -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
b
    _      -> ValueFrame t f m -> m Int
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m Int) -> ValueFrame t f m -> m Int
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TInt (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue Integer m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Integer)
fromValueMay = \case
    NVConstant' (NInt b :: Integer
b) -> Maybe Integer -> m (Maybe Integer)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Integer -> m (Maybe Integer))
-> Maybe Integer -> m (Maybe Integer)
forall a b. (a -> b) -> a -> b
$ Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
b
    _                    -> Maybe Integer -> m (Maybe Integer)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Integer
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m Integer
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe Integer)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe Integer) -> (Maybe Integer -> m Integer) -> m Integer
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: Integer
b -> Integer -> m Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
b
    _      -> ValueFrame t f m -> m Integer
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m Integer) -> ValueFrame t f m -> m Integer
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TInt (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue Float m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Float)
fromValueMay = \case
    NVConstant' (NFloat b :: Float
b) -> Maybe Float -> m (Maybe Float)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Float -> m (Maybe Float)) -> Maybe Float -> m (Maybe Float)
forall a b. (a -> b) -> a -> b
$ Float -> Maybe Float
forall a. a -> Maybe a
Just Float
b
    NVConstant' (NInt   i :: Integer
i) -> Maybe Float -> m (Maybe Float)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Float -> m (Maybe Float)) -> Maybe Float -> m (Maybe Float)
forall a b. (a -> b) -> a -> b
$ Float -> Maybe Float
forall a. a -> Maybe a
Just (Integer -> Float
forall a. Num a => Integer -> a
fromInteger Integer
i)
    _                      -> Maybe Float -> m (Maybe Float)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Float
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m Float
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe Float)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe Float) -> (Maybe Float -> m Float) -> m Float
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: Float
b -> Float -> m Float
forall (f :: * -> *) a. Applicative f => a -> f a
pure Float
b
    _      -> ValueFrame t f m -> m Float
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m Float) -> ValueFrame t f m -> m Float
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TFloat (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         , MonadEffects t f m
         )
  => FromValue NixString m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe NixString)
fromValueMay = \case
    NVStr' ns :: NixString
ns -> Maybe NixString -> m (Maybe NixString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe NixString -> m (Maybe NixString))
-> Maybe NixString -> m (Maybe NixString)
forall a b. (a -> b) -> a -> b
$ NixString -> Maybe NixString
forall a. a -> Maybe a
Just NixString
ns
    NVPath' p :: FilePath
p ->
      NixString -> Maybe NixString
forall a. a -> Maybe a
Just
        (NixString -> Maybe NixString)
-> (StorePath -> NixString) -> StorePath -> Maybe NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   Text -> NixString
hackyMakeNixStringWithoutContext
        (Text -> NixString)
-> (StorePath -> Text) -> StorePath -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   FilePath -> Text
Text.pack
        (FilePath -> Text) -> (StorePath -> FilePath) -> StorePath -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   StorePath -> FilePath
unStorePath
        (StorePath -> Maybe NixString)
-> m StorePath -> m (Maybe NixString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> m StorePath
forall e (m :: * -> *).
(Framed e m, MonadStore m) =>
FilePath -> m StorePath
addPath FilePath
p
    NVSet' s :: AttrSet (NValue t f m)
s _ -> case Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup "outPath" AttrSet (NValue t f m)
s of
      Nothing -> Maybe NixString -> m (Maybe NixString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe NixString
forall a. Maybe a
Nothing
      Just p :: NValue t f m
p  -> NValue t f m -> m (Maybe NixString)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue t f m
p
    _ -> Maybe NixString -> m (Maybe NixString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe NixString
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m NixString
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe NixString)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe NixString)
-> (Maybe NixString -> m NixString) -> m NixString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: NixString
b -> NixString -> m NixString
forall (f :: * -> *) a. Applicative f => a -> f a
pure NixString
b
    _      -> ValueFrame t f m -> m NixString
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m NixString)
-> ValueFrame t f m -> m NixString
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m (TStringContext -> ValueType
TString TStringContext
NoContext) (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue ByteString m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe ByteString)
fromValueMay = \case
    NVStr' ns :: NixString
ns -> Maybe ByteString -> m (Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe ByteString -> m (Maybe ByteString))
-> Maybe ByteString -> m (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ Text -> ByteString
encodeUtf8 (Text -> ByteString) -> Maybe Text -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NixString -> Maybe Text
hackyGetStringNoContext NixString
ns
    _         -> Maybe ByteString -> m (Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ByteString
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m ByteString
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe ByteString)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe ByteString)
-> (Maybe ByteString -> m ByteString) -> m ByteString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: ByteString
b -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
b
    _      -> ValueFrame t f m -> m ByteString
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m ByteString)
-> ValueFrame t f m -> m ByteString
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m (TStringContext -> ValueType
TString TStringContext
NoContext) (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

newtype Path = Path { Path -> FilePath
getPath :: FilePath }
    deriving Int -> Path -> ShowS
[Path] -> ShowS
Path -> FilePath
(Int -> Path -> ShowS)
-> (Path -> FilePath) -> ([Path] -> ShowS) -> Show Path
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [Path] -> ShowS
$cshowList :: [Path] -> ShowS
show :: Path -> FilePath
$cshow :: Path -> FilePath
showsPrec :: Int -> Path -> ShowS
$cshowsPrec :: Int -> Path -> ShowS
Show

instance ( Convertible e t f m
         , MonadValue (NValue t f m) m
         )
  => FromValue Path m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe Path)
fromValueMay = \case
    NVPath' p :: FilePath
p  -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Path -> m (Maybe Path)) -> Maybe Path -> m (Maybe Path)
forall a b. (a -> b) -> a -> b
$ Path -> Maybe Path
forall a. a -> Maybe a
Just (FilePath -> Path
Path FilePath
p)
    NVStr'  ns :: NixString
ns -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Path -> m (Maybe Path)) -> Maybe Path -> m (Maybe Path)
forall a b. (a -> b) -> a -> b
$ FilePath -> Path
Path (FilePath -> Path) -> (Text -> FilePath) -> Text -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> FilePath
Text.unpack (Text -> Path) -> Maybe Text -> Maybe Path
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NixString -> Maybe Text
hackyGetStringNoContext NixString
ns
    NVSet' s :: AttrSet (NValue t f m)
s _ -> case Text -> AttrSet (NValue t f m) -> Maybe (NValue t f m)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup "outPath" AttrSet (NValue t f m)
s of
      Nothing -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Path
forall a. Maybe a
Nothing
      Just p :: NValue t f m
p  -> NValue t f m -> m (Maybe Path)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay @Path NValue t f m
p
    _ -> Maybe Path -> m (Maybe Path)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Path
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m Path
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe Path)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe Path) -> (Maybe Path -> m Path) -> m Path
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: Path
b -> Path -> m Path
forall (f :: * -> *) a. Applicative f => a -> f a
pure Path
b
    _      -> ValueFrame t f m -> m Path
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m Path) -> ValueFrame t f m -> m Path
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TPath (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance Convertible e t f m
  => FromValue [NValue t f m] m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe [NValue t f m])
fromValueMay = \case
    NVList' l :: [NValue t f m]
l -> Maybe [NValue t f m] -> m (Maybe [NValue t f m])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe [NValue t f m] -> m (Maybe [NValue t f m]))
-> Maybe [NValue t f m] -> m (Maybe [NValue t f m])
forall a b. (a -> b) -> a -> b
$ [NValue t f m] -> Maybe [NValue t f m]
forall a. a -> Maybe a
Just [NValue t f m]
l
    _         -> Maybe [NValue t f m] -> m (Maybe [NValue t f m])
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [NValue t f m]
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m [NValue t f m]
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe [NValue t f m])
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe [NValue t f m])
-> (Maybe [NValue t f m] -> m [NValue t f m]) -> m [NValue t f m]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: [NValue t f m]
b -> [NValue t f m] -> m [NValue t f m]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [NValue t f m]
b
    _      -> ValueFrame t f m -> m [NValue t f m]
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m [NValue t f m])
-> ValueFrame t f m -> m [NValue t f m]
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TList (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue [a] m (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe [a])
fromValueMay = \case
    Deeper (NVList' l :: [NValue t f m]
l) -> [Maybe a] -> Maybe [a]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([Maybe a] -> Maybe [a]) -> m [Maybe a] -> m (Maybe [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (NValue t f m -> m (Maybe a)) -> [NValue t f m] -> m [Maybe a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay [NValue t f m]
l
    _                  -> Maybe [a] -> m (Maybe [a])
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [a]
forall a. Maybe a
Nothing
  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m [a]
fromValue v :: Deeper (NValue' t f m (NValue t f m))
v = Deeper (NValue' t f m (NValue t f m)) -> m (Maybe [a])
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay Deeper (NValue' t f m (NValue t f m))
v m (Maybe [a]) -> (Maybe [a] -> m [a]) -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: [a]
b -> [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [a]
b
    _      -> ValueFrame t f m -> m [a]
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m [a]) -> ValueFrame t f m -> m [a]
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TList (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
forall a. Deeper a -> a
getDeeper Deeper (NValue' t f m (NValue t f m))
v))

instance Convertible e t f m
  => FromValue (AttrSet (NValue t f m)) m (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m) -> m (Maybe (AttrSet (NValue t f m)))
fromValueMay = \case
    NVSet' s :: AttrSet (NValue t f m)
s _ -> Maybe (AttrSet (NValue t f m))
-> m (Maybe (AttrSet (NValue t f m)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (AttrSet (NValue t f m))
 -> m (Maybe (AttrSet (NValue t f m))))
-> Maybe (AttrSet (NValue t f m))
-> m (Maybe (AttrSet (NValue t f m)))
forall a b. (a -> b) -> a -> b
$ AttrSet (NValue t f m) -> Maybe (AttrSet (NValue t f m))
forall a. a -> Maybe a
Just AttrSet (NValue t f m)
s
    _          -> Maybe (AttrSet (NValue t f m))
-> m (Maybe (AttrSet (NValue t f m)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (AttrSet (NValue t f m))
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m) -> m (AttrSet (NValue t f m))
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m) -> m (Maybe (AttrSet (NValue t f m)))
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe (AttrSet (NValue t f m)))
-> (Maybe (AttrSet (NValue t f m)) -> m (AttrSet (NValue t f m)))
-> m (AttrSet (NValue t f m))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: AttrSet (NValue t f m)
b -> AttrSet (NValue t f m) -> m (AttrSet (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet (NValue t f m)
b
    _      -> ValueFrame t f m -> m (AttrSet (NValue t f m))
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (AttrSet (NValue t f m)))
-> ValueFrame t f m -> m (AttrSet (NValue t f m))
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TSet (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue (AttrSet a) m (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe (AttrSet a))
fromValueMay = \case
    Deeper (NVSet' s :: AttrSet (NValue t f m)
s _) -> HashMap Text (Maybe a) -> Maybe (AttrSet a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence (HashMap Text (Maybe a) -> Maybe (AttrSet a))
-> m (HashMap Text (Maybe a)) -> m (Maybe (AttrSet a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (NValue t f m -> m (Maybe a))
-> AttrSet (NValue t f m) -> m (HashMap Text (Maybe a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay AttrSet (NValue t f m)
s
    _                   -> Maybe (AttrSet a) -> m (Maybe (AttrSet a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (AttrSet a)
forall a. Maybe a
Nothing
  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m (AttrSet a)
fromValue v :: Deeper (NValue' t f m (NValue t f m))
v = Deeper (NValue' t f m (NValue t f m)) -> m (Maybe (AttrSet a))
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay Deeper (NValue' t f m (NValue t f m))
v m (Maybe (AttrSet a))
-> (Maybe (AttrSet a) -> m (AttrSet a)) -> m (AttrSet a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: AttrSet a
b -> AttrSet a -> m (AttrSet a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet a
b
    _      -> ValueFrame t f m -> m (AttrSet a)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (AttrSet a))
-> ValueFrame t f m -> m (AttrSet a)
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TSet (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
forall a. Deeper a -> a
getDeeper Deeper (NValue' t f m (NValue t f m))
v))

instance Convertible e t f m
  => FromValue (AttrSet (NValue t f m), AttrSet SourcePos) m
              (NValue' t f m (NValue t f m)) where
  fromValueMay :: NValue' t f m (NValue t f m)
-> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
fromValueMay = \case
    NVSet' s :: AttrSet (NValue t f m)
s p :: AttrSet SourcePos
p -> Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
-> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
 -> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos)))
-> Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
-> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
forall a b. (a -> b) -> a -> b
$ (AttrSet (NValue t f m), AttrSet SourcePos)
-> Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
forall a. a -> Maybe a
Just (AttrSet (NValue t f m)
s, AttrSet SourcePos
p)
    _          -> Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
-> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
forall a. Maybe a
Nothing
  fromValue :: NValue' t f m (NValue t f m)
-> m (AttrSet (NValue t f m), AttrSet SourcePos)
fromValue v :: NValue' t f m (NValue t f m)
v = NValue' t f m (NValue t f m)
-> m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay NValue' t f m (NValue t f m)
v m (Maybe (AttrSet (NValue t f m), AttrSet SourcePos))
-> (Maybe (AttrSet (NValue t f m), AttrSet SourcePos)
    -> m (AttrSet (NValue t f m), AttrSet SourcePos))
-> m (AttrSet (NValue t f m), AttrSet SourcePos)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: (AttrSet (NValue t f m), AttrSet SourcePos)
b -> (AttrSet (NValue t f m), AttrSet SourcePos)
-> m (AttrSet (NValue t f m), AttrSet SourcePos)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AttrSet (NValue t f m), AttrSet SourcePos)
b
    _      -> ValueFrame t f m -> m (AttrSet (NValue t f m), AttrSet SourcePos)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (AttrSet (NValue t f m), AttrSet SourcePos))
-> ValueFrame t f m
-> m (AttrSet (NValue t f m), AttrSet SourcePos)
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TSet (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free NValue' t f m (NValue t f m)
v)

instance ( Convertible e t f m
         , FromValue a m (NValue t f m)
         )
  => FromValue (AttrSet a, AttrSet SourcePos) m
              (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m))
-> m (Maybe (AttrSet a, AttrSet SourcePos))
fromValueMay = \case
    Deeper (NVSet' s :: AttrSet (NValue t f m)
s p :: AttrSet SourcePos
p) -> (AttrSet a -> (AttrSet a, AttrSet SourcePos))
-> Maybe (AttrSet a) -> Maybe (AttrSet a, AttrSet SourcePos)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (, AttrSet SourcePos
p) (Maybe (AttrSet a) -> Maybe (AttrSet a, AttrSet SourcePos))
-> (HashMap Text (Maybe a) -> Maybe (AttrSet a))
-> HashMap Text (Maybe a)
-> Maybe (AttrSet a, AttrSet SourcePos)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Text (Maybe a) -> Maybe (AttrSet a)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence (HashMap Text (Maybe a) -> Maybe (AttrSet a, AttrSet SourcePos))
-> m (HashMap Text (Maybe a))
-> m (Maybe (AttrSet a, AttrSet SourcePos))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (NValue t f m -> m (Maybe a))
-> AttrSet (NValue t f m) -> m (HashMap Text (Maybe a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NValue t f m -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay AttrSet (NValue t f m)
s
    _                   -> Maybe (AttrSet a, AttrSet SourcePos)
-> m (Maybe (AttrSet a, AttrSet SourcePos))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (AttrSet a, AttrSet SourcePos)
forall a. Maybe a
Nothing
  fromValue :: Deeper (NValue' t f m (NValue t f m))
-> m (AttrSet a, AttrSet SourcePos)
fromValue v :: Deeper (NValue' t f m (NValue t f m))
v = Deeper (NValue' t f m (NValue t f m))
-> m (Maybe (AttrSet a, AttrSet SourcePos))
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay Deeper (NValue' t f m (NValue t f m))
v m (Maybe (AttrSet a, AttrSet SourcePos))
-> (Maybe (AttrSet a, AttrSet SourcePos)
    -> m (AttrSet a, AttrSet SourcePos))
-> m (AttrSet a, AttrSet SourcePos)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Just b :: (AttrSet a, AttrSet SourcePos)
b -> (AttrSet a, AttrSet SourcePos) -> m (AttrSet a, AttrSet SourcePos)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AttrSet a, AttrSet SourcePos)
b
    _      -> ValueFrame t f m -> m (AttrSet a, AttrSet SourcePos)
forall s e (m :: * -> *) a.
(Framed e m, Exception s, MonadThrow m) =>
s -> m a
throwError (ValueFrame t f m -> m (AttrSet a, AttrSet SourcePos))
-> ValueFrame t f m -> m (AttrSet a, AttrSet SourcePos)
forall a b. (a -> b) -> a -> b
$ ValueType -> NValue t f m -> ValueFrame t f m
forall t (f :: * -> *) (m :: * -> *).
ValueType -> NValue t f m -> ValueFrame t f m
Expectation @t @f @m ValueType
TSet (NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
forall a. Deeper a -> a
getDeeper Deeper (NValue' t f m (NValue t f m))
v))

-- This instance needs IncoherentInstances, and only because of ToBuiltin
instance ( Convertible e t f m
         , FromValue a m (NValue' t f m (NValue t f m))
         )
  => FromValue a m (Deeper (NValue' t f m (NValue t f m))) where
  fromValueMay :: Deeper (NValue' t f m (NValue t f m)) -> m (Maybe a)
fromValueMay = NValue' t f m (NValue t f m) -> m (Maybe a)
forall a (m :: * -> *) v. FromValue a m v => v -> m (Maybe a)
fromValueMay (NValue' t f m (NValue t f m) -> m (Maybe a))
-> (Deeper (NValue' t f m (NValue t f m))
    -> NValue' t f m (NValue t f m))
-> Deeper (NValue' t f m (NValue t f m))
-> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
forall a. Deeper a -> a
getDeeper
  fromValue :: Deeper (NValue' t f m (NValue t f m)) -> m a
fromValue    = NValue' t f m (NValue t f m) -> m a
forall a (m :: * -> *) v. FromValue a m v => v -> m a
fromValue (NValue' t f m (NValue t f m) -> m a)
-> (Deeper (NValue' t f m (NValue t f m))
    -> NValue' t f m (NValue t f m))
-> Deeper (NValue' t f m (NValue t f m))
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Deeper (NValue' t f m (NValue t f m))
-> NValue' t f m (NValue t f m)
forall a. Deeper a -> a
getDeeper

{-----------------------------------------------------------------------
   ToValue
 -----------------------------------------------------------------------}

class ToValue a m v where
    toValue :: a -> m v

instance (Convertible e t f m, ToValue a m (NValue' t f m (NValue t f m)))
  => ToValue a m (NValue t f m) where
  toValue :: a -> m (NValue t f m)
toValue = (NValue' t f m (NValue t f m) -> NValue t f m)
-> m (NValue' t f m (NValue t f m)) -> m (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free (m (NValue' t f m (NValue t f m)) -> m (NValue t f m))
-> (a -> m (NValue' t f m (NValue t f m))) -> a -> m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (NValue' t f m (NValue t f m))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

instance ( Convertible e t f m
         , ToValue a m (Deeper (NValue' t f m (NValue t f m)))
         )
  => ToValue a m (Deeper (NValue t f m)) where
  toValue :: a -> m (Deeper (NValue t f m))
toValue = (Deeper (NValue' t f m (NValue t f m)) -> Deeper (NValue t f m))
-> m (Deeper (NValue' t f m (NValue t f m)))
-> m (Deeper (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((NValue' t f m (NValue t f m) -> NValue t f m)
-> Deeper (NValue' t f m (NValue t f m)) -> Deeper (NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NValue' t f m (NValue t f m) -> NValue t f m
forall (f :: * -> *) a. f (Free f a) -> Free f a
Free) (m (Deeper (NValue' t f m (NValue t f m)))
 -> m (Deeper (NValue t f m)))
-> (a -> m (Deeper (NValue' t f m (NValue t f m))))
-> a
-> m (Deeper (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Deeper (NValue' t f m (NValue t f m)))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

instance Convertible e t f m
  => ToValue () m (NValue' t f m (NValue t f m)) where
  toValue :: () -> m (NValue' t f m (NValue t f m))
toValue _ = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (NAtom -> NValue' t f m (NValue t f m))
-> NAtom
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
nvConstant' (NAtom -> m (NValue' t f m (NValue t f m)))
-> NAtom -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ NAtom
NNull

instance Convertible e t f m
  => ToValue Bool m (NValue' t f m (NValue t f m)) where
  toValue :: Bool -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Bool -> NValue' t f m (NValue t f m))
-> Bool
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
nvConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Bool -> NAtom) -> Bool -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool

instance Convertible e t f m
  => ToValue Int m (NValue' t f m (NValue t f m)) where
  toValue :: Int -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Int -> NValue' t f m (NValue t f m))
-> Int
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
nvConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Int -> NAtom) -> Int -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt (Integer -> NAtom) -> (Int -> Integer) -> Int -> NAtom
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger

instance Convertible e t f m
  => ToValue Integer m (NValue' t f m (NValue t f m)) where
  toValue :: Integer -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Integer -> NValue' t f m (NValue t f m))
-> Integer
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
nvConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Integer -> NAtom) -> Integer -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> NAtom
NInt

instance Convertible e t f m
  => ToValue Float m (NValue' t f m (NValue t f m)) where
  toValue :: Float -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Float -> NValue' t f m (NValue t f m))
-> Float
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NAtom -> NValue' t f m r
nvConstant' (NAtom -> NValue' t f m (NValue t f m))
-> (Float -> NAtom) -> Float -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> NAtom
NFloat

instance Convertible e t f m
  => ToValue NixString m (NValue' t f m (NValue t f m)) where
  toValue :: NixString -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (NixString -> NValue' t f m (NValue t f m))
-> NixString
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NixString -> NValue' t f m r
nvStr'

instance Convertible e t f m
  => ToValue ByteString m (NValue' t f m (NValue t f m)) where
  toValue :: ByteString -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (ByteString -> NValue' t f m (NValue t f m))
-> ByteString
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixString -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
NixString -> NValue' t f m r
nvStr' (NixString -> NValue' t f m (NValue t f m))
-> (ByteString -> NixString)
-> ByteString
-> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NixString
hackyMakeNixStringWithoutContext (Text -> NixString)
-> (ByteString -> Text) -> ByteString -> NixString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
decodeUtf8

instance Convertible e t f m
  => ToValue Path m (NValue' t f m (NValue t f m)) where
  toValue :: Path -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> (Path -> NValue' t f m (NValue t f m))
-> Path
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> NValue' t f m (NValue t f m)
forall (f :: * -> *) t (m :: * -> *) r.
Applicative f =>
FilePath -> NValue' t f m r
nvPath' (FilePath -> NValue' t f m (NValue t f m))
-> (Path -> FilePath) -> Path -> NValue' t f m (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path -> FilePath
getPath

instance Convertible e t f m
  => ToValue StorePath m (NValue' t f m (NValue t f m)) where
  toValue :: StorePath -> m (NValue' t f m (NValue t f m))
toValue = Path -> m (NValue' t f m (NValue t f m))
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Path -> m (NValue' t f m (NValue t f m)))
-> (StorePath -> Path)
-> StorePath
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Path
Path (FilePath -> Path) -> (StorePath -> FilePath) -> StorePath -> Path
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StorePath -> FilePath
unStorePath

instance ( Convertible e t f m
         )
  => ToValue SourcePos m (NValue' t f m (NValue t f m)) where
  toValue :: SourcePos -> m (NValue' t f m (NValue t f m))
toValue (SourcePos f :: FilePath
f l :: Pos
l c :: Pos
c) = do
    NValue t f m
f' <- NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Text -> NixString
principledMakeNixStringWithoutContext (FilePath -> Text
Text.pack FilePath
f))
    NValue t f m
l' <- Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Pos -> Int
unPos Pos
l)
    NValue t f m
c' <- Int -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue (Pos -> Int
unPos Pos
c)
    let pos :: HashMap Text (NValue t f m)
pos = [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [("file" :: Text, NValue t f m
f'), ("line", NValue t f m
l'), ("column", NValue t f m
c')]
    NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ HashMap Text (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' HashMap Text (NValue t f m)
pos AttrSet SourcePos
forall a. Monoid a => a
mempty

-- | With 'ToValue', we can always act recursively
instance Convertible e t f m
  => ToValue [NValue t f m] m (NValue' t f m (NValue t f m)) where
  toValue :: [NValue t f m] -> m (NValue' t f m (NValue t f m))
toValue = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> ([NValue t f m] -> NValue' t f m (NValue t f m))
-> [NValue t f m]
-> m (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
[r] -> NValue' t f m r
nvList'

instance (Convertible e t f m, ToValue a m (NValue t f m))
  => ToValue [a] m (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: [a] -> m (Deeper (NValue' t f m (NValue t f m)))
toValue = ([NValue t f m] -> Deeper (NValue' t f m (NValue t f m)))
-> m [NValue t f m] -> m (Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> ([NValue t f m] -> NValue' t f m (NValue t f m))
-> [NValue t f m]
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NValue t f m] -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
[r] -> NValue' t f m r
nvList') (m [NValue t f m] -> m (Deeper (NValue' t f m (NValue t f m))))
-> ([a] -> m [NValue t f m])
-> [a]
-> m (Deeper (NValue' t f m (NValue t f m)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m (NValue t f m)) -> [a] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue

instance Convertible e t f m
  => ToValue (AttrSet (NValue t f m)) m (NValue' t f m (NValue t f m)) where
  toValue :: AttrSet (NValue t f m) -> m (NValue' t f m (NValue t f m))
toValue s :: AttrSet (NValue t f m)
s = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ AttrSet (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' AttrSet (NValue t f m)
s AttrSet SourcePos
forall a. Monoid a => a
mempty

instance (Convertible e t f m, ToValue a m (NValue t f m))
  => ToValue (AttrSet a) m (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: AttrSet a -> m (Deeper (NValue' t f m (NValue t f m)))
toValue s :: AttrSet a
s = (NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> (AttrSet SourcePos -> NValue' t f m (NValue t f m))
-> AttrSet SourcePos
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((AttrSet SourcePos -> NValue' t f m (NValue t f m))
 -> AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> (HashMap Text (NValue t f m)
    -> AttrSet SourcePos -> NValue' t f m (NValue t f m))
-> HashMap Text (NValue t f m)
-> AttrSet SourcePos
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' (HashMap Text (NValue t f m)
 -> AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> m (HashMap Text (NValue t f m))
-> m (AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> m (NValue t f m))
-> AttrSet a -> m (HashMap Text (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue AttrSet a
s m (AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> m (AttrSet SourcePos)
-> m (Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AttrSet SourcePos -> m (AttrSet SourcePos)
forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet SourcePos
forall a. Monoid a => a
mempty

instance Convertible e t f m
  => ToValue (AttrSet (NValue t f m), AttrSet SourcePos) m
            (NValue' t f m (NValue t f m)) where
  toValue :: (AttrSet (NValue t f m), AttrSet SourcePos)
-> m (NValue' t f m (NValue t f m))
toValue (s :: AttrSet (NValue t f m)
s, p :: AttrSet SourcePos
p) = NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ AttrSet (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' AttrSet (NValue t f m)
s AttrSet SourcePos
p

instance (Convertible e t f m, ToValue a m (NValue t f m))
  => ToValue (AttrSet a, AttrSet SourcePos) m
            (Deeper (NValue' t f m (NValue t f m))) where
  toValue :: (AttrSet a, AttrSet SourcePos)
-> m (Deeper (NValue' t f m (NValue t f m)))
toValue (s :: AttrSet a
s, p :: AttrSet SourcePos
p) = (NValue' t f m (NValue t f m)
-> Deeper (NValue' t f m (NValue t f m))
forall a. a -> Deeper a
Deeper (NValue' t f m (NValue t f m)
 -> Deeper (NValue' t f m (NValue t f m)))
-> (AttrSet SourcePos -> NValue' t f m (NValue t f m))
-> AttrSet SourcePos
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((AttrSet SourcePos -> NValue' t f m (NValue t f m))
 -> AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> (HashMap Text (NValue t f m)
    -> AttrSet SourcePos -> NValue' t f m (NValue t f m))
-> HashMap Text (NValue t f m)
-> AttrSet SourcePos
-> Deeper (NValue' t f m (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' (HashMap Text (NValue t f m)
 -> AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> m (HashMap Text (NValue t f m))
-> m (AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> m (NValue t f m))
-> AttrSet a -> m (HashMap Text (NValue t f m))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue AttrSet a
s m (AttrSet SourcePos -> Deeper (NValue' t f m (NValue t f m)))
-> m (AttrSet SourcePos)
-> m (Deeper (NValue' t f m (NValue t f m)))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AttrSet SourcePos -> m (AttrSet SourcePos)
forall (f :: * -> *) a. Applicative f => a -> f a
pure AttrSet SourcePos
p

instance Convertible e t f m
  => ToValue NixLikeContextValue m (NValue' t f m (NValue t f m)) where
  toValue :: NixLikeContextValue -> m (NValue' t f m (NValue t f m))
toValue nlcv :: NixLikeContextValue
nlcv = do
    Maybe (NValue t f m)
path <- if NixLikeContextValue -> Bool
nlcvPath NixLikeContextValue
nlcv then NValue t f m -> Maybe (NValue t f m)
forall a. a -> Maybe a
Just (NValue t f m -> Maybe (NValue t f m))
-> m (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Bool
True else Maybe (NValue t f m) -> m (Maybe (NValue t f m))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (NValue t f m)
forall a. Maybe a
Nothing
    Maybe (NValue t f m)
allOutputs <- if NixLikeContextValue -> Bool
nlcvAllOutputs NixLikeContextValue
nlcv
      then NValue t f m -> Maybe (NValue t f m)
forall a. a -> Maybe a
Just (NValue t f m -> Maybe (NValue t f m))
-> m (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue Bool
True
      else Maybe (NValue t f m) -> m (Maybe (NValue t f m))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (NValue t f m)
forall a. Maybe a
Nothing
    Maybe (NValue t f m)
outputs <- do
      let outputs :: [NixString]
outputs =
            (Text -> NixString) -> [Text] -> [NixString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> NixString
principledMakeNixStringWithoutContext ([Text] -> [NixString]) -> [Text] -> [NixString]
forall a b. (a -> b) -> a -> b
$ NixLikeContextValue -> [Text]
nlcvOutputs NixLikeContextValue
nlcv
      [NValue t f m]
ts :: [NValue t f m] <- (NixString -> m (NValue t f m)) -> [NixString] -> m [NValue t f m]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse NixString -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue [NixString]
outputs
      case [NValue t f m]
ts of
        [] -> Maybe (NValue t f m) -> m (Maybe (NValue t f m))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (NValue t f m)
forall a. Maybe a
Nothing
        _  -> NValue t f m -> Maybe (NValue t f m)
forall a. a -> Maybe a
Just (NValue t f m -> Maybe (NValue t f m))
-> m (NValue t f m) -> m (Maybe (NValue t f m))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [NValue t f m] -> m (NValue t f m)
forall a (m :: * -> *) v. ToValue a m v => a -> m v
toValue [NValue t f m]
ts
    NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m)))
-> NValue' t f m (NValue t f m) -> m (NValue' t f m (NValue t f m))
forall a b. (a -> b) -> a -> b
$ (HashMap Text (NValue t f m)
 -> AttrSet SourcePos -> NValue' t f m (NValue t f m))
-> AttrSet SourcePos
-> HashMap Text (NValue t f m)
-> NValue' t f m (NValue t f m)
forall a b c. (a -> b -> c) -> b -> a -> c
flip HashMap Text (NValue t f m)
-> AttrSet SourcePos -> NValue' t f m (NValue t f m)
forall (f :: * -> *) r t (m :: * -> *).
Applicative f =>
HashMap Text r -> AttrSet SourcePos -> NValue' t f m r
nvSet' AttrSet SourcePos
forall k v. HashMap k v
M.empty (HashMap Text (NValue t f m) -> NValue' t f m (NValue t f m))
-> HashMap Text (NValue t f m) -> NValue' t f m (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Text, NValue t f m)] -> HashMap Text (NValue t f m))
-> [(Text, NValue t f m)] -> HashMap Text (NValue t f m)
forall a b. (a -> b) -> a -> b
$ [Maybe (Text, NValue t f m)] -> [(Text, NValue t f m)]
forall a. [Maybe a] -> [a]
catMaybes
      [ (\p :: NValue t f m
p -> ("path", NValue t f m
p)) (NValue t f m -> (Text, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (Text, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
path
      , (\ao :: NValue t f m
ao -> ("allOutputs", NValue t f m
ao)) (NValue t f m -> (Text, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (Text, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
allOutputs
      , (\os :: NValue t f m
os -> ("outputs", NValue t f m
os)) (NValue t f m -> (Text, NValue t f m))
-> Maybe (NValue t f m) -> Maybe (Text, NValue t f m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (NValue t f m)
outputs
      ]

instance Convertible e t f m => ToValue () m (NExprF (NValue t f m)) where
  toValue :: () -> m (NExprF (NValue t f m))
toValue _ = NExprF (NValue t f m) -> m (NExprF (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NExprF (NValue t f m) -> m (NExprF (NValue t f m)))
-> (NAtom -> NExprF (NValue t f m))
-> NAtom
-> m (NExprF (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NExprF (NValue t f m)
forall r. NAtom -> NExprF r
NConstant (NAtom -> m (NExprF (NValue t f m)))
-> NAtom -> m (NExprF (NValue t f m))
forall a b. (a -> b) -> a -> b
$ NAtom
NNull

instance Convertible e t f m => ToValue Bool m (NExprF (NValue t f m)) where
  toValue :: Bool -> m (NExprF (NValue t f m))
toValue = NExprF (NValue t f m) -> m (NExprF (NValue t f m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NExprF (NValue t f m) -> m (NExprF (NValue t f m)))
-> (Bool -> NExprF (NValue t f m))
-> Bool
-> m (NExprF (NValue t f m))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NAtom -> NExprF (NValue t f m)
forall r. NAtom -> NExprF r
NConstant (NAtom -> NExprF (NValue t f m))
-> (Bool -> NAtom) -> Bool -> NExprF (NValue t f m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> NAtom
NBool