Copyright | (c) Eric Mertens 2024 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Toml.Schema
Description
Synopsis
- class FromValue a where
- mapOf :: Ord k => (l -> Text -> Matcher l k) -> (Text -> Value' l -> Matcher l v) -> Value' l -> Matcher l (Map k v)
- listOf :: (Int -> Value' l -> Matcher l a) -> Value' l -> Matcher l [a]
- data Matcher l a
- runMatcher :: Matcher l a -> Result (MatchMessage l) a
- runMatcherFatalWarn :: Matcher l a -> Either [MatchMessage l] a
- runMatcherIgnoreWarn :: Matcher l a -> Either [MatchMessage l] a
- data Result e a
- data MatchMessage a = MatchMessage {}
- data Scope
- parseTableFromValue :: ParseTable l a -> Value' l -> Matcher l a
- parseTable :: ParseTable l a -> l -> Table' l -> Matcher l a
- getScope :: Matcher a [Scope]
- warn :: String -> Matcher a ()
- warnAt :: l -> String -> Matcher l ()
- failAt :: l -> String -> Matcher l a
- getTable :: ParseTable l (Table' l)
- setTable :: Table' l -> ParseTable l ()
- data ParseTable l a
- reqKey :: FromValue a => Text -> ParseTable l a
- optKey :: FromValue a => Text -> ParseTable l (Maybe a)
- reqKeyOf :: Text -> (Value' l -> Matcher l a) -> ParseTable l a
- optKeyOf :: Text -> (Value' l -> Matcher l a) -> ParseTable l (Maybe a)
- pickKey :: [KeyAlt l a] -> ParseTable l a
- data KeyAlt l a
- warnTable :: String -> ParseTable l ()
- warnTableAt :: l -> String -> ParseTable l ()
- failTableAt :: l -> String -> ParseTable l a
- liftMatcher :: Matcher l a -> ParseTable l a
- class ToValue a where
- toValue :: a -> Value
- toValueList :: [a] -> Value
- class ToValue a => ToTable a where
- table :: [(Text, Value)] -> Table
- (.=) :: ToValue a => Text -> a -> (Text, Value)
- defaultTableToValue :: ToTable a => a -> Value
- type Value = Value' ()
- data Value' a where
- Integer' a Integer
- Double' a Double
- List' a [Value' a]
- Table' a (Table' a)
- Bool' a Bool
- Text' a Text
- TimeOfDay' a TimeOfDay
- ZonedTime' a ZonedTime
- LocalTime' a LocalTime
- Day' a Day
- pattern Bool :: Bool -> Value
- pattern Double :: Double -> Value
- pattern List :: [Value] -> Value
- pattern Integer :: Integer -> Value
- pattern Text :: Text -> Value
- pattern Table :: Table -> Value
- pattern Day :: Day -> Value
- pattern TimeOfDay :: TimeOfDay -> Value
- pattern LocalTime :: LocalTime -> Value
- pattern ZonedTime :: ZonedTime -> Value
- type Table = Table' ()
- newtype Table' a = MkTable (Map Text (a, Value' a))
- newtype GenericTomlArray a = GenericTomlArray a
- newtype GenericTomlTable a = GenericTomlTable a
- genericFromTable :: (Generic a, GParseTable (Rep a)) => Value' l -> Matcher l a
- genericFromArray :: (Generic a, GFromArray (Rep a)) => Value' l -> Matcher l a
- genericToArray :: (Generic a, GToArray (Rep a)) => a -> Value
- genericToTable :: (Generic a, GToTable (Rep a)) => a -> Table
FromValue
Class for types that can be decoded from a TOML value.
Minimal complete definition
Methods
fromValue :: Value' l -> Matcher l a #
Convert a Value
or report an error message
listFromValue :: Value' l -> Matcher l [a] #
Used to implement instance for []
. Most implementations rely on the default implementation.
Instances
FromValue Int16 # | |
FromValue Int32 # | |
FromValue Int64 # | |
FromValue Int8 # | |
FromValue Word16 # | |
FromValue Word32 # | |
FromValue Word64 # | |
FromValue Word8 # | |
FromValue Text # | Matches string literals |
FromValue Text # | Matches string literals |
FromValue Day # | Matches local date literals |
FromValue UTCTime # | Matches offset date-time literals and converts to UTC |
FromValue LocalTime # | Matches local date-time literals |
FromValue TimeOfDay # | Matches local time literals |
FromValue ZonedTime # | Matches offset date-time literals |
FromValue Table # | |
FromValue Value # | Matches all values, used for pass-through |
FromValue Integer # | Matches integer values |
FromValue Natural # | Matches non-negative integer values |
FromValue Bool # | Matches |
FromValue Char # | Matches single-character strings with |
FromValue Double # | Matches floating-point and integer values |
FromValue Float # | Matches floating-point and integer values |
FromValue Int # | |
FromValue Word # | |
FromValue a => FromValue (Seq a) # | Matches arrays |
FromValue a => FromValue (NonEmpty a) # | Matches non-empty arrays or reports an error. |
Integral a => FromValue (Ratio a) # | Matches floating-point and integer values. TOML specifies |
(Generic a, GFromArray (Rep a)) => FromValue (GenericTomlArray a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlArray a) # listFromValue :: Value' l -> Matcher l [GenericTomlArray a] # | |
(Generic a, GParseTable (Rep a)) => FromValue (GenericTomlTable a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlTable a) # listFromValue :: Value' l -> Matcher l [GenericTomlTable a] # | |
FromValue a => FromValue [a] # | Implemented in terms of |
Defined in Toml.Schema.FromValue | |
(Ord k, FromKey k, FromValue v) => FromValue (Map k v) # | |
Arguments
:: Ord k | |
=> (l -> Text -> Matcher l k) | key matcher |
-> (Text -> Value' l -> Matcher l v) | value matcher |
-> Value' l | |
-> Matcher l (Map k v) |
Table matching function used to help implement fromValue
for tables.
Key matching function is given the annotation of the key for error reporting.
Value matching function is given the key in case values can depend on their keys.
listOf :: (Int -> Value' l -> Matcher l a) -> Value' l -> Matcher l [a] #
List matching function used to help implemented fromValue
for arrays.
The element matching function is given the list index in case values can
depend on their index.
Matcher
Computations that result in a Result
and which track a list
of nested contexts to assist in generating warnings and error
messages.
Instances
Alternative (Matcher a) # | |
Applicative (Matcher a) # | |
Defined in Toml.Schema.Matcher | |
Functor (Matcher a) # | |
Monad (Matcher a) # | |
MonadPlus (Matcher a) # | |
MonadFail (Matcher a) # | Fail with an error message without an annotation. |
Defined in Toml.Schema.Matcher |
runMatcher :: Matcher l a -> Result (MatchMessage l) a #
Run a Matcher
with an empty scope.
runMatcherFatalWarn :: Matcher l a -> Either [MatchMessage l] a #
Run Matcher
and treat warnings as errors.
runMatcherIgnoreWarn :: Matcher l a -> Either [MatchMessage l] a #
Run Matcher
and ignore warnings.
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.
Instances
(Read e, Read a) => Read (Result e a) # | Default instance |
(Show e, Show a) => Show (Result e a) # | Default instance |
(Eq e, Eq a) => Eq (Result e a) # | Default instance |
(Ord e, Ord a) => Ord (Result e a) # | Default instance |
Defined in Toml.Schema.Matcher |
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 | |
Instances
Scopes for TOML message.
Constructors
ScopeIndex Int | zero-based array index |
ScopeKey Text | key in a table |
parseTableFromValue :: ParseTable l a -> Value' l -> Matcher l a #
Used to derive a fromValue
implementation from a ParseTable
matcher.
parseTable :: ParseTable l a -> l -> Table' l -> Matcher l a #
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.
failAt :: l -> String -> Matcher l a #
Terminate the match with an error mentioning the given annotation.
getTable :: ParseTable l (Table' l) #
Return the remaining portion of the table being matched.
setTable :: Table' l -> ParseTable l () #
Replace the remaining portion of the table being matched.
Tables
data ParseTable l a #
Parser 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
reqKey :: FromValue a => Text -> ParseTable l a #
Arguments
:: Text | key |
-> (Value' l -> Matcher l a) | value matcher |
-> ParseTable l a |
Match a table entry by key or report an error if missing.
See pickKey
for more complex cases.
Arguments
:: Text | key |
-> (Value' l -> Matcher l a) | value matcher |
-> ParseTable l (Maybe a) |
pickKey :: [KeyAlt l a] -> ParseTable l a #
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 Alternative
which will fall-through as a result of any failure to the next case.
Key and value matching function
warnTable :: String -> ParseTable l () #
Emit a warning without an annotation.
warnTableAt :: l -> String -> ParseTable l () #
Emit a warning with the given annotation.
failTableAt :: l -> String -> ParseTable l a #
Abort the current table matching with an error message at the given annotation.
liftMatcher :: Matcher l a -> ParseTable l a #
Lift a matcher into the current table parsing context.
ToValue
Class for types that can be embedded into Value
Minimal complete definition
Methods
Embed a single thing into a TOML value.
toValueList :: [a] -> Value #
Helper for converting a list of things into a value. This is typically left to be defined by its default implementation and exists to help define the encoding for TOML arrays.
Instances
class ToValue a => ToTable a where #
Class for things that can be embedded into a TOML table.
Implement this for things that always embed into a Table
and then
the ToValue
instance can be derived with defaultTableToValue
.
instance ToValue Example where toValue = defaultTableToValue -- Option 1: Manual instance instance ToTable Example where toTable x =table
["field1".=
field1 x, "field2".=
field2 x] -- Option 2: GHC.Generics derived instance using Toml.ToValue.Generic instance ToTable Example where toTable = genericToTable
Instances
(Generic a, GToTable (Rep a)) => ToTable (GenericTomlTable a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods toTable :: GenericTomlTable a -> Table # | |
ToTable (Table' a) # | |
Defined in Toml.Schema.ToValue | |
(ToKey k, ToValue v) => ToTable (Map k v) # | |
Defined in Toml.Schema.ToValue |
defaultTableToValue :: ToTable a => a -> Value #
Convenience function for building ToValue
instances.
Types
Semantic TOML value with all table assignments resolved.
Constructors
Integer' a Integer | |
Double' a Double | |
List' a [Value' a] | |
Table' a (Table' a) | |
Bool' a Bool | |
Text' a Text | |
TimeOfDay' a TimeOfDay | |
ZonedTime' a ZonedTime | |
LocalTime' a LocalTime | |
Day' a Day |
Bundled Patterns
pattern Bool :: Bool -> Value | |
pattern Double :: Double -> Value | |
pattern List :: [Value] -> Value | |
pattern Integer :: Integer -> Value | |
pattern Text :: Text -> Value | |
pattern Table :: Table -> Value | |
pattern Day :: Day -> Value | |
pattern TimeOfDay :: TimeOfDay -> Value | |
pattern LocalTime :: LocalTime -> Value | |
pattern ZonedTime :: ZonedTime -> Value |
Instances
Functor Value' # | Derived |
Foldable Value' # | Derived |
Defined in Toml.Semantics.Types Methods fold :: Monoid m => Value' m -> m # foldMap :: Monoid m => (a -> m) -> Value' a -> m # foldMap' :: Monoid m => (a -> m) -> Value' a -> m # foldr :: (a -> b -> b) -> b -> Value' a -> b # foldr' :: (a -> b -> b) -> b -> Value' a -> b # foldl :: (b -> a -> b) -> b -> Value' a -> b # foldl' :: (b -> a -> b) -> b -> Value' a -> b # foldr1 :: (a -> a -> a) -> Value' a -> a # foldl1 :: (a -> a -> a) -> Value' a -> a # elem :: Eq a => a -> Value' a -> Bool # maximum :: Ord a => Value' a -> a # minimum :: Ord a => Value' a -> a # | |
Traversable Value' # | Derived |
FromValue Value # | Matches all values, used for pass-through |
ToValue Value # | Identity function |
Defined in Toml.Schema.ToValue | |
() ~ a => IsString (Value' a) # | Constructs a TOML string literal. fromString = String |
Defined in Toml.Semantics.Types Methods fromString :: String -> Value' a # | |
Read a => Read (Value' a) # | Default instance |
Show a => Show (Value' a) # | Default instance |
Eq a => Eq (Value' a) # | Nearly default instance except |
A table with annotated keys and values.
Instances
Functor Table' # | Derived |
Foldable Table' # | Derived |
Defined in Toml.Semantics.Types Methods fold :: Monoid m => Table' m -> m # foldMap :: Monoid m => (a -> m) -> Table' a -> m # foldMap' :: Monoid m => (a -> m) -> Table' a -> m # foldr :: (a -> b -> b) -> b -> Table' a -> b # foldr' :: (a -> b -> b) -> b -> Table' a -> b # foldl :: (b -> a -> b) -> b -> Table' a -> b # foldl' :: (b -> a -> b) -> b -> Table' a -> b # foldr1 :: (a -> a -> a) -> Table' a -> a # foldl1 :: (a -> a -> a) -> Table' a -> a # elem :: Eq a => a -> Table' a -> Bool # maximum :: Ord a => Table' a -> a # minimum :: Ord a => Table' a -> a # | |
Traversable Table' # | Derived |
FromValue Table # | |
Read a => Read (Table' a) # | Default instance |
Show a => Show (Table' a) # | Default instance |
Eq a => Eq (Table' a) # | Default instance |
ToTable (Table' a) # | |
Defined in Toml.Schema.ToValue | |
ToValue (Table' a) # | |
Defined in Toml.Schema.ToValue |
Generics
newtype GenericTomlArray a #
Helper type to use GHC's DerivingVia extension to derive
ToValue
, ToTable
, FromValue
for any product type.
Constructors
GenericTomlArray a |
Instances
(Generic a, GFromArray (Rep a)) => FromValue (GenericTomlArray a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlArray a) # listFromValue :: Value' l -> Matcher l [GenericTomlArray a] # | |
(Generic a, GToArray (Rep a)) => ToValue (GenericTomlArray a) # | Instance derived using |
Defined in Toml.Schema.Generic |
newtype GenericTomlTable a #
Constructors
GenericTomlTable a |
Instances
(Generic a, GParseTable (Rep a)) => FromValue (GenericTomlTable a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods fromValue :: Value' l -> Matcher l (GenericTomlTable a) # listFromValue :: Value' l -> Matcher l [GenericTomlTable a] # | |
(Generic a, GToTable (Rep a)) => ToTable (GenericTomlTable a) # | Instance derived using |
Defined in Toml.Schema.Generic Methods toTable :: GenericTomlTable a -> Table # | |
(Generic a, GToTable (Rep a)) => ToValue (GenericTomlTable a) # | Instance derived from |
Defined in Toml.Schema.Generic |
genericFromTable :: (Generic a, GParseTable (Rep a)) => Value' l -> Matcher l a #
Implementation of fromValue
using genericParseTable
to derive
a match from the record field names of the target type.
genericFromArray :: (Generic a, GFromArray (Rep a)) => Value' l -> Matcher l a #
Match a Value'
as an array positionally matching field fields
of a constructor to the elements of the array.