15-structured-data/03-toml-configuration.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
a99390809f6edbd6221969f838f4ae04ccbc6256- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// TOML configuration as semantic nodes.
//
// std:toml/toml parses a document into a root table node. Every node has a
// `kind` (string, integer, float, boolean, localDate, localTime,
// localDateTime, offsetDateTime, array or table) and a `value`. Parsing is
// semantic: comments and source layout are not preserved, and encoding writes
// a fresh canonical document rather than reproducing the input text.
TOML: import("std:toml/toml")
source: """
# Service configuration
name = "inventory"
replicas = 3
ratio = 0.75
[server]
host = "localhost"
port = 8080
[maintenance]
day = 2026-12-05
window = 02:30:00
announced = 2026-12-01T09:00:00-03:00
"""
config: TOML.parseDialect(source, "1.0")
print(config.kind) // table
print(config.value["name"].value) // inventory
print(config.value["replicas"].value + 1) // 4
print(config.value["server"].value["port"].value) // 8080
// Temporal values are validated records, not Strings.
day: config.value["maintenance"].value["day"]
print(day.kind) // localDate
print(day.value.month) // 12
announced: config.value["maintenance"].value["announced"]
print(announced.value.offsetMinutes) // -180
// TOML 1.1 (the default for parse) admits a time without seconds;
// TOML 1.0 requires seconds.
modern: TOML.parse("start = 07:30\n")
print(modern.value["start"].value.second) // 0
print(Error.handle(
() => { TOML.parseDialect("start = 07:30\n", "1.0"); "accepted" },
(error) => { "TOML 1.0 rejects a time without seconds" }
))
// Duplicate keys and invalid dates are rejected.
print(Error.handle(
() => { TOML.parse("a = 1\na = 2\n"); "accepted" },
(error) => { "duplicate key rejected" }
))
print(Error.handle(
() => { TOML.localDate(2026, 2, 29); "accepted" },
(error) => { "invalid date rejected" }
))
// Build a new table from alternating names and nodes, then encode it.
updated: TOML.table(
"name", TOML.string("inventory"),
"replicas", TOML.integer(5),
"server", TOML.table(
"host", TOML.string("localhost"),
"port", TOML.integer(9090)
),
"labels", TOML.array(TOML.string("blue"), TOML.integer(2))
)
text: TOML.encode(updated)
reparsed: TOML.parse(text)
print(reparsed.value["server"].value["port"].value) // 9090
print(reparsed.value["labels"].value.size()) // 2