packages feed

mdoc-0.2.0.0: src/Mdoc/Examples/Person.hs

-- |
--
-- Module      : Mdoc.Examples.Person
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Examples.Person
  ( person5
  ) where

import Mdoc.Prelude

import Autodocodec (HasCodec (..), object, optionalField, requiredField, (.=))
import Autodocodec.Schema.Mdoc (getPageViaCodec)
import Mdoc.Data.Named
import Mdoc.Data.Page
import Mdoc.Syntax

data Person = Person
  { name :: Text
  , age :: Int
  , address :: Maybe Address
  , hobbies :: Maybe [Text]
  }

{- FOURMOLU_DISABLE -}
instance HasCodec Person where
  codec = object "Person" $ Person
    <$> requiredField "name" "Their name" .= (.name)
    <*> requiredField "age" "Their age" .= (.age)
    <*> optionalField "address" "Their address" .= (.address)
    <*> optionalField "hobbies" "Their hobbies" .= (.hobbies)
{- FOURMOLU_ENABLE -}

data Address = Address
  { street :: Text
  , city :: Text
  , state :: Text
  , postalCode :: Text
  }

{- FOURMOLU_DISABLE -}
instance HasCodec Address where
  codec = object "Address" $ Address
    <$> requiredField "street" "The street" .= (.street)
    <*> requiredField "city" "The city" .= (.city)
    <*> requiredField "state" "The state" .= (.state)
    <*> requiredField "postalCode" "The postal code" .= (.postalCode)
{- FOURMOLU_ENABLE -}

person5 :: Named
person5 =
  mempty
    & (<> getPageViaCodec @Person)
    & setSynopsis
      ( Mdoc
          [ MacroLine Bl ["-tag", "-width", "indent", "-compact"]
          , MacroLine It [Callable Pa, "person.schema.json"]
          , MacroLine El []
          ]
      )
    & name "person" "person example via Codec"