{-# Language OverloadedStrings, GADTs #-}
{-|
Module      : Toml.Pretty
Description : Human-readable representations for error messages
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

This module provides human-readable renderers for types used
in this package to assist error message production.

The generated 'Doc' values are annotated with 'DocClass' values
to assist in producing syntax-highlighted outputs.

To extract a plain String representation, use 'show'.

-}
module Toml.Pretty (
    -- * Types
    TomlDoc,
    DocClass(..),

    -- * Printing semantic values
    prettyToml,
    prettyTomlOrdered,
    prettyValue,

    -- * Printing syntactic components
    prettyToken,
    prettySectionKind,

    -- * Printing keys
    prettySimpleKey,
    prettyKey,

    -- * Pretty errors
    prettySemanticError,
    prettyMatchMessage,
    prettyLocated,
    ) where

import Data.Char (ord, isAsciiLower, isAsciiUpper, isDigit, isPrint)
import Data.Foldable (fold)
import Data.List (partition, sortOn)
import Data.List.NonEmpty (NonEmpty)
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map qualified as Map
import Data.String (fromString)
import Data.Time (ZonedTime(zonedTimeZone), TimeZone (timeZoneMinutes))
import Data.Time.Format (formatTime, defaultTimeLocale)
import Prettyprinter
import Text.Printf (printf)
import Toml.FromValue.Matcher (MatchMessage(..), Scope (..))
import Toml.Lexer (Token(..))
import Toml.Located (Located(..))
import Toml.Parser.Types (SectionKind(..))
import Toml.Position (Position(..))
import Toml.Semantics (SemanticError (..), SemanticErrorKind (..))
import Toml.Value (Value(..), Table)

-- | Annotation used to enable styling pretty-printed TOML
data DocClass
    = TableClass  -- ^ top-level @[key]@ and @[[key]]@
    | KeyClass    -- ^ dotted keys, left-hand side of assignments
    | StringClass -- ^ string literals
    | NumberClass -- ^ number literals
    | DateClass   -- ^ date and time literals
    | BoolClass   -- ^ boolean literals
    deriving (ReadPrec [DocClass]
ReadPrec DocClass
Int -> ReadS DocClass
ReadS [DocClass]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DocClass]
$creadListPrec :: ReadPrec [DocClass]
readPrec :: ReadPrec DocClass
$creadPrec :: ReadPrec DocClass
readList :: ReadS [DocClass]
$creadList :: ReadS [DocClass]
readsPrec :: Int -> ReadS DocClass
$creadsPrec :: Int -> ReadS DocClass
Read, Int -> DocClass -> String -> String
[DocClass] -> String -> String
DocClass -> String
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [DocClass] -> String -> String
$cshowList :: [DocClass] -> String -> String
show :: DocClass -> String
$cshow :: DocClass -> String
showsPrec :: Int -> DocClass -> String -> String
$cshowsPrec :: Int -> DocClass -> String -> String
Show, DocClass -> DocClass -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DocClass -> DocClass -> Bool
$c/= :: DocClass -> DocClass -> Bool
== :: DocClass -> DocClass -> Bool
$c== :: DocClass -> DocClass -> Bool
Eq, Eq DocClass
DocClass -> DocClass -> Bool
DocClass -> DocClass -> Ordering
DocClass -> DocClass -> DocClass
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
min :: DocClass -> DocClass -> DocClass
$cmin :: DocClass -> DocClass -> DocClass
max :: DocClass -> DocClass -> DocClass
$cmax :: DocClass -> DocClass -> DocClass
>= :: DocClass -> DocClass -> Bool
$c>= :: DocClass -> DocClass -> Bool
> :: DocClass -> DocClass -> Bool
$c> :: DocClass -> DocClass -> Bool
<= :: DocClass -> DocClass -> Bool
$c<= :: DocClass -> DocClass -> Bool
< :: DocClass -> DocClass -> Bool
$c< :: DocClass -> DocClass -> Bool
compare :: DocClass -> DocClass -> Ordering
$ccompare :: DocClass -> DocClass -> Ordering
Ord)

-- | Pretty-printer document with TOML class attributes to aid
-- in syntax-highlighting.
type TomlDoc = Doc DocClass

-- | Renders a dotted-key using quotes where necessary and annotated
-- as a 'KeyClass'.
prettyKey :: NonEmpty String -> TomlDoc
prettyKey :: NonEmpty String -> Doc DocClass
prettyKey = forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
KeyClass forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> NonEmpty a -> NonEmpty a
NonEmpty.intersperse forall ann. Doc ann
dot forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. String -> Doc a
prettySimpleKey

-- | Renders a simple-key using quotes where necessary.
prettySimpleKey :: String -> Doc a
prettySimpleKey :: forall a. String -> Doc a
prettySimpleKey String
str
    | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
str), forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isBareKey String
str = forall a. IsString a => String -> a
fromString String
str
    | Bool
otherwise                         = forall a. IsString a => String -> a
fromString (String -> String
quoteString String
str)

-- | Predicate for the character-class that is allowed in bare keys
isBareKey :: Char -> Bool
isBareKey :: Char -> Bool
isBareKey Char
x = Char -> Bool
isAsciiLower Char
x Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
x Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
x Bool -> Bool -> Bool
|| Char
x forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
x forall a. Eq a => a -> a -> Bool
== Char
'_'

-- | Quote a string using basic string literal syntax.
quoteString :: String -> String
quoteString :: String -> String
quoteString = (Char
'"'forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
go
    where
        go :: String -> String
go = \case
            String
""        -> String
"\"" -- terminator
            Char
'"'  : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'"'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\\' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'\\' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\b' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'b'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\f' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'f'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\n' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'n'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\r' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'r'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\t' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
't'  forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
x    : String
xs
                | Char -> Bool
isPrint Char
x     -> Char
x forall a. a -> [a] -> [a]
: String -> String
go String
xs
                | Char
x forall a. Ord a => a -> a -> Bool
<= Char
'\xffff' -> forall r. PrintfType r => String -> r
printf String
"\\u%04X%s" (Char -> Int
ord Char
x) (String -> String
go String
xs)
                | Bool
otherwise     -> forall r. PrintfType r => String -> r
printf String
"\\U%08X%s" (Char -> Int
ord Char
x) (String -> String
go String
xs)

-- | Quote a string using basic string literal syntax.
quoteMlString :: String -> String
quoteMlString :: String -> String
quoteMlString = (String
"\"\"\"\n"forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
go
    where
        go :: String -> String
go = \case
            String
"" -> String
"\"\"\"" -- terminator
            Char
'"' : Char
'"' : Char
'"' : String
xs -> String
"\"\"\\\"" forall a. [a] -> [a] -> [a]
++ String -> String
go String
xs
            Char
'\\' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'\\' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\b' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'b' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\f' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'f' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\t' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
't' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\n' : String
xs -> Char
'\n' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\r' : Char
'\n' : String
xs -> Char
'\r' forall a. a -> [a] -> [a]
: Char
'\n' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
'\r' : String
xs -> Char
'\\' forall a. a -> [a] -> [a]
: Char
'r' forall a. a -> [a] -> [a]
: String -> String
go String
xs
            Char
x    : String
xs
                | Char -> Bool
isPrint Char
x     -> Char
x forall a. a -> [a] -> [a]
: String -> String
go String
xs
                | Char
x forall a. Ord a => a -> a -> Bool
<= Char
'\xffff' -> forall r. PrintfType r => String -> r
printf String
"\\u%04X%s" (Char -> Int
ord Char
x) (String -> String
go String
xs)
                | Bool
otherwise     -> forall r. PrintfType r => String -> r
printf String
"\\U%08X%s" (Char -> Int
ord Char
x) (String -> String
go String
xs)

-- | Pretty-print a section heading. The result is annotated as a 'TableClass'.
prettySectionKind :: SectionKind -> NonEmpty String -> TomlDoc
prettySectionKind :: SectionKind -> NonEmpty String -> Doc DocClass
prettySectionKind SectionKind
TableKind      NonEmpty String
key =
    forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
TableClass (forall ann xxx. Doc ann -> Doc xxx
unAnnotate (forall ann. Doc ann
lbracket forall a. Semigroup a => a -> a -> a
<> NonEmpty String -> Doc DocClass
prettyKey NonEmpty String
key forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
rbracket))
prettySectionKind SectionKind
ArrayTableKind NonEmpty String
key =
    forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
TableClass (forall ann xxx. Doc ann -> Doc xxx
unAnnotate (forall ann. Doc ann
lbracket forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
lbracket forall a. Semigroup a => a -> a -> a
<> NonEmpty String -> Doc DocClass
prettyKey NonEmpty String
key forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
rbracket forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
rbracket))

-- | Render token for human-readable error messages.
prettyToken :: Token -> String
prettyToken :: Token -> String
prettyToken = \case
    Token
TokComma            -> String
"','"
    Token
TokEquals           -> String
"'='"
    Token
TokPeriod           -> String
"'.'"
    Token
TokSquareO          -> String
"'['"
    Token
TokSquareC          -> String
"']'"
    Token
Tok2SquareO         -> String
"'[['"
    Token
Tok2SquareC         -> String
"']]'"
    Token
TokCurlyO           -> String
"'{'"
    Token
TokCurlyC           -> String
"'}'"
    Token
TokNewline          -> String
"end-of-line"
    TokBareKey        String
_ -> String
"bare key"
    Token
TokTrue             -> String
"true literal"
    Token
TokFalse            -> String
"false literal"
    TokString         String
_ -> String
"string"
    TokMlString       String
_ -> String
"multi-line string"
    TokInteger        Integer
_ -> String
"integer"
    TokFloat          Double
_ -> String
"float"
    TokOffsetDateTime ZonedTime
_ -> String
"offset date-time"
    TokLocalDateTime  LocalTime
_ -> String
"local date-time"
    TokLocalDate      Day
_ -> String
"local date"
    TokLocalTime      TimeOfDay
_ -> String
"local time"
    Token
TokEOF              -> String
"end-of-input"

prettyAssignment :: String -> Value -> TomlDoc
prettyAssignment :: String -> Value -> Doc DocClass
prettyAssignment = NonEmpty String -> Value -> Doc DocClass
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
    where
        go :: NonEmpty String -> Value -> Doc DocClass
go NonEmpty String
ks (Table (forall k a. Map k a -> [(k, a)]
Map.assocs -> [(String
k,Value
v)])) = NonEmpty String -> Value -> Doc DocClass
go (forall a. a -> NonEmpty a -> NonEmpty a
NonEmpty.cons String
k NonEmpty String
ks) Value
v
        go NonEmpty String
ks Value
v = NonEmpty String -> Doc DocClass
prettyKey (forall a. NonEmpty a -> NonEmpty a
NonEmpty.reverse NonEmpty String
ks) forall ann. Doc ann -> Doc ann -> Doc ann
<+> forall ann. Doc ann
equals forall ann. Doc ann -> Doc ann -> Doc ann
<+> Value -> Doc DocClass
prettyValue Value
v

-- | Render a value suitable for assignment on the right-hand side
-- of an equals sign. This value will always use inline table and list
-- syntax.
prettyValue :: Value -> TomlDoc
prettyValue :: Value -> Doc DocClass
prettyValue = \case
    Integer Integer
i           -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
NumberClass (forall a ann. Pretty a => a -> Doc ann
pretty Integer
i)
    Float   Double
f
        | forall a. RealFloat a => a -> Bool
isNaN Double
f       -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
NumberClass Doc DocClass
"nan"
        | forall a. RealFloat a => a -> Bool
isInfinite Double
f  -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
NumberClass (if Double
f forall a. Ord a => a -> a -> Bool
> Double
0 then Doc DocClass
"inf" else Doc DocClass
"-inf")
        | Bool
otherwise     -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
NumberClass (forall a ann. Pretty a => a -> Doc ann
pretty Double
f)
    Array [Value]
a             -> forall ann. Doc ann -> Doc ann
align (forall ann. [Doc ann] -> Doc ann
list [Value -> Doc DocClass
prettyValue Value
v | Value
v <- [Value]
a])
    Table Table
t             -> forall ann. Doc ann
lbrace forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith (forall ann. Doc ann -> Doc ann -> Doc ann -> Doc ann
surround Doc DocClass
", ") [String -> Value -> Doc DocClass
prettyAssignment String
k Value
v | (String
k,Value
v) <- forall k a. Map k a -> [(k, a)]
Map.assocs Table
t] forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
rbrace
    Bool Bool
True           -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
BoolClass Doc DocClass
"true"
    Bool Bool
False          -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
BoolClass Doc DocClass
"false"
    String String
str          -> String -> Doc DocClass
prettySmartString String
str
    TimeOfDay TimeOfDay
tod       -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
DateClass (forall a. IsString a => String -> a
fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%H:%M:%S%Q" TimeOfDay
tod))
    ZonedTime ZonedTime
zt
        | TimeZone -> Int
timeZoneMinutes (ZonedTime -> TimeZone
zonedTimeZone ZonedTime
zt) forall a. Eq a => a -> a -> Bool
== Int
0 ->
                           forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
DateClass (forall a. IsString a => String -> a
fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%0Y-%m-%dT%H:%M:%S%QZ" ZonedTime
zt))
        | Bool
otherwise     -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
DateClass (forall a. IsString a => String -> a
fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%0Y-%m-%dT%H:%M:%S%Q%Ez" ZonedTime
zt))
    LocalTime LocalTime
lt        -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
DateClass (forall a. IsString a => String -> a
fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%0Y-%m-%dT%H:%M:%S%Q" LocalTime
lt))
    Day Day
d               -> forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
DateClass (forall a. IsString a => String -> a
fromString (forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
defaultTimeLocale String
"%0Y-%m-%d" Day
d))

prettySmartString :: String -> TomlDoc
prettySmartString :: String -> Doc DocClass
prettySmartString String
str
    | Char
'\n' forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
str =
        forall ann. (Int -> Doc ann) -> Doc ann
column \Int
i ->
        forall ann. (PageWidth -> Doc ann) -> Doc ann
pageWidth \case
            AvailablePerLine Int
n Double
_ | forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str forall a. Ord a => a -> a -> Bool
> Int
n forall a. Num a => a -> a -> a
- Int
i ->
                String -> Doc DocClass
prettyMlString String
str
            PageWidth
_ -> String -> Doc DocClass
prettyString String
str
    | Bool
otherwise = String -> Doc DocClass
prettyString String
str

prettyMlString :: String -> TomlDoc
prettyMlString :: String -> Doc DocClass
prettyMlString String
str = forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
StringClass (forall ann. (Int -> Doc ann) -> Doc ann
column \Int
i -> forall ann. Int -> Doc ann -> Doc ann
hang (-Int
i) (forall a. IsString a => String -> a
fromString (String -> String
quoteMlString String
str)))

prettyString :: String -> TomlDoc
prettyString :: String -> Doc DocClass
prettyString String
str = forall ann. ann -> Doc ann -> Doc ann
annotate DocClass
StringClass (forall a. IsString a => String -> a
fromString (String -> String
quoteString String
str))

-- | Predicate for values that CAN rendered on the
-- righthand-side of an @=@.
isSimple :: Value -> Bool
isSimple :: Value -> Bool
isSimple = \case
    Integer   Integer
_ -> Bool
True
    Float     Double
_ -> Bool
True
    Bool      Bool
_ -> Bool
True
    String    String
_ -> Bool
True
    TimeOfDay TimeOfDay
_ -> Bool
True
    ZonedTime ZonedTime
_ -> Bool
True
    LocalTime LocalTime
_ -> Bool
True
    Day       Day
_ -> Bool
True
    Table     Table
x -> Table -> Bool
isSingularTable Table
x -- differs from isAlwaysSimple
    Array     [Value]
x -> forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Value]
x Bool -> Bool -> Bool
|| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Value -> Bool
isTable [Value]
x)

-- | Predicate for values that can be MUST rendered on the
-- righthand-side of an @=@.
isAlwaysSimple :: Value -> Bool
isAlwaysSimple :: Value -> Bool
isAlwaysSimple = \case
    Integer   Integer
_ -> Bool
True
    Float     Double
_ -> Bool
True
    Bool      Bool
_ -> Bool
True
    String    String
_ -> Bool
True
    TimeOfDay TimeOfDay
_ -> Bool
True
    ZonedTime ZonedTime
_ -> Bool
True
    LocalTime LocalTime
_ -> Bool
True
    Day       Day
_ -> Bool
True
    Table     Table
_ -> Bool
False -- differs from isSimple
    Array     [Value]
x -> forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Value]
x Bool -> Bool -> Bool
|| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Value -> Bool
isTable [Value]
x)

-- | Predicate for table values.
isTable :: Value -> Bool
isTable :: Value -> Bool
isTable Table {} = Bool
True
isTable Value
_        = Bool
False

-- | Predicate for tables that can be rendered with a single assignment.
-- These can be collapsed using dotted-key notation on the lefthand-side
-- of a @=@.
isSingularTable :: Table -> Bool
isSingularTable :: Table -> Bool
isSingularTable (forall k a. Map k a -> [a]
Map.elems -> [Value
v])  = Value -> Bool
isSimple Value
v
isSingularTable Table
_                   = Bool
False

-- | Render a complete TOML document using top-level table and array of
-- table sections where possible.
--
-- Keys are sorted alphabetically. To provide a custom ordering, see
-- 'prettyTomlOrdered'.
prettyToml ::
    Table {- ^ table to print -} ->
    TomlDoc {- ^ TOML syntax -}
prettyToml :: Table -> Doc DocClass
prettyToml = KeyProjection -> SectionKind -> [String] -> Table -> Doc DocClass
prettyToml_ KeyProjection
NoProjection SectionKind
TableKind []

-- | Render a complete TOML document like 'prettyToml' but use a
-- custom key ordering. The comparison function has access to the
-- complete key path. Note that only keys in the same table will
-- every be compared.
--
-- This operation allows you to render your TOML files with the
-- most important sections first. A TOML file describing a package
-- might desire to have the @[package]@ section first before any
-- of the ancilliary configuration sections.
--
-- The /table path/ is the name of the table being sorted. This allows
-- the projection to be aware of which table is being sorted.
--
-- The /key/ is the key in the table being sorted. These are the
-- keys that will be compared to each other.
--
-- Here's a projection that puts the @package@ section first, the
-- @secondary@ section second, and then all remaining cases are
-- sorted alphabetically afterward.
--
-- @
-- example :: [String] -> String -> Either Int String
-- example [] "package" = Left 1
-- example [] "second"  = Left 2
-- example _  other     = Right other
-- @
--
-- We could also put the tables in reverse-alphabetical order
-- by leveraging an existing newtype.
--
-- @
-- reverseOrderProj :: [String] -> String -> Down String
-- reverseOrderProj _ = Down
-- @
--
-- @since 1.2.1.0
prettyTomlOrdered ::
  Ord a =>
  ([String] -> String -> a) {- ^ table path -> key -> projection -} ->
  Table {- ^ table to print -} ->
  TomlDoc {- ^ TOML syntax -}
prettyTomlOrdered :: forall a.
Ord a =>
([String] -> String -> a) -> Table -> Doc DocClass
prettyTomlOrdered [String] -> String -> a
proj = KeyProjection -> SectionKind -> [String] -> Table -> Doc DocClass
prettyToml_ (forall a. Ord a => ([String] -> String -> a) -> KeyProjection
KeyProjection [String] -> String -> a
proj) SectionKind
TableKind []

-- | Optional projection used to order rendered tables
data KeyProjection where
    -- | No projection provided; alphabetical order used
    NoProjection :: KeyProjection
    -- | Projection provided: table name and current key are available
    KeyProjection :: Ord a => ([String] -> String -> a) -> KeyProjection

prettyToml_ :: KeyProjection -> SectionKind -> [String] -> Table -> TomlDoc
prettyToml_ :: KeyProjection -> SectionKind -> [String] -> Table -> Doc DocClass
prettyToml_ KeyProjection
mbKeyProj SectionKind
kind [String]
prefix Table
t = forall ann. [Doc ann] -> Doc ann
vcat ([Doc DocClass]
topLines forall a. [a] -> [a] -> [a]
++ [Doc DocClass]
subtables)
    where
        order :: [(String, Value)] -> [(String, Value)]
order =
            case KeyProjection
mbKeyProj of
                KeyProjection
NoProjection    -> forall a. a -> a
id
                KeyProjection [String] -> String -> a
f -> forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn ([String] -> String -> a
f [String]
prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst)

        kvs :: [(String, Value)]
kvs = [(String, Value)] -> [(String, Value)]
order (forall k a. Map k a -> [(k, a)]
Map.assocs Table
t)

        -- this table will require no subsequent tables to be defined
        simpleToml :: Bool
simpleToml = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Value -> Bool
isSimple Table
t

        ([(String, Value)]
simple, [(String, Value)]
sections) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Value -> Bool
isAlwaysSimple forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(String, Value)]
kvs

        topLines :: [Doc DocClass]
topLines = [forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold [Doc DocClass]
topElts | let topElts :: [Doc DocClass]
topElts = [Doc DocClass]
headers forall a. [a] -> [a] -> [a]
++ [Doc DocClass]
assignments, Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Doc DocClass]
topElts)]

        headers :: [Doc DocClass]
headers =
            case forall a. [a] -> Maybe (NonEmpty a)
NonEmpty.nonEmpty [String]
prefix of
                Just NonEmpty String
key | Bool
simpleToml Bool -> Bool -> Bool
|| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, Value)]
simple) Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, Value)]
sections Bool -> Bool -> Bool
|| SectionKind
kind forall a. Eq a => a -> a -> Bool
== SectionKind
ArrayTableKind ->
                    [SectionKind -> NonEmpty String -> Doc DocClass
prettySectionKind SectionKind
kind NonEmpty String
key forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
hardline]
                Maybe (NonEmpty String)
_ -> []

        assignments :: [Doc DocClass]
assignments = [String -> Value -> Doc DocClass
prettyAssignment String
k Value
v forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
hardline | (String
k,Value
v) <- if Bool
simpleToml then [(String, Value)]
kvs else [(String, Value)]
simple]

        subtables :: [Doc DocClass]
subtables = [[String] -> Value -> Doc DocClass
prettySection ([String]
prefix forall a. [a] -> [a] -> [a]
++ [String
k]) Value
v | Bool -> Bool
not Bool
simpleToml, (String
k,Value
v) <- [(String, Value)]
sections]

        prettySection :: [String] -> Value -> Doc DocClass
prettySection [String]
key (Table Table
tab) =
            KeyProjection -> SectionKind -> [String] -> Table -> Doc DocClass
prettyToml_ KeyProjection
mbKeyProj SectionKind
TableKind [String]
key Table
tab
        prettySection [String]
key (Array [Value]
a) =
            forall ann. [Doc ann] -> Doc ann
vcat [KeyProjection -> SectionKind -> [String] -> Table -> Doc DocClass
prettyToml_ KeyProjection
mbKeyProj SectionKind
ArrayTableKind [String]
key Table
tab | Table Table
tab <- [Value]
a]
        prettySection [String]
_ Value
_ = forall a. HasCallStack => String -> a
error String
"prettySection applied to simple value"

-- | Render a semantic TOML error in a human-readable string.
--
-- @since 1.3.0.0
prettySemanticError :: SemanticError -> String
prettySemanticError :: SemanticError -> String
prettySemanticError (SemanticError String
key SemanticErrorKind
kind) =
    forall r. PrintfType r => String -> r
printf String
"key error: %s %s" (forall a. Show a => a -> String
show (forall a. String -> Doc a
prettySimpleKey String
key))
    case SemanticErrorKind
kind of
        SemanticErrorKind
AlreadyAssigned -> String
"is already assigned" :: String
        SemanticErrorKind
ClosedTable     -> String
"is a closed table"
        SemanticErrorKind
ImplicitlyTable -> String
"is already implicitly defined to be a table"

-- | Render a TOML decoding error as a human-readable string.
--
-- @since 1.3.0.0
prettyMatchMessage :: MatchMessage -> String
prettyMatchMessage :: MatchMessage -> String
prettyMatchMessage (MatchMessage [Scope]
scope String
msg) =
    String
msg forall a. [a] -> [a] -> [a]
++ String
" in top" forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Scope -> String -> String
f String
"" [Scope]
scope
    where
        f :: Scope -> String -> String
f (ScopeIndex Int
i) = (Char
'[' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows Int
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
']'forall a. a -> [a] -> [a]
:)
        f (ScopeKey String
key) = (Char
'.' forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String -> String
shows (forall a. String -> Doc a
prettySimpleKey String
key)

prettyLocated :: Located String -> String
prettyLocated :: Located String -> String
prettyLocated (Located Position
p String
s) = forall r. PrintfType r => String -> r
printf String
"%d:%d: %s" (Position -> Int
posLine Position
p) (Position -> Int
posColumn Position
p) String
s