toml-parser-1.2.1.0: TOML 1.0.0 parser
Copyright(c) Eric Mertens 2023
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellSafe-Inferred
LanguageHaskell2010

Toml.FromValue

Description

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

Deserialization classes

class FromValue a where Source #

Class for types that can be decoded from a TOML value.

Minimal complete definition

fromValue

Methods

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

Instances details
FromValue Int16 Source # 
Instance details

Defined in Toml.FromValue

FromValue Int32 Source # 
Instance details

Defined in Toml.FromValue

FromValue Int64 Source # 
Instance details

Defined in Toml.FromValue

FromValue Int8 Source # 
Instance details

Defined in Toml.FromValue

FromValue Word16 Source # 
Instance details

Defined in Toml.FromValue

FromValue Word32 Source # 
Instance details

Defined in Toml.FromValue

FromValue Word64 Source # 
Instance details

Defined in Toml.FromValue

FromValue Word8 Source # 
Instance details

Defined in Toml.FromValue

FromValue Text Source #

Matches string literals

Instance details

Defined in Toml.FromValue

FromValue Text Source #

Matches string literals

Since: 1.2.1.0

Instance details

Defined in Toml.FromValue

FromValue Day Source #

Matches local date literals

Instance details

Defined in Toml.FromValue

FromValue LocalTime Source #

Matches local date-time literals

Instance details

Defined in Toml.FromValue

FromValue TimeOfDay Source #

Matches local time literals

Instance details

Defined in Toml.FromValue

FromValue ZonedTime Source #

Matches offset date-time literals

Instance details

Defined in Toml.FromValue

FromValue Value Source #

Matches all values, used for pass-through

Instance details

Defined in Toml.FromValue

FromValue Integer Source #

Matches integer values

Instance details

Defined in Toml.FromValue

FromValue Natural Source #

Matches non-negative integer values

Instance details

Defined in Toml.FromValue

FromValue Bool Source #

Matches true and false

Instance details

Defined in Toml.FromValue

FromValue Char Source #

Matches single-character strings with fromValue and arbitrary strings with listFromValue to support String

Instance details

Defined in Toml.FromValue

FromValue Double Source #

Matches floating-point and integer values

Since: 1.2.1.0

Instance details

Defined in Toml.FromValue

FromValue Float Source #

Matches floating-point and integer values

Instance details

Defined in Toml.FromValue

FromValue Int Source # 
Instance details

Defined in Toml.FromValue

FromValue Word Source # 
Instance details

Defined in Toml.FromValue

FromValue a => FromValue [a] Source #

Implemented in terms of listFromValue

Instance details

Defined in Toml.FromValue

(Ord k, IsString k, FromValue v) => FromValue (Map k v) Source # 
Instance details

Defined in Toml.FromValue

Matcher

data Matcher a Source #

Computations that result in a Result and which track a list of nested contexts to assist in generating warnings and error messages.

Use withScope to run a Matcher in a new, nested scope.

Instances

Instances details
MonadFail Matcher Source #

Fail with an error message annotated to the current location.

Instance details

Defined in Toml.FromValue.Matcher

Methods

fail :: String -> Matcher a #

Alternative Matcher Source # 
Instance details

Defined in Toml.FromValue.Matcher

Methods

empty :: Matcher a #

(<|>) :: Matcher a -> Matcher a -> Matcher a #

some :: Matcher a -> Matcher [a] #

many :: Matcher a -> Matcher [a] #

Applicative Matcher Source # 
Instance details

Defined in Toml.FromValue.Matcher

Methods

pure :: a -> Matcher a #

(<*>) :: Matcher (a -> b) -> Matcher a -> Matcher b #

liftA2 :: (a -> b -> c) -> Matcher a -> Matcher b -> Matcher c #

(*>) :: Matcher a -> Matcher b -> Matcher b #

(<*) :: Matcher a -> Matcher b -> Matcher a #

Functor Matcher Source # 
Instance details

Defined in Toml.FromValue.Matcher

Methods

fmap :: (a -> b) -> Matcher a -> Matcher b #

(<$) :: a -> Matcher b -> Matcher a #

Monad Matcher Source # 
Instance details

Defined in Toml.FromValue.Matcher

Methods

(>>=) :: Matcher a -> (a -> Matcher b) -> Matcher b #

(>>) :: Matcher a -> Matcher b -> Matcher b #

return :: a -> Matcher a #

MonadPlus Matcher Source # 
Instance details

Defined in Toml.FromValue.Matcher

Methods

mzero :: Matcher a #

mplus :: Matcher a -> Matcher a -> Matcher a #

data Result a Source #

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.

Constructors

Failure [String]

error messages

Success [String] a

warning messages and result

Instances

Instances details
Read a => Read (Result a) Source #

Default instance

Instance details

Defined in Toml.FromValue.Matcher

Show a => Show (Result a) Source #

Default instance

Instance details

Defined in Toml.FromValue.Matcher

Methods

showsPrec :: Int -> Result a -> ShowS #

show :: Result a -> String #

showList :: [Result a] -> ShowS #

Eq a => Eq (Result a) Source #

Default instance

Instance details

Defined in Toml.FromValue.Matcher

Methods

(==) :: Result a -> Result a -> Bool #

(/=) :: Result a -> Result a -> Bool #

Ord a => Ord (Result a) Source #

Default instance

Instance details

Defined in Toml.FromValue.Matcher

Methods

compare :: Result a -> Result a -> Ordering #

(<) :: Result a -> Result a -> Bool #

(<=) :: Result a -> Result a -> Bool #

(>) :: Result a -> Result a -> Bool #

(>=) :: Result a -> Result a -> Bool #

max :: Result a -> Result a -> Result a #

min :: Result a -> Result a -> Result a #

warning :: String -> Matcher () Source #

Emit a warning mentioning the current scope.

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

Instances details
MonadFail ParseTable Source #

Implemented in terms of fail on Matcher

Instance details

Defined in Toml.FromValue.ParseTable

Methods

fail :: String -> ParseTable a #

Alternative ParseTable Source # 
Instance details

Defined in Toml.FromValue.ParseTable

Applicative ParseTable Source # 
Instance details

Defined in Toml.FromValue.ParseTable

Methods

pure :: a -> ParseTable a #

(<*>) :: ParseTable (a -> b) -> ParseTable a -> ParseTable b #

liftA2 :: (a -> b -> c) -> ParseTable a -> ParseTable b -> ParseTable c #

(*>) :: ParseTable a -> ParseTable b -> ParseTable b #

(<*) :: ParseTable a -> ParseTable b -> ParseTable a #

Functor ParseTable Source # 
Instance details

Defined in Toml.FromValue.ParseTable

Methods

fmap :: (a -> b) -> ParseTable a -> ParseTable b #

(<$) :: a -> ParseTable b -> ParseTable a #

Monad ParseTable Source # 
Instance details

Defined in Toml.FromValue.ParseTable

Methods

(>>=) :: ParseTable a -> (a -> ParseTable b) -> ParseTable b #

(>>) :: ParseTable a -> ParseTable b -> ParseTable b #

return :: a -> ParseTable a #

MonadPlus ParseTable Source # 
Instance details

Defined in Toml.FromValue.ParseTable

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.

reqKey :: FromValue a => String -> ParseTable a Source #

Convenience function for matching a required key with a FromValue instance.

reqKey key = reqKeyOf key fromValue

optKey :: FromValue a => String -> ParseTable (Maybe a) Source #

Convenience function for matching an optional key with a FromValue instance.

optKey key = optKeyOf key fromValue

reqKeyOf Source #

Arguments

:: String

key

-> (Value -> Matcher a)

value matcher

-> ParseTable a 

Match a table entry by key or report an error if missing.

See pickKey for more complex cases.

optKeyOf Source #

Arguments

:: String

key

-> (Value -> Matcher a)

value matcher

-> ParseTable (Maybe a) 

Match a table entry by key if it exists or return Nothing if not. If the key is defined, it is matched by the given function.

See pickKey for more complex cases.

warnTable :: String -> ParseTable () Source #

Emit a warning at the current location.

data KeyAlt a Source #

Key and value matching function

Since: 1.2.0.0

Constructors

Key String (Value -> Matcher a)

pick alternative based on key match

Else (Matcher a)

default case when no previous cases matched

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.