-- | Internal utilities and instances
module Calamity.Internal.Utils (
  whileMFinalIO,
  untilJustFinalIO,
  whenJust,
  whenM,
  unlessM,
  lastMaybe,
  leftToMaybe,
  rightToMaybe,
  justToEither,
  (<<$>>),
  (<<*>>),
  (<.>),
  debug,
  info,
  Calamity.Internal.Utils.error,
  swap,
  DefaultingMap (..),
  AesonVector (..),
  CalamityFromStringShow (..),
  MaybeNull (..),
) where

-- import Calamity.Internal.RunIntoIO
import Calamity.Types.LogEff
import Control.Applicative
import Control.Monad (when)
import Data.Aeson
import Data.Aeson.Encoding (null_)
import Data.Default.Class
import qualified Data.Map as M
import Data.Semigroup (Last (..))
import Data.Text
import qualified Data.Vector.Unboxing as VU
import qualified DiPolysemy as Di
import GHC.Generics
import qualified Polysemy as P
import TextShow
import qualified TextShow.Generic as TSG

{- | Like whileM, but stateful effects are not preserved to mitigate memory leaks

 This means Polysemy.Error won't work to break the loop, etc.
 Instead, Error/Alternative will just result in the loop quitting.
-}
whileMFinalIO :: P.Sem r Bool -> P.Sem r ()
whileMFinalIO :: Sem r Bool -> Sem r ()
whileMFinalIO Sem r Bool
action = do
  Sem r Bool -> Sem r ()
forall (m :: * -> *). Monad m => m Bool -> m ()
go Sem r Bool
action
  where
    go :: m Bool -> m ()
go m Bool
action = do
      Bool
r <- m Bool
action
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
r (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ m Bool -> m ()
go_b m Bool
action
    {-# INLINE go #-}
    go_b :: m Bool -> m ()
go_b = m Bool -> m ()
go
    {-# NOINLINE go_b #-}

{- | Like untilJust, but stateful effects are not preserved to mitigate memory leaks

 This means Polysemy.Error won't work to break the loop, etc.
 Instead, Error/Alternative will just result in another loop.
-}
untilJustFinalIO :: P.Member (P.Final IO) r => P.Sem r (Maybe a) -> P.Sem r a
untilJustFinalIO :: Sem r (Maybe a) -> Sem r a
untilJustFinalIO Sem r (Maybe a)
action = do
  Sem r (Maybe a) -> Sem r a
forall (m :: * -> *) b. Monad m => m (Maybe b) -> m b
go Sem r (Maybe a)
action
  where
    go :: m (Maybe b) -> m b
go m (Maybe b)
action = do
      Maybe b
r <- m (Maybe b)
action
      case Maybe b
r of
        Just b
a ->
          b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
a
        Maybe b
_ ->
          m (Maybe b) -> m b
go_b m (Maybe b)
action
    {-# INLINE go #-}
    go_b :: m (Maybe b) -> m b
go_b = m (Maybe b) -> m b
go
    {-# NOINLINE go_b #-}

whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
whenJust :: Maybe a -> (a -> m ()) -> m ()
whenJust = ((a -> m ()) -> Maybe a -> m ()) -> Maybe a -> (a -> m ()) -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((a -> m ()) -> Maybe a -> m ())
 -> Maybe a -> (a -> m ()) -> m ())
-> ((a -> m ()) -> Maybe a -> m ())
-> Maybe a
-> (a -> m ())
-> m ()
forall a b. (a -> b) -> a -> b
$ m () -> (a -> m ()) -> Maybe a -> m ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

whenM :: Monad m => m Bool -> m () -> m ()
whenM :: m Bool -> m () -> m ()
whenM m Bool
p m ()
m =
  m Bool
p m Bool -> (Bool -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Bool
True -> m ()
m
    Bool
False -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: m Bool -> m () -> m ()
unlessM = m Bool -> m () -> m ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM (m Bool -> m () -> m ())
-> (m Bool -> m Bool) -> m Bool -> m () -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Bool
not (Bool -> Bool) -> m Bool -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>)

lastMaybe :: Maybe a -> Maybe a -> Maybe a
lastMaybe :: Maybe a -> Maybe a -> Maybe a
lastMaybe Maybe a
l Maybe a
r = Last a -> a
forall a. Last a -> a
getLast (Last a -> a) -> Maybe (Last a) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Last a) -> Maybe a -> Maybe (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Last a
forall a. a -> Last a
Last Maybe a
l Maybe (Last a) -> Maybe (Last a) -> Maybe (Last a)
forall a. Semigroup a => a -> a -> a
<> (a -> Last a) -> Maybe a -> Maybe (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Last a
forall a. a -> Last a
Last Maybe a
r

leftToMaybe :: Either e a -> Maybe e
leftToMaybe :: Either e a -> Maybe e
leftToMaybe (Left e
x) = e -> Maybe e
forall a. a -> Maybe a
Just e
x
leftToMaybe Either e a
_ = Maybe e
forall a. Maybe a
Nothing

rightToMaybe :: Either e a -> Maybe a
rightToMaybe :: Either e a -> Maybe a
rightToMaybe (Right a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
rightToMaybe Either e a
_ = Maybe a
forall a. Maybe a
Nothing

justToEither :: Maybe e -> Either e ()
justToEither :: Maybe e -> Either e ()
justToEither (Just e
x) = e -> Either e ()
forall a b. a -> Either a b
Left e
x
justToEither Maybe e
_ = () -> Either e ()
forall a b. b -> Either a b
Right ()

(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
<<$>> :: (a -> b) -> f (g a) -> f (g b)
(<<$>>) = (g a -> g b) -> f (g a) -> f (g b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((g a -> g b) -> f (g a) -> f (g b))
-> ((a -> b) -> g a -> g b) -> (a -> b) -> f (g a) -> f (g b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

infixl 4 <<$>>

(<<*>>) :: (Applicative f, Applicative g) => f (g (a -> b)) -> f (g a) -> f (g b)
<<*>> :: f (g (a -> b)) -> f (g a) -> f (g b)
(<<*>>) = (g (a -> b) -> g a -> g b) -> f (g (a -> b)) -> f (g a) -> f (g b)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 g (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

infixl 4 <<*>>

(<.>) :: Functor f => (a -> b) -> (c -> f a) -> (c -> f b)
<.> :: (a -> b) -> (c -> f a) -> c -> f b
(<.>) a -> b
f c -> f a
g c
x = a -> b
f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> c -> f a
g c
x

infixl 4 <.>

debug :: P.Member LogEff r => Text -> P.Sem r ()
debug :: Text -> Sem r ()
debug = Text -> Sem r ()
forall msg path (r :: EffectRow).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.debug

info :: P.Member LogEff r => Text -> P.Sem r ()
info :: Text -> Sem r ()
info = Text -> Sem r ()
forall msg path (r :: EffectRow).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.info

error :: P.Member LogEff r => Text -> P.Sem r ()
error :: Text -> Sem r ()
error = Text -> Sem r ()
forall msg path (r :: EffectRow).
(ToMessage msg, Member (Di Level path Message) r) =>
msg -> Sem r ()
Di.error

swap :: (a, b) -> (b, a)
swap :: (a, b) -> (b, a)
swap ~(a
a, b
b) = (b
b, a
a)

newtype DefaultingMap k v = DefaultingMap {DefaultingMap k v -> Map k v
unDefaultingMap :: M.Map k v}

instance Default (DefaultingMap k v) where
  def :: DefaultingMap k v
def = Map k v -> DefaultingMap k v
forall k v. Map k v -> DefaultingMap k v
DefaultingMap Map k v
forall k a. Map k a
M.empty

newtype AesonVector a = AesonVector {AesonVector a -> Vector a
unAesonVector :: VU.Vector a}
  deriving (Int -> AesonVector a -> ShowS
[AesonVector a] -> ShowS
AesonVector a -> String
(Int -> AesonVector a -> ShowS)
-> (AesonVector a -> String)
-> ([AesonVector a] -> ShowS)
-> Show (AesonVector a)
forall a. (Show a, Unboxable a) => Int -> AesonVector a -> ShowS
forall a. (Show a, Unboxable a) => [AesonVector a] -> ShowS
forall a. (Show a, Unboxable a) => AesonVector a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AesonVector a] -> ShowS
$cshowList :: forall a. (Show a, Unboxable a) => [AesonVector a] -> ShowS
show :: AesonVector a -> String
$cshow :: forall a. (Show a, Unboxable a) => AesonVector a -> String
showsPrec :: Int -> AesonVector a -> ShowS
$cshowsPrec :: forall a. (Show a, Unboxable a) => Int -> AesonVector a -> ShowS
Show) via VU.Vector a

instance (FromJSON a, VU.Unboxable a) => FromJSON (AesonVector a) where
  parseJSON :: Value -> Parser (AesonVector a)
parseJSON = (Vector a -> AesonVector a
forall a. Vector a -> AesonVector a
AesonVector (Vector a -> AesonVector a)
-> ([a] -> Vector a) -> [a] -> AesonVector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Vector a
forall a. Unboxable a => [a] -> Vector a
VU.fromList ([a] -> AesonVector a) -> Parser [a] -> Parser (AesonVector a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Parser [a] -> Parser (AesonVector a))
-> (Value -> Parser [a]) -> Value -> Parser (AesonVector a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser [a]
forall a. FromJSON a => Value -> Parser a
parseJSON

instance (ToJSON a, VU.Unboxable a) => ToJSON (AesonVector a) where
  toJSON :: AesonVector a -> Value
toJSON = [a] -> Value
forall a. ToJSON a => a -> Value
toJSON ([a] -> Value) -> (AesonVector a -> [a]) -> AesonVector a -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Unboxable a => Vector a -> [a]
VU.toList (Vector a -> [a])
-> (AesonVector a -> Vector a) -> AesonVector a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AesonVector a -> Vector a
forall a. AesonVector a -> Vector a
unAesonVector
  toEncoding :: AesonVector a -> Encoding
toEncoding = [a] -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding ([a] -> Encoding)
-> (AesonVector a -> [a]) -> AesonVector a -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Unboxable a => Vector a -> [a]
VU.toList (Vector a -> [a])
-> (AesonVector a -> Vector a) -> AesonVector a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AesonVector a -> Vector a
forall a. AesonVector a -> Vector a
unAesonVector

instance (TextShow a, VU.Unboxable a) => TextShow (AesonVector a) where
  showb :: AesonVector a -> Builder
showb = [a] -> Builder
forall a. TextShow a => [a] -> Builder
showbList ([a] -> Builder)
-> (AesonVector a -> [a]) -> AesonVector a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Unboxable a => Vector a -> [a]
VU.toList (Vector a -> [a])
-> (AesonVector a -> Vector a) -> AesonVector a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AesonVector a -> Vector a
forall a. AesonVector a -> Vector a
unAesonVector

newtype CalamityFromStringShow a = CalamityFromStringShow {CalamityFromStringShow a -> a
unCalamityFromStringShow :: a}
  deriving (Value -> Parser [CalamityFromStringShow a]
Value -> Parser (CalamityFromStringShow a)
(Value -> Parser (CalamityFromStringShow a))
-> (Value -> Parser [CalamityFromStringShow a])
-> FromJSON (CalamityFromStringShow a)
forall a. FromJSON a => Value -> Parser [CalamityFromStringShow a]
forall a. FromJSON a => Value -> Parser (CalamityFromStringShow a)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [CalamityFromStringShow a]
$cparseJSONList :: forall a. FromJSON a => Value -> Parser [CalamityFromStringShow a]
parseJSON :: Value -> Parser (CalamityFromStringShow a)
$cparseJSON :: forall a. FromJSON a => Value -> Parser (CalamityFromStringShow a)
FromJSON, [CalamityFromStringShow a] -> Encoding
[CalamityFromStringShow a] -> Value
CalamityFromStringShow a -> Encoding
CalamityFromStringShow a -> Value
(CalamityFromStringShow a -> Value)
-> (CalamityFromStringShow a -> Encoding)
-> ([CalamityFromStringShow a] -> Value)
-> ([CalamityFromStringShow a] -> Encoding)
-> ToJSON (CalamityFromStringShow a)
forall a. ToJSON a => [CalamityFromStringShow a] -> Encoding
forall a. ToJSON a => [CalamityFromStringShow a] -> Value
forall a. ToJSON a => CalamityFromStringShow a -> Encoding
forall a. ToJSON a => CalamityFromStringShow a -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CalamityFromStringShow a] -> Encoding
$ctoEncodingList :: forall a. ToJSON a => [CalamityFromStringShow a] -> Encoding
toJSONList :: [CalamityFromStringShow a] -> Value
$ctoJSONList :: forall a. ToJSON a => [CalamityFromStringShow a] -> Value
toEncoding :: CalamityFromStringShow a -> Encoding
$ctoEncoding :: forall a. ToJSON a => CalamityFromStringShow a -> Encoding
toJSON :: CalamityFromStringShow a -> Value
$ctoJSON :: forall a. ToJSON a => CalamityFromStringShow a -> Value
ToJSON) via a
  deriving (Int -> CalamityFromStringShow a -> Builder
Int -> CalamityFromStringShow a -> Text
Int -> CalamityFromStringShow a -> Text
[CalamityFromStringShow a] -> Builder
[CalamityFromStringShow a] -> Text
[CalamityFromStringShow a] -> Text
CalamityFromStringShow a -> Builder
CalamityFromStringShow a -> Text
CalamityFromStringShow a -> Text
(Int -> CalamityFromStringShow a -> Builder)
-> (CalamityFromStringShow a -> Builder)
-> ([CalamityFromStringShow a] -> Builder)
-> (Int -> CalamityFromStringShow a -> Text)
-> (CalamityFromStringShow a -> Text)
-> ([CalamityFromStringShow a] -> Text)
-> (Int -> CalamityFromStringShow a -> Text)
-> (CalamityFromStringShow a -> Text)
-> ([CalamityFromStringShow a] -> Text)
-> TextShow (CalamityFromStringShow a)
forall a. Show a => Int -> CalamityFromStringShow a -> Builder
forall a. Show a => Int -> CalamityFromStringShow a -> Text
forall a. Show a => Int -> CalamityFromStringShow a -> Text
forall a. Show a => [CalamityFromStringShow a] -> Builder
forall a. Show a => [CalamityFromStringShow a] -> Text
forall a. Show a => [CalamityFromStringShow a] -> Text
forall a. Show a => CalamityFromStringShow a -> Builder
forall a. Show a => CalamityFromStringShow a -> Text
forall a. Show a => CalamityFromStringShow a -> Text
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
showtlList :: [CalamityFromStringShow a] -> Text
$cshowtlList :: forall a. Show a => [CalamityFromStringShow a] -> Text
showtl :: CalamityFromStringShow a -> Text
$cshowtl :: forall a. Show a => CalamityFromStringShow a -> Text
showtlPrec :: Int -> CalamityFromStringShow a -> Text
$cshowtlPrec :: forall a. Show a => Int -> CalamityFromStringShow a -> Text
showtList :: [CalamityFromStringShow a] -> Text
$cshowtList :: forall a. Show a => [CalamityFromStringShow a] -> Text
showt :: CalamityFromStringShow a -> Text
$cshowt :: forall a. Show a => CalamityFromStringShow a -> Text
showtPrec :: Int -> CalamityFromStringShow a -> Text
$cshowtPrec :: forall a. Show a => Int -> CalamityFromStringShow a -> Text
showbList :: [CalamityFromStringShow a] -> Builder
$cshowbList :: forall a. Show a => [CalamityFromStringShow a] -> Builder
showb :: CalamityFromStringShow a -> Builder
$cshowb :: forall a. Show a => CalamityFromStringShow a -> Builder
showbPrec :: Int -> CalamityFromStringShow a -> Builder
$cshowbPrec :: forall a. Show a => Int -> CalamityFromStringShow a -> Builder
TextShow) via FromStringShow a

{- | An alternative 'Maybe' type that allows us to distinguish between parsed
 json fields that were null, and fields that didn't exist.
-}
data MaybeNull a
  = WasNull
  | NotNull a
  deriving (Int -> MaybeNull a -> ShowS
[MaybeNull a] -> ShowS
MaybeNull a -> String
(Int -> MaybeNull a -> ShowS)
-> (MaybeNull a -> String)
-> ([MaybeNull a] -> ShowS)
-> Show (MaybeNull a)
forall a. Show a => Int -> MaybeNull a -> ShowS
forall a. Show a => [MaybeNull a] -> ShowS
forall a. Show a => MaybeNull a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MaybeNull a] -> ShowS
$cshowList :: forall a. Show a => [MaybeNull a] -> ShowS
show :: MaybeNull a -> String
$cshow :: forall a. Show a => MaybeNull a -> String
showsPrec :: Int -> MaybeNull a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> MaybeNull a -> ShowS
Show, (forall x. MaybeNull a -> Rep (MaybeNull a) x)
-> (forall x. Rep (MaybeNull a) x -> MaybeNull a)
-> Generic (MaybeNull a)
forall x. Rep (MaybeNull a) x -> MaybeNull a
forall x. MaybeNull a -> Rep (MaybeNull a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (MaybeNull a) x -> MaybeNull a
forall a x. MaybeNull a -> Rep (MaybeNull a) x
$cto :: forall a x. Rep (MaybeNull a) x -> MaybeNull a
$cfrom :: forall a x. MaybeNull a -> Rep (MaybeNull a) x
Generic)
  deriving (Int -> MaybeNull a -> Builder
Int -> MaybeNull a -> Text
Int -> MaybeNull a -> Text
[MaybeNull a] -> Builder
[MaybeNull a] -> Text
[MaybeNull a] -> Text
MaybeNull a -> Builder
MaybeNull a -> Text
MaybeNull a -> Text
(Int -> MaybeNull a -> Builder)
-> (MaybeNull a -> Builder)
-> ([MaybeNull a] -> Builder)
-> (Int -> MaybeNull a -> Text)
-> (MaybeNull a -> Text)
-> ([MaybeNull a] -> Text)
-> (Int -> MaybeNull a -> Text)
-> (MaybeNull a -> Text)
-> ([MaybeNull a] -> Text)
-> TextShow (MaybeNull a)
forall a. TextShow a => Int -> MaybeNull a -> Builder
forall a. TextShow a => Int -> MaybeNull a -> Text
forall a. TextShow a => Int -> MaybeNull a -> Text
forall a. TextShow a => [MaybeNull a] -> Builder
forall a. TextShow a => [MaybeNull a] -> Text
forall a. TextShow a => [MaybeNull a] -> Text
forall a. TextShow a => MaybeNull a -> Builder
forall a. TextShow a => MaybeNull a -> Text
forall a. TextShow a => MaybeNull a -> Text
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
showtlList :: [MaybeNull a] -> Text
$cshowtlList :: forall a. TextShow a => [MaybeNull a] -> Text
showtl :: MaybeNull a -> Text
$cshowtl :: forall a. TextShow a => MaybeNull a -> Text
showtlPrec :: Int -> MaybeNull a -> Text
$cshowtlPrec :: forall a. TextShow a => Int -> MaybeNull a -> Text
showtList :: [MaybeNull a] -> Text
$cshowtList :: forall a. TextShow a => [MaybeNull a] -> Text
showt :: MaybeNull a -> Text
$cshowt :: forall a. TextShow a => MaybeNull a -> Text
showtPrec :: Int -> MaybeNull a -> Text
$cshowtPrec :: forall a. TextShow a => Int -> MaybeNull a -> Text
showbList :: [MaybeNull a] -> Builder
$cshowbList :: forall a. TextShow a => [MaybeNull a] -> Builder
showb :: MaybeNull a -> Builder
$cshowb :: forall a. TextShow a => MaybeNull a -> Builder
showbPrec :: Int -> MaybeNull a -> Builder
$cshowbPrec :: forall a. TextShow a => Int -> MaybeNull a -> Builder
TextShow) via TSG.FromGeneric (MaybeNull a)

instance FromJSON a => FromJSON (MaybeNull a) where
  parseJSON :: Value -> Parser (MaybeNull a)
parseJSON Value
Null = MaybeNull a -> Parser (MaybeNull a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MaybeNull a
forall a. MaybeNull a
WasNull
  parseJSON Value
x = a -> MaybeNull a
forall a. a -> MaybeNull a
NotNull (a -> MaybeNull a) -> Parser a -> Parser (MaybeNull a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser a
forall a. FromJSON a => Value -> Parser a
parseJSON Value
x

instance ToJSON a => ToJSON (MaybeNull a) where
  toJSON :: MaybeNull a -> Value
toJSON MaybeNull a
WasNull = Value
Null
  toJSON (NotNull a
x) = a -> Value
forall a. ToJSON a => a -> Value
toJSON a
x

  toEncoding :: MaybeNull a -> Encoding
toEncoding MaybeNull a
WasNull = Encoding
null_
  toEncoding (NotNull a
x) = a -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding a
x