packages feed

hydra-kernel-0.18.0: src/main/haskell/Hydra/Core/Decode/Markdown.hs

-- Note: this is an automatically generated file. Do not edit.

-- | Term decoders for hydra.core.markdown

module Hydra.Core.Decode.Markdown where

import qualified Hydra.Core.Errors as Errors
import qualified Hydra.Core.Extract.Model as ExtractModel
import qualified Hydra.Core.Graph as Graph
import qualified Hydra.Core.Lexical as Lexical
import qualified Hydra.Core.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Core.Overlay.Haskell.Lib.Maps as Maps
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Markdown as Markdown
import qualified Hydra.Core.Model as Model
import qualified Hydra.Core.Rewriting as Rewriting
import qualified Hydra.Core.Util as Util
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void

-- | Decoder for hydra.core.markdown.Block
block :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Block
block cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermInject v0 ->
        let field = Model.injectionField v0
            fname = Model.fieldName field
            fterm = Model.fieldTerm field
            variantMap =
                    Maps.fromList [
                      (Model.Name "codeBlock", (\input -> Eithers.map (\t -> Markdown.BlockCodeBlock t) (codeBlock cx input))),
                      (Model.Name "list", (\input -> Eithers.map (\t -> Markdown.BlockList t) (list cx input))),
                      (Model.Name "paragraph", (\input -> Eithers.map (\t -> Markdown.BlockParagraph t) (paragraph cx input))),
                      (
                        Model.Name "raw",
                        (\input -> Eithers.map (\t -> Markdown.BlockRaw t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralString v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected string literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input)))),
                      (Model.Name "section", (\input -> Eithers.map (\t -> Markdown.BlockSection t) (section cx input))),
                      (Model.Name "table", (\input -> Eithers.map (\t -> Markdown.BlockTable t) (table cx input)))]
        in (Optionals.match (Maps.lookup fname variantMap) (Left (Errors.DecodingError (Strings.concat [
          "no such field ",
          (Model.unName fname),
          " in union"]))) (\f -> f fterm))
      _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.CodeBlock
codeBlock :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.CodeBlock
codeBlock cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "language" (ExtractModel.decodeMaybe (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2))) fieldMap cx) (\field_language -> Eithers.bind (ExtractModel.requireField "content" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_content -> Right (Markdown.CodeBlock {
          Markdown.codeBlockLanguage = field_language,
          Markdown.codeBlockContent = field_content}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.CodeBlock")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Document
document :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Document
document cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "title" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_title -> Eithers.bind (ExtractModel.requireField "content" (ExtractModel.decodeList block) fieldMap cx) (\field_content -> Right (Markdown.Document {
          Markdown.documentTitle = field_title,
          Markdown.documentContent = field_content}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Document")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Heading
heading :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Heading
heading cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "level" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralInteger v2 -> case v2 of
              Model.IntegerValueInt32 v3 -> Right v3
              _ -> Left (Errors.DecodingError "expected int32 value")
            _ -> Left (Errors.DecodingError "expected int32 literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_level -> Eithers.bind (ExtractModel.requireField "content" (ExtractModel.decodeList inline) fieldMap cx) (\field_content -> Eithers.bind (ExtractModel.requireField "anchor" (ExtractModel.decodeMaybe (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2))) fieldMap cx) (\field_anchor -> Right (Markdown.Heading {
          Markdown.headingLevel = field_level,
          Markdown.headingContent = field_content,
          Markdown.headingAnchor = field_anchor})))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Heading")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Inline
inline :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Inline
inline cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermInject v0 ->
        let field = Model.injectionField v0
            fname = Model.fieldName field
            fterm = Model.fieldTerm field
            variantMap =
                    Maps.fromList [
                      (
                        Model.Name "code",
                        (\input -> Eithers.map (\t -> Markdown.InlineCode t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralString v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected string literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input)))),
                      (
                        Model.Name "emphasis",
                        (\input -> Eithers.map (\t -> Markdown.InlineEmphasis t) (ExtractModel.decodeList inline cx input))),
                      (Model.Name "link", (\input -> Eithers.map (\t -> Markdown.InlineLink t) (link cx input))),
                      (Model.Name "strong", (\input -> Eithers.map (\t -> Markdown.InlineStrong t) (ExtractModel.decodeList inline cx input))),
                      (
                        Model.Name "text",
                        (\input -> Eithers.map (\t -> Markdown.InlineText t) (Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
                          Model.TermLiteral v1 -> case v1 of
                            Model.LiteralString v2 -> Right v2
                            _ -> Left (Errors.DecodingError "expected string literal")
                          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx input))))]
        in (Optionals.match (Maps.lookup fname variantMap) (Left (Errors.DecodingError (Strings.concat [
          "no such field ",
          (Model.unName fname),
          " in union"]))) (\f -> f fterm))
      _ -> Left (Errors.DecodingError "expected union")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Link
link :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Link
link cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "text" (ExtractModel.decodeList inline) fieldMap cx) (\field_text -> Eithers.bind (ExtractModel.requireField "target" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_target -> Right (Markdown.Link {
          Markdown.linkText = field_text,
          Markdown.linkTarget = field_target}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Link")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.List
list :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.List
list cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "ordered" (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralBoolean v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected boolean literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2)) fieldMap cx) (\field_ordered -> Eithers.bind (ExtractModel.requireField "items" (ExtractModel.decodeList listItem) fieldMap cx) (\field_items -> Right (Markdown.List {
          Markdown.listOrdered = field_ordered,
          Markdown.listItems = field_items}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.List")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.ListItem
listItem :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.ListItem
listItem cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermWrap v0 -> Eithers.map (\b -> Markdown.ListItem b) (ExtractModel.decodeList block cx (Model.wrappedTermBody v0))
      _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Paragraph
paragraph :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Paragraph
paragraph cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "content" (ExtractModel.decodeList inline) fieldMap cx) (\field_content -> Eithers.bind (ExtractModel.requireField "anchor" (ExtractModel.decodeMaybe (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2))) fieldMap cx) (\field_anchor -> Right (Markdown.Paragraph {
          Markdown.paragraphContent = field_content,
          Markdown.paragraphAnchor = field_anchor}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Paragraph")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Section
section :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Section
section cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "heading" heading fieldMap cx) (\field_heading -> Eithers.bind (ExtractModel.requireField "content" (ExtractModel.decodeList block) fieldMap cx) (\field_content -> Eithers.bind (ExtractModel.requireField "anchor" (ExtractModel.decodeMaybe (\cx2 -> \raw2 -> Eithers.either (\err -> Left err) (\stripped2 -> case stripped2 of
          Model.TermLiteral v1 -> case v1 of
            Model.LiteralString v2 -> Right v2
            _ -> Left (Errors.DecodingError "expected string literal")
          _ -> Left (Errors.DecodingError "expected literal")) (ExtractModel.stripWithDecodingError cx2 raw2))) fieldMap cx) (\field_anchor -> Right (Markdown.Section {
          Markdown.sectionHeading = field_heading,
          Markdown.sectionContent = field_content,
          Markdown.sectionAnchor = field_anchor})))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Section")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.Table
table :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.Table
table cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermRecord v0 ->
        let fieldMap = ExtractModel.toFieldMap v0
        in (Eithers.bind (ExtractModel.requireField "header" tableRow fieldMap cx) (\field_header -> Eithers.bind (ExtractModel.requireField "rows" (ExtractModel.decodeList tableRow) fieldMap cx) (\field_rows -> Right (Markdown.Table {
          Markdown.tableHeader = field_header,
          Markdown.tableRows = field_rows}))))
      _ -> Left (Errors.DecodingError "expected a record of type hydra.core.markdown.Table")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.TableCell
tableCell :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.TableCell
tableCell cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermWrap v0 -> Eithers.map (\b -> Markdown.TableCell b) (ExtractModel.decodeList inline cx (Model.wrappedTermBody v0))
      _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)

-- | Decoder for hydra.core.markdown.TableRow
tableRow :: Graph.Graph -> Model.Term -> Either Errors.DecodingError Markdown.TableRow
tableRow cx raw =
    Eithers.either (\err -> Left err) (\stripped -> case stripped of
      Model.TermWrap v0 -> Eithers.map (\b -> Markdown.TableRow b) (ExtractModel.decodeList tableCell cx (Model.wrappedTermBody v0))
      _ -> Left (Errors.DecodingError "expected wrapped type")) (ExtractModel.stripWithDecodingError cx raw)