toml-reader-0.3.0.0: TOML format parser compliant with v1.0.0.
Safe HaskellNone
LanguageGHC2021

TOML.Decode

Synopsis

Decoding functions

decode :: DecodeTOML a => Text -> Either TOMLError a Source #

Decode the given TOML input.

decodeWith :: Decoder a -> Text -> Either TOMLError a Source #

Decode the given TOML input using the given Decoder.

decodeWithOpts :: Decoder a -> String -> Text -> Either TOMLError a Source #

decodeFile :: DecodeTOML a => FilePath -> IO (Either TOMLError a) Source #

Decode a TOML file at the given file path.

Decoder interface

class DecodeTOML a where Source #

A type class containing the default Decoder for the given type.

See the docs for Decoder for examples.

Instances

Instances details
DecodeTOML IntSet Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder IntSet Source #

DecodeTOML Void Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Void Source #

DecodeTOML Version Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Version Source #

DecodeTOML Int16 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Int16 Source #

DecodeTOML Int32 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Int32 Source #

DecodeTOML Int64 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Int64 Source #

DecodeTOML Int8 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Int8 Source #

DecodeTOML Word16 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Word16 Source #

DecodeTOML Word32 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Word32 Source #

DecodeTOML Word64 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Word64 Source #

DecodeTOML Word8 Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Word8 Source #

DecodeTOML Ordering Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Ordering Source #

DecodeTOML Text Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Text Source #

DecodeTOML Text Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Text Source #

DecodeTOML CalendarDiffDays Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder CalendarDiffDays Source #

DecodeTOML Day Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Day Source #

DecodeTOML DayOfWeek Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder DayOfWeek Source #

DecodeTOML DiffTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder DiffTime Source #

DecodeTOML NominalDiffTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder NominalDiffTime Source #

DecodeTOML SystemTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder SystemTime Source #

DecodeTOML UTCTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder UTCTime Source #

DecodeTOML CalendarDiffTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder CalendarDiffTime Source #

DecodeTOML LocalTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder LocalTime Source #

DecodeTOML TimeOfDay Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder TimeOfDay Source #

DecodeTOML ZonedTime Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder ZonedTime Source #

DecodeTOML Value Source # 
Instance details

Defined in TOML.Decode

DecodeTOML Integer Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Integer Source #

DecodeTOML Natural Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Natural Source #

DecodeTOML String Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder String Source #

DecodeTOML () Source # 
Instance details

Defined in TOML.Decode

DecodeTOML Bool Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Bool Source #

DecodeTOML Char Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Char Source #

DecodeTOML Double Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Double Source #

DecodeTOML Float Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Float Source #

DecodeTOML Int Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Int Source #

DecodeTOML Word Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder Word Source #

DecodeTOML a => DecodeTOML (First a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (First a) Source #

DecodeTOML a => DecodeTOML (Last a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Last a) Source #

DecodeTOML a => DecodeTOML (Max a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Max a) Source #

DecodeTOML a => DecodeTOML (Min a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Min a) Source #

DecodeTOML a => DecodeTOML (IntMap a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (IntMap a) Source #

DecodeTOML a => DecodeTOML (Seq a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Seq a) Source #

(DecodeTOML a, Ord a) => DecodeTOML (Set a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Set a) Source #

DecodeTOML a => DecodeTOML (NonEmpty a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (NonEmpty a) Source #

DecodeTOML a => DecodeTOML (Identity a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Identity a) Source #

DecodeTOML a => DecodeTOML (First a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (First a) Source #

DecodeTOML a => DecodeTOML (Last a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Last a) Source #

DecodeTOML a => DecodeTOML (Dual a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Dual a) Source #

Integral a => DecodeTOML (Ratio a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Ratio a) Source #

DecodeTOML a => DecodeTOML (Maybe a) Source #

Since TOML doesn't support literal NULLs, this will only ever return Just. To get the absence of a field, use getFieldOpt or one of its variants.

Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Maybe a) Source #

DecodeTOML a => DecodeTOML [a] Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder [a] Source #

HasResolution a => DecodeTOML (Fixed a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Fixed a) Source #

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

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Map k v) Source #

(DecodeTOML a, DecodeTOML b) => DecodeTOML (Either a b) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Either a b) Source #

DecodeTOML (Proxy a) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Proxy a) Source #

(DecodeTOML a, DecodeTOML b) => DecodeTOML (a, b) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (a, b) Source #

DecodeTOML a => DecodeTOML (Const a b) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (Const a b) Source #

(DecodeTOML a, DecodeTOML b, DecodeTOML c) => DecodeTOML (a, b, c) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (a, b, c) Source #

(DecodeTOML a, DecodeTOML b, DecodeTOML c, DecodeTOML d) => DecodeTOML (a, b, c, d) Source # 
Instance details

Defined in TOML.Decode

Methods

tomlDecoder :: Decoder (a, b, c, d) Source #

newtype Decoder a Source #

A Decoder a represents a function for decoding a TOML value to a value of type a.

Generally, you'd only need to chain the getField* functions together, like

decoder =
  MyConfig
    <$> getField "a"
    <*> getField "b"
    <*> getField "c"

or use interfaces like Monad and Alternative:

decoder = do
  cfgType <- getField "type"
  case cfgType of
    "int" -> MyIntValue <$> (getField "int" <|> getField "integer")
    "bool" -> MyBoolValue <$> getField "bool"
    _ -> fail $ "Invalid type: " <> cfgType

but you can also manually implement a Decoder with makeDecoder.

Constructors

Decoder 

Fields

Instances

Instances details
Alternative Decoder Source # 
Instance details

Defined in TOML.Decode

Methods

empty :: Decoder a

(<|>) :: Decoder a -> Decoder a -> Decoder a

some :: Decoder a -> Decoder [a]

many :: Decoder a -> Decoder [a]

Applicative Decoder Source # 
Instance details

Defined in TOML.Decode

Methods

pure :: a -> Decoder a

(<*>) :: Decoder (a -> b) -> Decoder a -> Decoder b

liftA2 :: (a -> b -> c) -> Decoder a -> Decoder b -> Decoder c

(*>) :: Decoder a -> Decoder b -> Decoder b

(<*) :: Decoder a -> Decoder b -> Decoder a

Functor Decoder Source # 
Instance details

Defined in TOML.Decode

Methods

fmap :: (a -> b) -> Decoder a -> Decoder b

(<$) :: a -> Decoder b -> Decoder a

Monad Decoder Source # 
Instance details

Defined in TOML.Decode

Methods

(>>=) :: Decoder a -> (a -> Decoder b) -> Decoder b

(>>) :: Decoder a -> Decoder b -> Decoder b

return :: a -> Decoder a

MonadFail Decoder Source # 
Instance details

Defined in TOML.Decode

Methods

fail :: String -> Decoder a

Decoder getters

getField :: DecodeTOML a => Text -> Decoder a Source #

Decode a field in a TOML Value. Equivalent to getFields with a single-element list.

a = 1
b = asdf
-- MyConfig 1 "asdf"
MyConfig <$> getField "a" <*> getField "b"

getFieldOr :: DecodeTOML a => a -> Text -> Decoder a Source #

Decode a field in a TOML Value or succeed with a default value when the field is missing.

a = 1
# b is missing
-- MyConfig 1 "asdf"
MyConfig <$> getFieldOr 42 "a" <*> getFieldOr "asdf" "b"

getFields :: DecodeTOML a => [Text] -> Decoder a Source #

Decode a nested field in a TOML Value.

a.b = 1
-- MyConfig 1
MyConfig <$> getFields ["a", "b"]

getFieldOpt :: DecodeTOML a => Text -> Decoder (Maybe a) Source #

Decode a field in a TOML Value, or Nothing if the field doesn't exist. Equivalent to getFieldsOpt with a single-element list.

a = 1
-- MyConfig (Just 1) Nothing
MyConfig <$> getFieldOpt "a" <*> getFieldOpt "b"

getFieldsOpt :: DecodeTOML a => [Text] -> Decoder (Maybe a) Source #

Decode a nested field in a TOML Value, or Nothing if any of the fields don't exist.

a.b = 1
-- MyConfig (Just 1) Nothing Nothing
MyConfig
  <$> getFieldsOpt ["a", "b"]
  <*> getFieldsOpt ["a", "c"]
  <*> getFieldsOpt ["b", "c"]

getFieldWith :: Decoder a -> Text -> Decoder a Source #

Same as getField, except with the given Decoder.

getFieldsWith :: Decoder a -> [Text] -> Decoder a Source #

Same as getFields, except with the given Decoder.

getFieldOptWith :: Decoder a -> Text -> Decoder (Maybe a) Source #

Same as getFieldOpt, except with the given Decoder.

getFieldsOptWith :: Decoder a -> [Text] -> Decoder (Maybe a) Source #

Same as getFieldsOpt, except with the given Decoder.

getArrayOf :: Decoder a -> Decoder [a] Source #

Decode a list of values using the given Decoder.

[[a]]
b = 1

[[a]]
b = 2
-- MyConfig [1, 2]
MyConfig
  <$> getFieldWith (getArrayOf (getField "b")) "a"

getTableOf :: Decoder a -> Decoder (Map Text a) Source #

Decode a table of values using a given Decoder, returning the results as a Text-indexed Map.

Build custom Decoder

newtype DecodeM a Source #

The underlying decoding monad that either returns a value of type a or returns an error.

Constructors

DecodeM 

Instances

Instances details
Alternative DecodeM Source # 
Instance details

Defined in TOML.Decode

Methods

empty :: DecodeM a

(<|>) :: DecodeM a -> DecodeM a -> DecodeM a

some :: DecodeM a -> DecodeM [a]

many :: DecodeM a -> DecodeM [a]

Applicative DecodeM Source # 
Instance details

Defined in TOML.Decode

Methods

pure :: a -> DecodeM a

(<*>) :: DecodeM (a -> b) -> DecodeM a -> DecodeM b

liftA2 :: (a -> b -> c) -> DecodeM a -> DecodeM b -> DecodeM c

(*>) :: DecodeM a -> DecodeM b -> DecodeM b

(<*) :: DecodeM a -> DecodeM b -> DecodeM a

Functor DecodeM Source # 
Instance details

Defined in TOML.Decode

Methods

fmap :: (a -> b) -> DecodeM a -> DecodeM b

(<$) :: a -> DecodeM b -> DecodeM a

Monad DecodeM Source # 
Instance details

Defined in TOML.Decode

Methods

(>>=) :: DecodeM a -> (a -> DecodeM b) -> DecodeM b

(>>) :: DecodeM a -> DecodeM b -> DecodeM b

return :: a -> DecodeM a

MonadFail DecodeM Source # 
Instance details

Defined in TOML.Decode

Methods

fail :: String -> DecodeM a

makeDecoder :: (Value -> DecodeM a) -> Decoder a Source #

Manually implement a Decoder with the given function.

runDecoder :: Decoder a -> Value -> DecodeM a Source #

Run a Decoder with the given Value.

makeDecoder $ \v -> do
  a <- runDecoder decoder1 v
  b <- runDecoder decoder2 v
  return (a, b)

Satisfies

makeDecoder . runDecoder === id
runDecoder . makeDecoder === id

invalidValue :: Text -> Value -> DecodeM a Source #

Throw an error indicating that the given Value is invalid.

makeDecoder $ \v ->
  case v of
    Integer 42 -> invalidValue "We don't like this number" v
    _ -> runDecoder tomlDecoder v

-- or alternatively,
tomlDecoder >>= case
  42 -> makeDecoder $ invalidValue "We don't like this number"
  v -> pure v

typeMismatch :: Value -> DecodeM a Source #

Throw an error indicating that the given Value isn't the correct type of value.

makeDecoder $ \v ->
  case v of
    String s -> ...
    _ -> typeMismatch v

decodeFail :: Text -> DecodeM a Source #

Throw a generic failure message.