-- Since this module mostly re-exports the submodules,
-- we disable the warning.
{-# OPTIONS_GHC -Wno-missing-import-lists #-}

module Nbparts.Types
  ( module Nbparts.Types.Manifest,
    module Nbparts.Types.Sources,
    module Nbparts.Types.Outputs,
    module Nbparts.Types.Metadata,
    module Nbparts.Types.Mime,
    module Nbparts.Types.Error,
    SomeNotebook (..),
    withSomeNotebook,
  )
where

import Control.Applicative (Alternative ((<|>)))
import Data.Aeson qualified as Aeson
import Data.Ipynb qualified as Ipynb
import Nbparts.Types.Error
import Nbparts.Types.Manifest
import Nbparts.Types.Metadata
import Nbparts.Types.Mime
import Nbparts.Types.Outputs
import Nbparts.Types.Sources

data SomeNotebook where
  SomeNotebook :: (Aeson.ToJSON (Ipynb.Notebook a), Aeson.FromJSON (Ipynb.Notebook a)) => Ipynb.Notebook a -> SomeNotebook

withSomeNotebook :: SomeNotebook -> (forall a. (Aeson.ToJSON (Ipynb.Notebook a), Aeson.FromJSON (Ipynb.Notebook a)) => Ipynb.Notebook a -> r) -> r
withSomeNotebook :: forall r.
SomeNotebook
-> (forall a.
    (ToJSON (Notebook a), FromJSON (Notebook a)) =>
    Notebook a -> r)
-> r
withSomeNotebook (SomeNotebook Notebook a
nb) forall a.
(ToJSON (Notebook a), FromJSON (Notebook a)) =>
Notebook a -> r
f = Notebook a -> r
forall a.
(ToJSON (Notebook a), FromJSON (Notebook a)) =>
Notebook a -> r
f Notebook a
nb

instance Show SomeNotebook where
  show :: SomeNotebook -> String
show (SomeNotebook Notebook a
nb) = String
"SomeNotebook (" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Notebook a -> String
forall a. Show a => a -> String
show Notebook a
nb String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"

instance Aeson.ToJSON SomeNotebook where
  toJSON :: SomeNotebook -> Value
toJSON (SomeNotebook Notebook a
nb) = Notebook a -> Value
forall a. ToJSON a => a -> Value
Aeson.toJSON Notebook a
nb

instance Aeson.FromJSON SomeNotebook where
  parseJSON :: Value -> Parser SomeNotebook
parseJSON Value
v =
    Notebook NbV3 -> SomeNotebook
forall a.
(ToJSON (Notebook a), FromJSON (Notebook a)) =>
Notebook a -> SomeNotebook
SomeNotebook (Notebook NbV3 -> SomeNotebook)
-> Parser (Notebook NbV3) -> Parser SomeNotebook
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON @(Ipynb.Notebook Ipynb.NbV3)) Value
v
      Parser SomeNotebook -> Parser SomeNotebook -> Parser SomeNotebook
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Notebook NbV4 -> SomeNotebook
forall a.
(ToJSON (Notebook a), FromJSON (Notebook a)) =>
Notebook a -> SomeNotebook
SomeNotebook (Notebook NbV4 -> SomeNotebook)
-> Parser (Notebook NbV4) -> Parser SomeNotebook
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON @(Ipynb.Notebook Ipynb.NbV4)) Value
v