Skip to content

Commit

Permalink
Parsing TOML
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Sep 23, 2024
1 parent 6e1617b commit 67d6c63
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Examples showing how to do many things in Gleam!

## Formats

- [Parsing TOML](./universal/test/formats/parsing_toml.gleam)
- [Rendering HTML](./universal/test/formats/rendering_html.gleam)
- [Rendering JSON](./universal/test/formats/rendering_json.gleam)
- [Rendering XML](./universal/test/formats/rendering_xml.gleam)
1 change: 1 addition & 0 deletions universal/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xmb = ">= 1.0.0 and < 2.0.0"
gleam_json = ">= 1.0.1 and < 2.0.0"
lustre = ">= 4.4.4 and < 5.0.0"
gleam_crypto = ">= 1.3.0 and < 2.0.0"
tom = ">= 1.1.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions universal/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages = [
{ name = "lustre", version = "4.4.4", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib"], otp_app = "lustre", source = "hex", outer_checksum = "35A8597B3054AFAF734A54979B7C92D8AB43273B843717F854CF5A05CF2BC00E" },
{ name = "simplifile", version = "2.1.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "BDD04F5D31D6D34E2EDFAEF0B68A6297AEC939888C3BFCE61133DE13857F6DA2" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" },
{ name = "xmb", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "xmb", source = "hex", outer_checksum = "99425CD67BA2AF3E4A38515FB4BFCC121007B69444C81DE6EB837421290F14B4" },
]

Expand All @@ -22,4 +23,5 @@ gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
lustre = { version = ">= 4.4.4 and < 5.0.0" }
simplifile = { version = ">= 2.1.0 and < 3.0.0" }
tom = { version = ">= 1.1.0 and < 2.0.0" }
xmb = { version = ">= 1.0.0 and < 2.0.0" }
43 changes: 43 additions & 0 deletions universal/test/formats/parsing_toml.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//// # Parsing TOML
////
//// Lustre is often used for complex stateful HTML applications, but it also
//// makes a great type-safe HTML templating system. It works on 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")
}

0 comments on commit 67d6c63

Please sign in to comment.