{- |
Module: Pollock.Documentation.MetadataAndDoc
Copyright: (c) Trevis Elser 2023
License: MIT
Maintainer: trevis@flipstone.com
Stability: experimental
-}
module Pollock.Documentation.MetadataAndDoc
  ( MetaAndDoc (..)
  , withEmptyMetadata
  , metaAndDocConcat
  , metaAndDocAppend
  ) where

import Pollock.Documentation.Doc (Doc (DocEmpty), docAppend)
import Pollock.Documentation.Metadata
  ( Metadata
  , emptyMetadata
  , metaAppend
  )

data MetaAndDoc = MetaAndDoc
  { MetaAndDoc -> Metadata
meta :: !Metadata
  , MetaAndDoc -> Doc
doc :: !Doc
  }

withEmptyMetadata :: Doc -> MetaAndDoc
withEmptyMetadata :: Doc -> MetaAndDoc
withEmptyMetadata Doc
d =
  MetaAndDoc
    { meta :: Metadata
meta = Metadata
emptyMetadata
    , doc :: Doc
doc = Doc
d
    }

metaAndDocConcat :: [MetaAndDoc] -> MetaAndDoc
metaAndDocConcat :: [MetaAndDoc] -> MetaAndDoc
metaAndDocConcat = (MetaAndDoc -> MetaAndDoc -> MetaAndDoc)
-> MetaAndDoc -> [MetaAndDoc] -> MetaAndDoc
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr MetaAndDoc -> MetaAndDoc -> MetaAndDoc
metaAndDocAppend MetaAndDoc
emptyMetaAndDoc

-- Append where for metadata prefence is on the right, but for doc it is on the left.
metaAndDocAppend :: MetaAndDoc -> MetaAndDoc -> MetaAndDoc
metaAndDocAppend :: MetaAndDoc -> MetaAndDoc -> MetaAndDoc
metaAndDocAppend MetaAndDoc
md1 MetaAndDoc
md2 =
  MetaAndDoc
    { meta :: Metadata
meta = Metadata -> Metadata -> Metadata
metaAppend (MetaAndDoc -> Metadata
meta MetaAndDoc
md2) (MetaAndDoc -> Metadata
meta MetaAndDoc
md1)
    , doc :: Doc
doc = Doc -> Doc -> Doc
docAppend (MetaAndDoc -> Doc
doc MetaAndDoc
md1) (MetaAndDoc -> Doc
doc MetaAndDoc
md2)
    }

emptyMetaAndDoc :: MetaAndDoc
emptyMetaAndDoc :: MetaAndDoc
emptyMetaAndDoc = MetaAndDoc{meta :: Metadata
meta = Metadata
emptyMetadata, doc :: Doc
doc = Doc
DocEmpty}