module Nbparts.Types.Sources
  ( CellSource (..),
    CellType (..),
    CellMarker (..),
  )
where

import Data.Aeson (Options (constructorTagModifier, omitNothingFields, sumEncoding))
import Data.Aeson qualified as Aeson
import Data.Text (Text)
import GHC.Generics (Generic)
import Nbparts.Types.Mime (UnembeddedMimeAttachments)

data CellSource = CellSource
  { CellSource -> Text
id :: Text,
    CellSource -> CellType
cellType :: CellType,
    CellSource -> [Text]
lines :: [Text],
    CellSource -> Maybe UnembeddedMimeAttachments
attachments :: Maybe UnembeddedMimeAttachments
  }
  deriving ((forall x. CellSource -> Rep CellSource x)
-> (forall x. Rep CellSource x -> CellSource) -> Generic CellSource
forall x. Rep CellSource x -> CellSource
forall x. CellSource -> Rep CellSource x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CellSource -> Rep CellSource x
from :: forall x. CellSource -> Rep CellSource x
$cto :: forall x. Rep CellSource x -> CellSource
to :: forall x. Rep CellSource x -> CellSource
Generic, Int -> CellSource -> ShowS
[CellSource] -> ShowS
CellSource -> String
(Int -> CellSource -> ShowS)
-> (CellSource -> String)
-> ([CellSource] -> ShowS)
-> Show CellSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellSource -> ShowS
showsPrec :: Int -> CellSource -> ShowS
$cshow :: CellSource -> String
show :: CellSource -> String
$cshowList :: [CellSource] -> ShowS
showList :: [CellSource] -> ShowS
Show, CellSource -> CellSource -> Bool
(CellSource -> CellSource -> Bool)
-> (CellSource -> CellSource -> Bool) -> Eq CellSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellSource -> CellSource -> Bool
== :: CellSource -> CellSource -> Bool
$c/= :: CellSource -> CellSource -> Bool
/= :: CellSource -> CellSource -> Bool
Eq, Eq CellSource
Eq CellSource =>
(CellSource -> CellSource -> Ordering)
-> (CellSource -> CellSource -> Bool)
-> (CellSource -> CellSource -> Bool)
-> (CellSource -> CellSource -> Bool)
-> (CellSource -> CellSource -> Bool)
-> (CellSource -> CellSource -> CellSource)
-> (CellSource -> CellSource -> CellSource)
-> Ord CellSource
CellSource -> CellSource -> Bool
CellSource -> CellSource -> Ordering
CellSource -> CellSource -> CellSource
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 :: CellSource -> CellSource -> Ordering
compare :: CellSource -> CellSource -> Ordering
$c< :: CellSource -> CellSource -> Bool
< :: CellSource -> CellSource -> Bool
$c<= :: CellSource -> CellSource -> Bool
<= :: CellSource -> CellSource -> Bool
$c> :: CellSource -> CellSource -> Bool
> :: CellSource -> CellSource -> Bool
$c>= :: CellSource -> CellSource -> Bool
>= :: CellSource -> CellSource -> Bool
$cmax :: CellSource -> CellSource -> CellSource
max :: CellSource -> CellSource -> CellSource
$cmin :: CellSource -> CellSource -> CellSource
min :: CellSource -> CellSource -> CellSource
Ord)

data CellType = Markdown | Raw | Code
  deriving ((forall x. CellType -> Rep CellType x)
-> (forall x. Rep CellType x -> CellType) -> Generic CellType
forall x. Rep CellType x -> CellType
forall x. CellType -> Rep CellType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CellType -> Rep CellType x
from :: forall x. CellType -> Rep CellType x
$cto :: forall x. Rep CellType x -> CellType
to :: forall x. Rep CellType x -> CellType
Generic, Int -> CellType -> ShowS
[CellType] -> ShowS
CellType -> String
(Int -> CellType -> ShowS)
-> (CellType -> String) -> ([CellType] -> ShowS) -> Show CellType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellType -> ShowS
showsPrec :: Int -> CellType -> ShowS
$cshow :: CellType -> String
show :: CellType -> String
$cshowList :: [CellType] -> ShowS
showList :: [CellType] -> ShowS
Show, CellType -> CellType -> Bool
(CellType -> CellType -> Bool)
-> (CellType -> CellType -> Bool) -> Eq CellType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellType -> CellType -> Bool
== :: CellType -> CellType -> Bool
$c/= :: CellType -> CellType -> Bool
/= :: CellType -> CellType -> Bool
Eq, Eq CellType
Eq CellType =>
(CellType -> CellType -> Ordering)
-> (CellType -> CellType -> Bool)
-> (CellType -> CellType -> Bool)
-> (CellType -> CellType -> Bool)
-> (CellType -> CellType -> Bool)
-> (CellType -> CellType -> CellType)
-> (CellType -> CellType -> CellType)
-> Ord CellType
CellType -> CellType -> Bool
CellType -> CellType -> Ordering
CellType -> CellType -> CellType
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 :: CellType -> CellType -> Ordering
compare :: CellType -> CellType -> Ordering
$c< :: CellType -> CellType -> Bool
< :: CellType -> CellType -> Bool
$c<= :: CellType -> CellType -> Bool
<= :: CellType -> CellType -> Bool
$c> :: CellType -> CellType -> Bool
> :: CellType -> CellType -> Bool
$c>= :: CellType -> CellType -> Bool
>= :: CellType -> CellType -> Bool
$cmax :: CellType -> CellType -> CellType
max :: CellType -> CellType -> CellType
$cmin :: CellType -> CellType -> CellType
min :: CellType -> CellType -> CellType
Ord)

data CellMarker = CellMarker
  { CellMarker -> Text
id :: Text,
    CellMarker -> CellType
cellType :: CellType,
    CellMarker -> Maybe UnembeddedMimeAttachments
attachments :: Maybe UnembeddedMimeAttachments
  }
  deriving ((forall x. CellMarker -> Rep CellMarker x)
-> (forall x. Rep CellMarker x -> CellMarker) -> Generic CellMarker
forall x. Rep CellMarker x -> CellMarker
forall x. CellMarker -> Rep CellMarker x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CellMarker -> Rep CellMarker x
from :: forall x. CellMarker -> Rep CellMarker x
$cto :: forall x. Rep CellMarker x -> CellMarker
to :: forall x. Rep CellMarker x -> CellMarker
Generic, Int -> CellMarker -> ShowS
[CellMarker] -> ShowS
CellMarker -> String
(Int -> CellMarker -> ShowS)
-> (CellMarker -> String)
-> ([CellMarker] -> ShowS)
-> Show CellMarker
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellMarker -> ShowS
showsPrec :: Int -> CellMarker -> ShowS
$cshow :: CellMarker -> String
show :: CellMarker -> String
$cshowList :: [CellMarker] -> ShowS
showList :: [CellMarker] -> ShowS
Show, CellMarker -> CellMarker -> Bool
(CellMarker -> CellMarker -> Bool)
-> (CellMarker -> CellMarker -> Bool) -> Eq CellMarker
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellMarker -> CellMarker -> Bool
== :: CellMarker -> CellMarker -> Bool
$c/= :: CellMarker -> CellMarker -> Bool
/= :: CellMarker -> CellMarker -> Bool
Eq, Eq CellMarker
Eq CellMarker =>
(CellMarker -> CellMarker -> Ordering)
-> (CellMarker -> CellMarker -> Bool)
-> (CellMarker -> CellMarker -> Bool)
-> (CellMarker -> CellMarker -> Bool)
-> (CellMarker -> CellMarker -> Bool)
-> (CellMarker -> CellMarker -> CellMarker)
-> (CellMarker -> CellMarker -> CellMarker)
-> Ord CellMarker
CellMarker -> CellMarker -> Bool
CellMarker -> CellMarker -> Ordering
CellMarker -> CellMarker -> CellMarker
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 :: CellMarker -> CellMarker -> Ordering
compare :: CellMarker -> CellMarker -> Ordering
$c< :: CellMarker -> CellMarker -> Bool
< :: CellMarker -> CellMarker -> Bool
$c<= :: CellMarker -> CellMarker -> Bool
<= :: CellMarker -> CellMarker -> Bool
$c> :: CellMarker -> CellMarker -> Bool
> :: CellMarker -> CellMarker -> Bool
$c>= :: CellMarker -> CellMarker -> Bool
>= :: CellMarker -> CellMarker -> Bool
$cmax :: CellMarker -> CellMarker -> CellMarker
max :: CellMarker -> CellMarker -> CellMarker
$cmin :: CellMarker -> CellMarker -> CellMarker
min :: CellMarker -> CellMarker -> CellMarker
Ord)

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

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

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

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

instance Aeson.ToJSON CellMarker

instance Aeson.FromJSON CellMarker

jsonOptions :: Aeson.Options
jsonOptions :: Options
jsonOptions =
  Options
Aeson.defaultOptions
    { sumEncoding =
        Aeson.TaggedObject
          { tagFieldName = "type",
            contentsFieldName = "value"
          },
      omitNothingFields = True, -- So that `attachments: null` doesn't show up everywhere.
      constructorTagModifier = \case
        String
"Markdown" -> String
"markdown"
        String
"Raw" -> String
"raw"
        String
"Code" -> String
"code"
        String
other -> String
other
    }