{-# LANGUAGE UndecidableInstances #-}

module Nbparts.Types.Outputs
  ( NotebookOutputs (..),
    UnembeddedNotebookOutputs (..),
    UnembeddedCellOutput (..),
  )
where

import Data.Aeson (Options (constructorTagModifier, sumEncoding))
import Data.Aeson qualified as Aeson
import Data.Ipynb qualified as Ipynb
import Data.Map (Map)
import Data.Text (Text)
import GHC.Generics (Generic)
import Nbparts.Types.Mime (UnembeddedMimeBundle)

newtype NotebookOutputs a = NotebookOutputs (Map Text [Ipynb.Output a]) -- Map of Cell IDs to outputs.
  deriving ((forall x. NotebookOutputs a -> Rep (NotebookOutputs a) x)
-> (forall x. Rep (NotebookOutputs a) x -> NotebookOutputs a)
-> Generic (NotebookOutputs a)
forall x. Rep (NotebookOutputs a) x -> NotebookOutputs a
forall x. NotebookOutputs a -> Rep (NotebookOutputs a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (NotebookOutputs a) x -> NotebookOutputs a
forall a x. NotebookOutputs a -> Rep (NotebookOutputs a) x
$cfrom :: forall a x. NotebookOutputs a -> Rep (NotebookOutputs a) x
from :: forall x. NotebookOutputs a -> Rep (NotebookOutputs a) x
$cto :: forall a x. Rep (NotebookOutputs a) x -> NotebookOutputs a
to :: forall x. Rep (NotebookOutputs a) x -> NotebookOutputs a
Generic, Int -> NotebookOutputs a -> ShowS
[NotebookOutputs a] -> ShowS
NotebookOutputs a -> String
(Int -> NotebookOutputs a -> ShowS)
-> (NotebookOutputs a -> String)
-> ([NotebookOutputs a] -> ShowS)
-> Show (NotebookOutputs a)
forall a. Int -> NotebookOutputs a -> ShowS
forall a. [NotebookOutputs a] -> ShowS
forall a. NotebookOutputs a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> NotebookOutputs a -> ShowS
showsPrec :: Int -> NotebookOutputs a -> ShowS
$cshow :: forall a. NotebookOutputs a -> String
show :: NotebookOutputs a -> String
$cshowList :: forall a. [NotebookOutputs a] -> ShowS
showList :: [NotebookOutputs a] -> ShowS
Show, NotebookOutputs a -> NotebookOutputs a -> Bool
(NotebookOutputs a -> NotebookOutputs a -> Bool)
-> (NotebookOutputs a -> NotebookOutputs a -> Bool)
-> Eq (NotebookOutputs a)
forall a. NotebookOutputs a -> NotebookOutputs a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. NotebookOutputs a -> NotebookOutputs a -> Bool
== :: NotebookOutputs a -> NotebookOutputs a -> Bool
$c/= :: forall a. NotebookOutputs a -> NotebookOutputs a -> Bool
/= :: NotebookOutputs a -> NotebookOutputs a -> Bool
Eq, NonEmpty (NotebookOutputs a) -> NotebookOutputs a
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
(NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a)
-> (NonEmpty (NotebookOutputs a) -> NotebookOutputs a)
-> (forall b.
    Integral b =>
    b -> NotebookOutputs a -> NotebookOutputs a)
-> Semigroup (NotebookOutputs a)
forall b. Integral b => b -> NotebookOutputs a -> NotebookOutputs a
forall a. NonEmpty (NotebookOutputs a) -> NotebookOutputs a
forall a.
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a b.
Integral b =>
b -> NotebookOutputs a -> NotebookOutputs a
$c<> :: forall a.
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
<> :: NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
$csconcat :: forall a. NonEmpty (NotebookOutputs a) -> NotebookOutputs a
sconcat :: NonEmpty (NotebookOutputs a) -> NotebookOutputs a
$cstimes :: forall a b.
Integral b =>
b -> NotebookOutputs a -> NotebookOutputs a
stimes :: forall b. Integral b => b -> NotebookOutputs a -> NotebookOutputs a
Semigroup, Semigroup (NotebookOutputs a)
NotebookOutputs a
Semigroup (NotebookOutputs a) =>
NotebookOutputs a
-> (NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a)
-> ([NotebookOutputs a] -> NotebookOutputs a)
-> Monoid (NotebookOutputs a)
[NotebookOutputs a] -> NotebookOutputs a
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
forall a. Semigroup (NotebookOutputs a)
forall a. NotebookOutputs a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. [NotebookOutputs a] -> NotebookOutputs a
forall a.
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
$cmempty :: forall a. NotebookOutputs a
mempty :: NotebookOutputs a
$cmappend :: forall a.
NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
mappend :: NotebookOutputs a -> NotebookOutputs a -> NotebookOutputs a
$cmconcat :: forall a. [NotebookOutputs a] -> NotebookOutputs a
mconcat :: [NotebookOutputs a] -> NotebookOutputs a
Monoid)

newtype UnembeddedNotebookOutputs = UnembeddedNotebookOutputs (Map Text [UnembeddedCellOutput])
  deriving ((forall x.
 UnembeddedNotebookOutputs -> Rep UnembeddedNotebookOutputs x)
-> (forall x.
    Rep UnembeddedNotebookOutputs x -> UnembeddedNotebookOutputs)
-> Generic UnembeddedNotebookOutputs
forall x.
Rep UnembeddedNotebookOutputs x -> UnembeddedNotebookOutputs
forall x.
UnembeddedNotebookOutputs -> Rep UnembeddedNotebookOutputs x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
UnembeddedNotebookOutputs -> Rep UnembeddedNotebookOutputs x
from :: forall x.
UnembeddedNotebookOutputs -> Rep UnembeddedNotebookOutputs x
$cto :: forall x.
Rep UnembeddedNotebookOutputs x -> UnembeddedNotebookOutputs
to :: forall x.
Rep UnembeddedNotebookOutputs x -> UnembeddedNotebookOutputs
Generic, Int -> UnembeddedNotebookOutputs -> ShowS
[UnembeddedNotebookOutputs] -> ShowS
UnembeddedNotebookOutputs -> String
(Int -> UnembeddedNotebookOutputs -> ShowS)
-> (UnembeddedNotebookOutputs -> String)
-> ([UnembeddedNotebookOutputs] -> ShowS)
-> Show UnembeddedNotebookOutputs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnembeddedNotebookOutputs -> ShowS
showsPrec :: Int -> UnembeddedNotebookOutputs -> ShowS
$cshow :: UnembeddedNotebookOutputs -> String
show :: UnembeddedNotebookOutputs -> String
$cshowList :: [UnembeddedNotebookOutputs] -> ShowS
showList :: [UnembeddedNotebookOutputs] -> ShowS
Show, UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
(UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> (UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> Eq UnembeddedNotebookOutputs
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
== :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
$c/= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
/= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
Eq, Eq UnembeddedNotebookOutputs
Eq UnembeddedNotebookOutputs =>
(UnembeddedNotebookOutputs
 -> UnembeddedNotebookOutputs -> Ordering)
-> (UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> (UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> (UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> (UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool)
-> (UnembeddedNotebookOutputs
    -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs)
-> (UnembeddedNotebookOutputs
    -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs)
-> Ord UnembeddedNotebookOutputs
UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Ordering
UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Ordering
compare :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Ordering
$c< :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
< :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
$c<= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
<= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
$c> :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
> :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
$c>= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
>= :: UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs -> Bool
$cmax :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
max :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
$cmin :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
min :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
Ord, NonEmpty UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
(UnembeddedNotebookOutputs
 -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs)
-> (NonEmpty UnembeddedNotebookOutputs
    -> UnembeddedNotebookOutputs)
-> (forall b.
    Integral b =>
    b -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs)
-> Semigroup UnembeddedNotebookOutputs
forall b.
Integral b =>
b -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
$c<> :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
<> :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
$csconcat :: NonEmpty UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
sconcat :: NonEmpty UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
$cstimes :: forall b.
Integral b =>
b -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
stimes :: forall b.
Integral b =>
b -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
Semigroup, Semigroup UnembeddedNotebookOutputs
UnembeddedNotebookOutputs
Semigroup UnembeddedNotebookOutputs =>
UnembeddedNotebookOutputs
-> (UnembeddedNotebookOutputs
    -> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs)
-> ([UnembeddedNotebookOutputs] -> UnembeddedNotebookOutputs)
-> Monoid UnembeddedNotebookOutputs
[UnembeddedNotebookOutputs] -> UnembeddedNotebookOutputs
UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
$cmempty :: UnembeddedNotebookOutputs
mempty :: UnembeddedNotebookOutputs
$cmappend :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
mappend :: UnembeddedNotebookOutputs
-> UnembeddedNotebookOutputs -> UnembeddedNotebookOutputs
$cmconcat :: [UnembeddedNotebookOutputs] -> UnembeddedNotebookOutputs
mconcat :: [UnembeddedNotebookOutputs] -> UnembeddedNotebookOutputs
Monoid)

data UnembeddedCellOutput
  = Stream
      { UnembeddedCellOutput -> Text
name :: Text,
        UnembeddedCellOutput -> [Text]
lines :: [Text]
      }
  | DisplayData
      { UnembeddedCellOutput -> UnembeddedMimeBundle
outputs :: UnembeddedMimeBundle,
        UnembeddedCellOutput -> JSONMeta
metadata :: Ipynb.JSONMeta
      }
  | ExecuteResult
      { UnembeddedCellOutput -> Int
executeCount :: Int,
        outputs :: UnembeddedMimeBundle,
        metadata :: Ipynb.JSONMeta
      }
  | Err
      { name :: Text,
        UnembeddedCellOutput -> Text
value :: Text,
        UnembeddedCellOutput -> [Text]
traceback :: [Text]
      }
  deriving ((forall x. UnembeddedCellOutput -> Rep UnembeddedCellOutput x)
-> (forall x. Rep UnembeddedCellOutput x -> UnembeddedCellOutput)
-> Generic UnembeddedCellOutput
forall x. Rep UnembeddedCellOutput x -> UnembeddedCellOutput
forall x. UnembeddedCellOutput -> Rep UnembeddedCellOutput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UnembeddedCellOutput -> Rep UnembeddedCellOutput x
from :: forall x. UnembeddedCellOutput -> Rep UnembeddedCellOutput x
$cto :: forall x. Rep UnembeddedCellOutput x -> UnembeddedCellOutput
to :: forall x. Rep UnembeddedCellOutput x -> UnembeddedCellOutput
Generic, Int -> UnembeddedCellOutput -> ShowS
[UnembeddedCellOutput] -> ShowS
UnembeddedCellOutput -> String
(Int -> UnembeddedCellOutput -> ShowS)
-> (UnembeddedCellOutput -> String)
-> ([UnembeddedCellOutput] -> ShowS)
-> Show UnembeddedCellOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnembeddedCellOutput -> ShowS
showsPrec :: Int -> UnembeddedCellOutput -> ShowS
$cshow :: UnembeddedCellOutput -> String
show :: UnembeddedCellOutput -> String
$cshowList :: [UnembeddedCellOutput] -> ShowS
showList :: [UnembeddedCellOutput] -> ShowS
Show, UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
(UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> (UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> Eq UnembeddedCellOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
== :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
$c/= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
/= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
Eq, Eq UnembeddedCellOutput
Eq UnembeddedCellOutput =>
(UnembeddedCellOutput -> UnembeddedCellOutput -> Ordering)
-> (UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> (UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> (UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> (UnembeddedCellOutput -> UnembeddedCellOutput -> Bool)
-> (UnembeddedCellOutput
    -> UnembeddedCellOutput -> UnembeddedCellOutput)
-> (UnembeddedCellOutput
    -> UnembeddedCellOutput -> UnembeddedCellOutput)
-> Ord UnembeddedCellOutput
UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
UnembeddedCellOutput -> UnembeddedCellOutput -> Ordering
UnembeddedCellOutput
-> UnembeddedCellOutput -> UnembeddedCellOutput
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: UnembeddedCellOutput -> UnembeddedCellOutput -> Ordering
compare :: UnembeddedCellOutput -> UnembeddedCellOutput -> Ordering
$c< :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
< :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
$c<= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
<= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
$c> :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
> :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
$c>= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
>= :: UnembeddedCellOutput -> UnembeddedCellOutput -> Bool
$cmax :: UnembeddedCellOutput
-> UnembeddedCellOutput -> UnembeddedCellOutput
max :: UnembeddedCellOutput
-> UnembeddedCellOutput -> UnembeddedCellOutput
$cmin :: UnembeddedCellOutput
-> UnembeddedCellOutput -> UnembeddedCellOutput
min :: UnembeddedCellOutput
-> UnembeddedCellOutput -> UnembeddedCellOutput
Ord)

instance (Aeson.ToJSON (Ipynb.Output a)) => Aeson.ToJSON (NotebookOutputs a)

instance (Aeson.FromJSON (Ipynb.Output a)) => Aeson.FromJSON (NotebookOutputs a)

instance Aeson.ToJSON UnembeddedNotebookOutputs

instance Aeson.FromJSON UnembeddedNotebookOutputs

instance Aeson.ToJSON UnembeddedCellOutput where
  toJSON :: UnembeddedCellOutput -> Value
toJSON = Options -> UnembeddedCellOutput -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
Aeson.genericToJSON Options
jsonOptions

instance Aeson.FromJSON UnembeddedCellOutput where
  parseJSON :: Value -> Parser UnembeddedCellOutput
parseJSON = Options -> Value -> Parser UnembeddedCellOutput
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
Aeson.genericParseJSON Options
jsonOptions

jsonOptions :: Aeson.Options
jsonOptions :: Options
jsonOptions =
  Options
Aeson.defaultOptions
    { sumEncoding =
        Aeson.TaggedObject
          { tagFieldName = "type",
            contentsFieldName = "value"
          },
      constructorTagModifier = \case
        String
"Stream" -> String
"stream"
        String
"DisplayData" -> String
"display-data"
        String
"ExecuteResult" -> String
"execute-result"
        String
"Err" -> String
"error"
        String
other -> String
other
    }