-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
parsing_toml.gleam
42 lines (34 loc) · 1.17 KB
/
parsing_toml.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//// # Parsing TOML
////
//// The tom library is a TOML parser that supports all targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/tom
import gleam/dict
import gleeunit/should
import tom
pub fn main_test() {
let toml =
"
name = \"cookbook\"
version = \"1.0.0\"
licences = [\"Apache-2.0\"]
repository = { type = \"github\", user = \"gleam-lang\", repo = \"cookbook\" }
[dependencies]
gleam_stdlib = \">= 0.34.0 and < 2.0.0\"
[dev-dependencies]
gleeunit = \">= 1.0.0 and < 2.0.0\"
"
// The `parse` function parses a string of TOML into a Gleam data structure
let assert Ok(doc) = tom.parse(toml)
// We've used `let assert` to crash if any of these functions fail. In a real
// application or library you'd want to handle the results properly.
// The resulting data structure is a Dict, and the TOML data type is not
// opaque, so you can pattern match on them directly.
let assert Ok(tom.String(name)) = dict.get(doc, "name")
name |> should.equal("cookbook")
// Alternatively some of the helper functions can be used to get nested values.
let assert Ok(user) = tom.get_string(doc, ["repository", "user"])
user |> should.equal("gleam-lang")
}