Toml.Schema.Matcher

toml-parser-2.0.1.0: TOML 1.0.0 parser

Copyright | (c) Eric Mertens 2023
License | ISC
Maintainer | emertens@gmail.com
Safe Haskell | None
Language | Haskell2010

Toml.Schema.Matcher

Contents

- Types
- Operations
- Run helpers
- Scope helpers

Description

This type helps to build up computations that can validate a TOML value and compute some application-specific representation.

It supports warning messages which can be used to deprecate old configuration options and to detect unused table keys.

It supports tracking multiple error messages when you have more than one decoding option and all of them have failed.

Use `prettyMatchMessage` for an easy way to make human readable strings from matcher outputs.

Synopsis

- data Matcher l a
- data Result e a
  - = Failure [e]
  - | Success [e] a
- data MatchMessage a = MatchMessage {
  - matchAnn :: Maybe a
  - matchPath :: [Scope]
  - matchMessage :: String
  }
- runMatcher :: Matcher l a -> Result (MatchMessage l) a
- withScope :: Scope -> Matcher l a -> Matcher l a
- getScope :: Matcher a [Scope]
- warn :: String -> Matcher a ()
- warnAt :: l -> String -> Matcher l ()
- failAt :: l -> String -> Matcher l a
- runMatcherIgnoreWarn :: Matcher l a -> Either [MatchMessage l] a
- runMatcherFatalWarn :: Matcher l a -> Either [MatchMessage l] a
- data Scope
  - = ScopeIndex Int
  - | ScopeKey Text
- inKey :: Text -> Matcher l a -> Matcher l a
- inIndex :: Int -> Matcher l a -> Matcher l a

# Types

data Matcher l a #

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

Alternative (Matcher a) # |
Instance details Defined in Toml.Schema.Matcher Methods empty :: Matcher a a0 # (<|>) :: Matcher a a0 -> Matcher a a0 -> Matcher a a0 # some :: Matcher a a0 -> Matcher a [a0] # many :: Matcher a a0 -> Matcher a [a0] #
Applicative (Matcher a) # |
Instance details Defined in Toml.Schema.Matcher Methods pure :: a0 -> Matcher a a0 # (<*>) :: Matcher a (a0 -> b) -> Matcher a a0 -> Matcher a b # liftA2 :: (a0 -> b -> c) -> Matcher a a0 -> Matcher a b -> Matcher a c # (*>) :: Matcher a a0 -> Matcher a b -> Matcher a b # (<*) :: Matcher a a0 -> Matcher a b -> Matcher a a0 #
Functor (Matcher a) # |
Instance details Defined in Toml.Schema.Matcher Methods fmap :: (a0 -> b) -> Matcher a a0 -> Matcher a b # (<$) :: a0 -> Matcher a b -> Matcher a a0 #
Monad (Matcher a) # |
Instance details Defined in Toml.Schema.Matcher Methods (>>=) :: Matcher a a0 -> (a0 -> Matcher a b) -> Matcher a b # (>>) :: Matcher a a0 -> Matcher a b -> Matcher a b # return :: a0 -> Matcher a a0 #
MonadPlus (Matcher a) # |
Instance details Defined in Toml.Schema.Matcher Methods mzero :: Matcher a a0 # mplus :: Matcher a a0 -> Matcher a a0 -> Matcher a a0 #
MonadFail (Matcher a) # | Fail with an error message without an annotation.
Instance details Defined in Toml.Schema.Matcher Methods fail :: String -> Matcher a a0 #

data Result e a #

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 [e] | error messages
Success [e] a | warning messages and result

#### Instances

Instances details

(Read e, Read a) => Read (Result e a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods readsPrec :: Int -> ReadS (Result e a) # readList :: ReadS [Result e a] # readPrec :: ReadPrec (Result e a) # readListPrec :: ReadPrec [Result e a] #
(Show e, Show a) => Show (Result e a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods showsPrec :: Int -> Result e a -> ShowS # show :: Result e a -> String # showList :: [Result e a] -> ShowS #
(Eq e, Eq a) => Eq (Result e a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods (==) :: Result e a -> Result e a -> Bool # (/=) :: Result e a -> Result e a -> Bool #
(Ord e, Ord a) => Ord (Result e a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods compare :: Result e a -> Result e a -> Ordering # (<) :: Result e a -> Result e a -> Bool # (<=) :: Result e a -> Result e a -> Bool # (>) :: Result e a -> Result e a -> Bool # (>=) :: Result e a -> Result e a -> Bool # max :: Result e a -> Result e a -> Result e a # min :: Result e a -> Result e a -> Result e a #

data MatchMessage a #

A message emitted while matching a TOML value. The message is paired with the path to the value that was in focus when the message was generated. These message get used for both warnings and errors.

For a convenient way to render these to a string, see `prettyMatchMessage`.

Constructors

MatchMessage |
Fields matchAnn :: Maybe a matchPath :: [Scope] path to message location matchMessage :: String error and warning message body

#### Instances

Instances details

Functor MatchMessage # |
Instance details Defined in Toml.Schema.Matcher Methods fmap :: (a -> b) -> MatchMessage a -> MatchMessage b # (<$) :: a -> MatchMessage b -> MatchMessage a #
Foldable MatchMessage # |
Instance details Defined in Toml.Schema.Matcher Methods fold :: Monoid m => MatchMessage m -> m # foldMap :: Monoid m => (a -> m) -> MatchMessage a -> m # foldMap' :: Monoid m => (a -> m) -> MatchMessage a -> m # foldr :: (a -> b -> b) -> b -> MatchMessage a -> b # foldr' :: (a -> b -> b) -> b -> MatchMessage a -> b # foldl :: (b -> a -> b) -> b -> MatchMessage a -> b # foldl' :: (b -> a -> b) -> b -> MatchMessage a -> b # foldr1 :: (a -> a -> a) -> MatchMessage a -> a # foldl1 :: (a -> a -> a) -> MatchMessage a -> a # toList :: MatchMessage a -> [a] # null :: MatchMessage a -> Bool # length :: MatchMessage a -> Int # elem :: Eq a => a -> MatchMessage a -> Bool # maximum :: Ord a => MatchMessage a -> a # minimum :: Ord a => MatchMessage a -> a # sum :: Num a => MatchMessage a -> a # product :: Num a => MatchMessage a -> a #
Traversable MatchMessage # |
Instance details Defined in Toml.Schema.Matcher Methods traverse :: Applicative f => (a -> f b) -> MatchMessage a -> f (MatchMessage b) # sequenceA :: Applicative f => MatchMessage (f a) -> f (MatchMessage a) # mapM :: Monad m => (a -> m b) -> MatchMessage a -> m (MatchMessage b) # sequence :: Monad m => MatchMessage (m a) -> m (MatchMessage a) #
Read a => Read (MatchMessage a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods readsPrec :: Int -> ReadS (MatchMessage a) # readList :: ReadS [MatchMessage a] # readPrec :: ReadPrec (MatchMessage a) # readListPrec :: ReadPrec [MatchMessage a] #
Show a => Show (MatchMessage a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods showsPrec :: Int -> MatchMessage a -> ShowS # show :: MatchMessage a -> String # showList :: [MatchMessage a] -> ShowS #
Eq a => Eq (MatchMessage a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods (==) :: MatchMessage a -> MatchMessage a -> Bool # (/=) :: MatchMessage a -> MatchMessage a -> Bool #
Ord a => Ord (MatchMessage a) # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods compare :: MatchMessage a -> MatchMessage a -> Ordering # (<) :: MatchMessage a -> MatchMessage a -> Bool # (<=) :: MatchMessage a -> MatchMessage a -> Bool # (>) :: MatchMessage a -> MatchMessage a -> Bool # (>=) :: MatchMessage a -> MatchMessage a -> Bool # max :: MatchMessage a -> MatchMessage a -> MatchMessage a # min :: MatchMessage a -> MatchMessage a -> MatchMessage a #

# Operations

runMatcher :: Matcher l a -> Result (MatchMessage l) a #

Run a `Matcher` with an empty scope.

withScope :: Scope -> Matcher l a -> Matcher l a #

Run a `Matcher` with a locally extended scope.

getScope :: Matcher a [Scope] #

Get the current list of scopes.

warn :: String -> Matcher a () #

Emit a warning without an annotation.

warnAt :: l -> String -> Matcher l () #

Emit a warning mentioning the given annotation.

failAt :: l -> String -> Matcher l a #

Terminate the match with an error mentioning the given annotation.

# Run helpers

runMatcherIgnoreWarn :: Matcher l a -> Either [MatchMessage l] a #

Run `Matcher` and ignore warnings.

runMatcherFatalWarn :: Matcher l a -> Either [MatchMessage l] a #

Run `Matcher` and treat warnings as errors.

# Scope helpers

data Scope #

Scopes for TOML message.

Constructors

ScopeIndex Int | zero-based array index
ScopeKey Text | key in a table

#### Instances

Instances details

Read Scope # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods readsPrec :: Int -> ReadS Scope # readList :: ReadS [Scope] # readPrec :: ReadPrec Scope # readListPrec :: ReadPrec [Scope] #
Show Scope # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods showsPrec :: Int -> Scope -> ShowS # show :: Scope -> String # showList :: [Scope] -> ShowS #
Eq Scope # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods (==) :: Scope -> Scope -> Bool # (/=) :: Scope -> Scope -> Bool #
Ord Scope # | Default instance
Instance details Defined in Toml.Schema.Matcher Methods compare :: Scope -> Scope -> Ordering # (<) :: Scope -> Scope -> Bool # (<=) :: Scope -> Scope -> Bool # (>) :: Scope -> Scope -> Bool # (>=) :: Scope -> Scope -> Bool # max :: Scope -> Scope -> Scope # min :: Scope -> Scope -> Scope #

inKey :: Text -> Matcher l a -> Matcher l a #

Update the scope with the message corresponding to a table key

inIndex :: Int -> Matcher l a -> Matcher l a #

Update the scope with the message corresponding to an array index