packages feed

mdoc-0.1.0.2: src/Mdoc/Gen/ExitStatus.hs

-- |
--
-- Module      : Mdoc.Gen.ExitStatus
-- Copyright   : (c) 2026 Patrick Brisbin
-- License     : AGPL-3
-- Maintainer  : pbrisbin@gmail.com
-- Stability   : experimental
-- Portability : POSIX
module Mdoc.Gen.ExitStatus
  ( NonZeroStatus (..)
  , renderExitStatuses
  ) where

import Mdoc.Prelude

import Mdoc.MacroName
import Mdoc.MdocLine

data NonZeroStatus = NonZeroStatus
  { status :: Text
  -- ^ For example, @\">0\"@
  , condition :: Text
  -- ^ For example, @\"if an error occurs\"@
  }
  deriving stock (Eq, Generic, Show)

renderExitStatuses :: NonEmpty NonZeroStatus -> NonEmpty MdocLine
renderExitStatuses nzs =
  TextLine "The"
    :| concat
      [ [MacroLine Nm []]
      , [TextLine "utility exits 0 on success,"]
      , map middleLine (init nzs)
      , [finaleLine $ last nzs]
      ]
 where
  middleLine nz = TextLine $ nz.status <> " " <> nz.condition <> ","
  finaleLine nz = TextLine $ "and " <> nz.status <> " " <> nz.condition <> "."