module Nbparts.Types.Metadata
( NotebookMetadata (..),
CellMetadata (..),
emptyCodeMetadata,
emptyGenericMetadata,
)
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.Map qualified as Map
import Data.Text (Text)
import GHC.Generics (Generic)
data NotebookMetadata = NotebookMetadata
{ NotebookMetadata -> Int
formatMajor :: Int,
NotebookMetadata -> Int
formatMinor :: Int,
NotebookMetadata -> JSONMeta
toplevel :: Ipynb.JSONMeta,
NotebookMetadata -> Map Text CellMetadata
cells :: Map Text CellMetadata
}
deriving ((forall x. NotebookMetadata -> Rep NotebookMetadata x)
-> (forall x. Rep NotebookMetadata x -> NotebookMetadata)
-> Generic NotebookMetadata
forall x. Rep NotebookMetadata x -> NotebookMetadata
forall x. NotebookMetadata -> Rep NotebookMetadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. NotebookMetadata -> Rep NotebookMetadata x
from :: forall x. NotebookMetadata -> Rep NotebookMetadata x
$cto :: forall x. Rep NotebookMetadata x -> NotebookMetadata
to :: forall x. Rep NotebookMetadata x -> NotebookMetadata
Generic, Int -> NotebookMetadata -> ShowS
[NotebookMetadata] -> ShowS
NotebookMetadata -> String
(Int -> NotebookMetadata -> ShowS)
-> (NotebookMetadata -> String)
-> ([NotebookMetadata] -> ShowS)
-> Show NotebookMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NotebookMetadata -> ShowS
showsPrec :: Int -> NotebookMetadata -> ShowS
$cshow :: NotebookMetadata -> String
show :: NotebookMetadata -> String
$cshowList :: [NotebookMetadata] -> ShowS
showList :: [NotebookMetadata] -> ShowS
Show, NotebookMetadata -> NotebookMetadata -> Bool
(NotebookMetadata -> NotebookMetadata -> Bool)
-> (NotebookMetadata -> NotebookMetadata -> Bool)
-> Eq NotebookMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NotebookMetadata -> NotebookMetadata -> Bool
== :: NotebookMetadata -> NotebookMetadata -> Bool
$c/= :: NotebookMetadata -> NotebookMetadata -> Bool
/= :: NotebookMetadata -> NotebookMetadata -> Bool
Eq, Eq NotebookMetadata
Eq NotebookMetadata =>
(NotebookMetadata -> NotebookMetadata -> Ordering)
-> (NotebookMetadata -> NotebookMetadata -> Bool)
-> (NotebookMetadata -> NotebookMetadata -> Bool)
-> (NotebookMetadata -> NotebookMetadata -> Bool)
-> (NotebookMetadata -> NotebookMetadata -> Bool)
-> (NotebookMetadata -> NotebookMetadata -> NotebookMetadata)
-> (NotebookMetadata -> NotebookMetadata -> NotebookMetadata)
-> Ord NotebookMetadata
NotebookMetadata -> NotebookMetadata -> Bool
NotebookMetadata -> NotebookMetadata -> Ordering
NotebookMetadata -> NotebookMetadata -> NotebookMetadata
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 :: NotebookMetadata -> NotebookMetadata -> Ordering
compare :: NotebookMetadata -> NotebookMetadata -> Ordering
$c< :: NotebookMetadata -> NotebookMetadata -> Bool
< :: NotebookMetadata -> NotebookMetadata -> Bool
$c<= :: NotebookMetadata -> NotebookMetadata -> Bool
<= :: NotebookMetadata -> NotebookMetadata -> Bool
$c> :: NotebookMetadata -> NotebookMetadata -> Bool
> :: NotebookMetadata -> NotebookMetadata -> Bool
$c>= :: NotebookMetadata -> NotebookMetadata -> Bool
>= :: NotebookMetadata -> NotebookMetadata -> Bool
$cmax :: NotebookMetadata -> NotebookMetadata -> NotebookMetadata
max :: NotebookMetadata -> NotebookMetadata -> NotebookMetadata
$cmin :: NotebookMetadata -> NotebookMetadata -> NotebookMetadata
min :: NotebookMetadata -> NotebookMetadata -> NotebookMetadata
Ord)
data CellMetadata
= CodeCellMetadata {CellMetadata -> Maybe Int
executionCount :: Maybe Int, CellMetadata -> JSONMeta
meta :: Ipynb.JSONMeta}
| GenericCellMetadata Ipynb.JSONMeta
deriving ((forall x. CellMetadata -> Rep CellMetadata x)
-> (forall x. Rep CellMetadata x -> CellMetadata)
-> Generic CellMetadata
forall x. Rep CellMetadata x -> CellMetadata
forall x. CellMetadata -> Rep CellMetadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CellMetadata -> Rep CellMetadata x
from :: forall x. CellMetadata -> Rep CellMetadata x
$cto :: forall x. Rep CellMetadata x -> CellMetadata
to :: forall x. Rep CellMetadata x -> CellMetadata
Generic, Int -> CellMetadata -> ShowS
[CellMetadata] -> ShowS
CellMetadata -> String
(Int -> CellMetadata -> ShowS)
-> (CellMetadata -> String)
-> ([CellMetadata] -> ShowS)
-> Show CellMetadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellMetadata -> ShowS
showsPrec :: Int -> CellMetadata -> ShowS
$cshow :: CellMetadata -> String
show :: CellMetadata -> String
$cshowList :: [CellMetadata] -> ShowS
showList :: [CellMetadata] -> ShowS
Show, CellMetadata -> CellMetadata -> Bool
(CellMetadata -> CellMetadata -> Bool)
-> (CellMetadata -> CellMetadata -> Bool) -> Eq CellMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellMetadata -> CellMetadata -> Bool
== :: CellMetadata -> CellMetadata -> Bool
$c/= :: CellMetadata -> CellMetadata -> Bool
/= :: CellMetadata -> CellMetadata -> Bool
Eq, Eq CellMetadata
Eq CellMetadata =>
(CellMetadata -> CellMetadata -> Ordering)
-> (CellMetadata -> CellMetadata -> Bool)
-> (CellMetadata -> CellMetadata -> Bool)
-> (CellMetadata -> CellMetadata -> Bool)
-> (CellMetadata -> CellMetadata -> Bool)
-> (CellMetadata -> CellMetadata -> CellMetadata)
-> (CellMetadata -> CellMetadata -> CellMetadata)
-> Ord CellMetadata
CellMetadata -> CellMetadata -> Bool
CellMetadata -> CellMetadata -> Ordering
CellMetadata -> CellMetadata -> CellMetadata
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 :: CellMetadata -> CellMetadata -> Ordering
compare :: CellMetadata -> CellMetadata -> Ordering
$c< :: CellMetadata -> CellMetadata -> Bool
< :: CellMetadata -> CellMetadata -> Bool
$c<= :: CellMetadata -> CellMetadata -> Bool
<= :: CellMetadata -> CellMetadata -> Bool
$c> :: CellMetadata -> CellMetadata -> Bool
> :: CellMetadata -> CellMetadata -> Bool
$c>= :: CellMetadata -> CellMetadata -> Bool
>= :: CellMetadata -> CellMetadata -> Bool
$cmax :: CellMetadata -> CellMetadata -> CellMetadata
max :: CellMetadata -> CellMetadata -> CellMetadata
$cmin :: CellMetadata -> CellMetadata -> CellMetadata
min :: CellMetadata -> CellMetadata -> CellMetadata
Ord)
emptyCodeMetadata :: CellMetadata
emptyCodeMetadata :: CellMetadata
emptyCodeMetadata = Maybe Int -> JSONMeta -> CellMetadata
CodeCellMetadata Maybe Int
forall a. Maybe a
Nothing (JSONMeta -> CellMetadata) -> JSONMeta -> CellMetadata
forall a b. (a -> b) -> a -> b
$ Map Text Value -> JSONMeta
Ipynb.JSONMeta Map Text Value
forall k a. Map k a
Map.empty
emptyGenericMetadata :: CellMetadata
emptyGenericMetadata :: CellMetadata
emptyGenericMetadata = JSONMeta -> CellMetadata
GenericCellMetadata (JSONMeta -> CellMetadata) -> JSONMeta -> CellMetadata
forall a b. (a -> b) -> a -> b
$ Map Text Value -> JSONMeta
Ipynb.JSONMeta Map Text Value
forall k a. Map k a
Map.empty
instance Aeson.ToJSON NotebookMetadata
instance Aeson.FromJSON NotebookMetadata
instance Aeson.ToJSON CellMetadata where
toJSON :: CellMetadata -> Value
toJSON = Options -> CellMetadata -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
Aeson.genericToJSON Options
jsonOptions
instance Aeson.FromJSON CellMetadata where
parseJSON :: Value -> Parser CellMetadata
parseJSON = Options -> Value -> Parser CellMetadata
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 = "meta"
},
constructorTagModifier = \case
String
"CodeCellMetadata" -> String
"code"
String
"GenericCellMetadata" -> String
"generic"
String
other -> String
other
}