2 releases
new 0.1.2 | Mar 23, 2025 |
---|---|
0.1.1 | Mar 23, 2025 |
#377 in Development tools
116 downloads per month
15KB
216 lines
gtoml
Get TOML values quickly
gtoml is a Rust crate that offers a fast and simple way to retrieve values from a TOML document. It comes with features such as one-line retrieval, dot notation paths, and iteration.
Getting Started
Usage
Add the following to your Cargo.toml
:
[dependencies]
gtoml = "0.1"
TOML Parser and Value Retrieval Library
Overview
This is a Rust library designed to parse TOML strings and retrieve values based on specified paths. It supports basic TOML parsing, path-based value lookup, array length retrieval, and wildcard matching.
Features
- TOML Parsing: It can parse a TOML string into a
toml::Value
type. - Value Retrieval: Retrieve values from the parsed
Value
using a specified path. - Wildcard Support: Supports the use of
*
and?
wildcards in the path for matching. - Array Length Retrieval: Retrieve the length of an array.
How to Use
Parse a TOML String
use toml::Value;
use gtoml::parse;
let toml_str = r#"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z
"#;
let value = parse(toml_str).unwrap();
Retrieve a Value by Path
use toml::Value;
use gtoml::{parse, get};
let toml_str = r#"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z
"#;
let value = parse(toml_str).unwrap();
let result = get(&value, "owner.name").unwrap();
Retrieve a Value Directly from a TOML String
use toml::Value;
use gtoml::get_from_str;
let toml_str = r#"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00Z
"#;
let result = get_from_str(toml_str, "owner.name").unwrap();
Using Wildcards
You can use *
and ?
wildcards in the path for matching:
use toml::Value;
use gtoml::{parse, get};
let toml_str = r#"
children = ["Sara", "Alex", "Jack"]
"#;
let value = parse(toml_str).unwrap();
let result = get(&value, "child*").unwrap();
Retrieving Array Length
To get the length of an array, use #
in the path:
use toml::Value;
use gtoml::{parse, get};
let toml_str = r#"
children = ["Sara", "Alex", "Jack"]
"#;
let value = parse(toml_str).unwrap();
let result = get(&value, "children.#").unwrap();
Testing
This library includes a series of test cases. You can run the tests with the following command:
cargo test
Dependencies
~2.5–4MB
~76K SLoC