#parse #sort #format

toml-parse

Set of modules for parsing, sorting and formating toml

9 releases

0.2.11 Sep 8, 2020
0.2.10 Sep 8, 2020
0.2.7 Mar 15, 2020
0.1.5 Mar 13, 2020

#537 in Parser implementations

Download history 174/week @ 2022-06-10 223/week @ 2022-06-17 156/week @ 2022-06-24 255/week @ 2022-07-01 136/week @ 2022-07-08 136/week @ 2022-07-15 114/week @ 2022-07-22 248/week @ 2022-07-29 249/week @ 2022-08-05 194/week @ 2022-08-12 163/week @ 2022-08-19 166/week @ 2022-08-26 175/week @ 2022-09-02 196/week @ 2022-09-09 332/week @ 2022-09-16 122/week @ 2022-09-23

886 downloads per month
Used in cargo-sort-ck

MIT/Apache

120KB
3K SLoC

P.O.T.

Parser of Toml powered by Muncher

Build Status Released API docs Latest Version

About

The most important thing about this toml parser is that it maintains the structure of the original parsed file (whitespace, comments, ect.). For toml formatting tools like cargo sort check this feature is essential, this is the only reason something like toml-rs cannot be used unfortunatly :(.

Use

[dependencies]
toml-parse = "0.2.7"

Examples

Parsing

use toml_parse::{parse_it, SyntaxNodeExtTrait};

let file = 
r#"[deps]
alpha = "beta"
number = 1234
array = [ true, false, true ]
inline-table = { date = 1988-02-03T10:32:10, }
"#;

let parsed = parse_it(file).expect("parse failed");
let root = parsed.syntax();
assert_eq!(root.token_text(), file)

The parse tree is a rowan SyntaxNode that can be manipulated and traversed freely. The SyntaxNodeExtTrait allows easy to string representation of the tokens (the source file text).

Sorting

use toml_parse::{parse_it, SyntaxNodeExtTrait};

let file = r#"# comment
[dependencies]
number = 1234
# comment
alpha = "beta"
"#;
let parsed = parse_it(file).expect("parse failed").syntax();
let parsed2 = parse_it(file).expect("parse failed").syntax();

assert!(parsed.deep_eq(&parsed2));

let sorted = sort_toml_items(&parsed, &HEADER);

assert!(!parsed.deep_eq(&sorted));

The sorted tree is a rowan SyntaxNode that can be manipulated and traversed freely.

Formatting

use toml_parse::{parse_it, Formatter};

let parsed = parse_it(&input).expect("").syntax();
let fmted = Formatter::new(&parsed).format();
assert_ne!(fmted.to_string(), input);

Structured (Coming soonish)

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2MB
~35K SLoC