Copyright | (c) Eric Mertens 2023 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Use FromValue
to define a transformation from some Value
to an application
domain type.
Use ParseTable
to help build FromValue
instances that match tables. It
will make it easy to track which table keys have been used and which are left
over.
Warnings can be emitted using warning
and warnTable
(depending on what)
context you're in. These warnings can provide useful feedback about
problematic decodings or keys that might be unused now but were perhaps
meaningful in an old version of a configuration file.
Toml.FromValue.Generic can be used to derive instances of FromValue
automatically for record types.
Synopsis
- class FromValue a where
- data Matcher a
- data Result a
- warning :: String -> Matcher ()
- data ParseTable a
- runParseTable :: ParseTable a -> Table -> Matcher a
- parseTableFromValue :: ParseTable a -> Value -> Matcher a
- optKey :: FromValue a => String -> ParseTable (Maybe a)
- reqKey :: FromValue a => String -> ParseTable a
- warnTable :: String -> ParseTable ()
- data KeyAlt a
- pickKey :: [KeyAlt a] -> ParseTable a
- getTable :: ParseTable Table
- setTable :: Table -> ParseTable ()
- liftMatcher :: Matcher a -> ParseTable a
Deserialization classes
class FromValue a where Source #
Class for types that can be decoded from a TOML value.
fromValue :: Value -> Matcher a Source #
Convert a Value
or report an error message
listFromValue :: Value -> Matcher [a] Source #
Used to implement instance for '[]'. Most implementations rely on the default implementation.
Instances
Matcher
Computations that result in a Result
and which track a list
of nested contexts to assist in generating warnings and error
messages.
Computation outcome with error and warning messages. Multiple error messages can occur when multiple alternatives all fail. Resolving any one of the error messages could allow the computation to succeed.
Table matching
data ParseTable a Source #
A Matcher
that tracks a current set of unmatched key-value
pairs from a table.
Use optKey
and reqKey
to extract keys.
Use getTable
and setTable
to override the table and implement
other primitives.
Instances
runParseTable :: ParseTable a -> Table -> Matcher a Source #
Run a ParseTable
computation with a given starting Table
.
Unused tables will generate a warning. To change this behavior
getTable
and setTable
can be used to discard or generate
error messages.
parseTableFromValue :: ParseTable a -> Value -> Matcher a Source #
Used to derive a fromValue
implementation from a ParseTable
matcher.
optKey :: FromValue a => String -> ParseTable (Maybe a) Source #
Match a table entry by key if it exists or return Nothing
if not.
reqKey :: FromValue a => String -> ParseTable a Source #
Match a table entry by key or report an error if missing.
warnTable :: String -> ParseTable () Source #
Emit a warning at the current location.
Key and value matching function
Since: 1.2.0.0
pickKey :: [KeyAlt a] -> ParseTable a Source #
Take the first option from a list of table keys and matcher functions. This operation will commit to the first table key that matches. If the associated matcher fails, only that error will be propagated and the other alternatives will not be matched.
If no keys match, an error message is generated explaining which keys would have been accepted.
This is provided as an alternative to chaining multiple reqKey
cases
together with (
because that will generate one error message for
each unmatched alternative as well as the error associate with the
matched alternative.<|>
)
Since: 1.2.0.0
Table matching primitives
getTable :: ParseTable Table Source #
Return the remaining portion of the table being matched.
setTable :: Table -> ParseTable () Source #
Replace the remaining portion of the table being matched.
liftMatcher :: Matcher a -> ParseTable a Source #
Lift a matcher into the current table parsing context.