Toml.Parser.Validate

tomland-1.3.3.3: Bidirectional TOML serialization

Copyright | (c) 2018-2022 Kowainik
License | MPL-2.0
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | None
Language | Haskell2010

Toml.Parser.Validate

Contents

- Decoding
- Internal helpers

Description

This module contains functions that aggregate the result of `tomlP` parser into `TOML`. This approach allows to keep parser fast and simple and delegate the process of creating tree structure to a separate function.

Since: 1.2.0.0

Synopsis

- validateItems :: [TomlItem] -> Either ValidationError TOML
- data ValidationError
  - = DuplicateKey !Key
  - | DuplicateTable !Key
  - | SameNameKeyTable !Key
  - | SameNameTableArray !Key
- groupItems :: [TomlItem] -> Forest TomlItem
- groupWithParent :: Maybe Key -> [TomlItem] -> (Forest TomlItem, [TomlItem])
- validateItemForest :: Forest TomlItem -> Either ValidationError TOML

# Decoding

validateItems :: [TomlItem] -> Either ValidationError TOML #

Validate list of `TomlItem`s and convert to `TOML` if not validation errors are found.

data ValidationError #

Error that happens during validating TOML which is already syntactically correct. For the list of all possible validation errors and their explanation, see the following issue on GitHub:

- https://github.com/kowainik/tomland/issues/5

Constructors

DuplicateKey !Key |
DuplicateTable !Key |
SameNameKeyTable !Key |
SameNameTableArray !Key |

#### Instances

Instances details

Show ValidationError # |
Instance details Defined in Toml.Parser.Validate Methods showsPrec :: Int -> ValidationError -> ShowS # show :: ValidationError -> String # showList :: [ValidationError] -> ShowS #
Eq ValidationError # |
Instance details Defined in Toml.Parser.Validate Methods (==) :: ValidationError -> ValidationError -> Bool # (/=) :: ValidationError -> ValidationError -> Bool #

# Internal helpers

groupItems :: [TomlItem] -> Forest TomlItem #

This function takes flat list of `TomlItem`s and groups it into list of `Tree`s by putting all corresponding items inside tables and table arrays. It doesn't perform any validation, just groups items according to prefixes of their keys. So, for example, if you have the following keys as flat list:

    aaa              # ordinary key
    aaa.bbb          # ordinary key
    [foo]            # table nam
    foo.bar
    foo.baz
    [xxx]            # table name
    [xxx.yyy]        # table name
    zzz

the following tree structure will be created:

    aaa
    aaa.bbb
    [foo]
    ├──── foo.bar
    └──── foo.baz
    [xxx]
    └──── [yyy]
          └──── zzz

groupWithParent #

Arguments

:: Maybe Key | Parent name
-> [TomlItem] | List of items
-> (Forest TomlItem, [TomlItem]) | Forest of times and remaining items

This function groups list of TOML items into `Forest` and returns list of items that are not children of specified parent.

Invariant: When this function is called with `Nothing`, second element in the result tuple should be empty list.

validateItemForest :: Forest TomlItem -> Either ValidationError TOML #

Construct `TOML` from the `Forest` of `TomlItem` and performing validation of TOML at the same time.