TOML
Parse and generate TOML configuration files. TOML is a minimal configuration file format that's easy to read due to obvious semantics.
import Encoding.TOML
Parsing
TOML.parse
String -> Result TomlValue Error
Parse a TOML string into a TomlValue. Returns an error for invalid TOML.
toml-str = "[package]\nname = \"my-app\"\nversion = \"1.0.0\""
match TOML.parse toml-str
| Ok config -> println "Parsed config"
| Err e -> println "Parse error: ${e}"
Type-Safe Getters
Extract values from parsed TOML with type safety. Each getter returns Option
to handle missing or mistyped values.
TOML.get-string
TomlValue -> String -> Option String
Get a string value by key. Returns
None if the key doesn't exist
or the value isn't a string.
config = TOML.parse toml-str |> Result.unwrap
name = TOML.get-string config "name" ?? "unknown"
TOML.get-int
TomlValue -> String -> Option Int
Get an integer value by key.
port = TOML.get-int config "port" ?? 8080
TOML.get-float
TomlValue -> String -> Option Float
Get a float value by key.
TOML.get-bool
TomlValue -> String -> Option Bool
Get a boolean value by key.
debug = TOML.get-bool config "debug" ?? false
TOML.get-array
TomlValue -> String -> Option [TomlValue]
Get an array value by key as a list of TomlValues.
TOML.get-raw
TomlValue -> String -> Option TomlValue
Get a raw TomlValue by key without type conversion. Useful for nested tables.
# Access nested [package] section
match TOML.get-raw config "package"
| Some pkg -> TOML.get-string pkg "name"
| None -> None
Generating
TOML.stringify
TomlValue -> String
Convert a TomlValue to a TOML string (inline format).
TOML.generate
a -> String
Generate a TOML document from a Kit map/record. Produces properly formatted
TOML with sections and indentation.
config = {
package: {
name: "my-app",
version: "1.0.0"
},
dependencies: {
http: "2.0.0"
}
}
toml = TOML.generate config
# [package]
# name = "my-app"
# version = "1.0.0"
#
# [dependencies]
# http = "2.0.0"