MyNixOS website logo
Description

TOML format parser compliant with v1.0.0.

TOML format parser compliant with v1.0.0. See README.md for more details.

toml-reader

GitHub Actions

TOML format parser compliant with v1.0.0 (verified with the toml-test tool).

Usage

{-# LANGUAGE OverloadedStrings #-}

import TOML (DecodeTOML, tomlDecoder, getField, decodeFile)

data MyConfig = MyConfig
  { field1 :: Int
  , field2 :: Bool
  } deriving (Show)

instance DecodeTOML MyConfig where
  tomlDecoder =
    MyConfig
      <$> getField "field1"
      <*> getField "field2"

main :: IO ()
main = do
  result <- decodeFile "config.toml"
  case result of
    Right cfg -> print (cfg :: MyConfig)
    Left e -> print e

Design decisions

  • Only supports reading, not writing, since TOML is lossy. For example, a simple a.b.c = 1 line could be written in a number of ways:

    a.b.c = 1
    a.b = { c = 1 }
    a = { b.c = 1 }
    
    [a]
    b.c = 1
    b = { c = 1 }
    
    [a.b]
    c = 1
    

    Since reading/writing isn't an idempotent operation, this library won't even pretend to provide DecodeTOML/EncodeTOML typeclasses that imply that they're inverses of each other.

    Hopefully some other toml-writer library may come along to make it easy to specify how to format your data in TOML (e.g. a combinator for table vs inlineTable), or you could use tomland.

  • This library defines DecodeTOML with an opaque Decoder a as opposed to a Value -> DecodeM a function, like aeson does. In my opinion, this makes the common case of decoding config files much more straightforward, especially around nested fields, which are much more common in TOML than JSON. e.g.

    -- aeson-like
    instance DecodeTOML MyConfig where
      decodeTOML :: Value -> DecodeM MyConfig
      decodeTOML = withObject "MyConfig" $ \o ->
        MyConfig
          <$> o .: "field1"
          <*> (o .: "field2" >>= (.: "field3"))
    
    -- with toml-parser
    instance DecodeTOML MyConfig where
      tomlDecoder :: Decoder MyConfig
      tomlDecoder =
        MyConfig
          <$> getField "field1"
          <*> getFields ["field2", "field3"]
    

    It also makes it easy to define ad-hoc decoders:

    instance DecodeTOML MyConfig where
      tomlDecoder = ...
    
    alternativeDecoder :: Decoder MyConfig
    alternativeDecoder = ...
    
    -- uses tomlDecoder
    decode "a = 1"
    
    -- uses explicit decoder
    decodeWith alternativeDecoder "a = 1"
    

    As a bonus, it also makes for a less point-free interface when defining a decoder based on another decoder, which is kinda cool:

    -- aeson-like
    instance DecodeTOML MyString where
      decodeTOML = fmap toMyString . decodeTOML
    
    -- with toml-parser
    instance DecodeTOML MyString where
      tomlDecoder = toMyString <$> tomlDecoder
    

    Ultimately, Decoder is just a newtype around Value -> DecodeM a, so we could always go back to it. Originally, I wanted to do something like jordan, where this interface is required due to the way it parses and deserializes at the same time, but this isn't possible with TOML due to the way TOML needs to be normalized.

Metadata

Version

0.2.1.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows