Elm:
Working with TOML
How to:
Elm doesn’t have a built-in TOML parser, but you can interop with JavaScript or use a community package. Here’s how you might parse TOML using a hypothetical elm-toml
package:
import Toml
configToml : String
configToml =
"""
[server]
port = 8080
"""
parseResult : Result Toml.Decode.Error Toml.Value
parseResult =
Toml.decodeString configToml
For decoding specific values:
portDecoder : Toml.Decode.Decoder Int
portDecoder =
Toml.Decode.field "server" (Toml.Decode.field "port" Toml.Decode.int)
port : Result String Int
port =
Toml.decodeString portDecoder configToml
Sample output for port
might be Ok 8080
if the decoding succeeds.
Deep Dive
TOML was created by Tom Preston-Werner, co-founder of GitHub, as a simple language for configuration files. It competes with YAML and JSON; TOML’s syntax aims for the best of both worlds with a focus on being easy for humans to read and write.
In Elm, to handle TOML, you typically need to go through JavaScript interop, which can be a bit of a hassle. Thankfully, the Elm community is resourceful, and several third-party packages exist. The hypothetical elm-toml
package would likely use Elm’s Port
to talk to a JavaScript TOML parser or implement the parsing directly in Elm.
The main hurdle in Elm is that it statically types everything, so you’ll need to write custom decoders to handle different data structures within TOML, which can be a bit verbose but adds safety.
See Also
For specs and more info on TOML itself, check out TOML. If you’re looking for a hands-on approach to Elm and JavaScript interop, start with the official guide: Elm Ports. For community packages or to contribute, browse Elm Packages.