neco-kdl 0.1.0

Zero-dependency KDL v2 parser
Documentation
  • Coverage
  • 61.76%
    21 out of 34 items documented0 out of 1 items with examples
  • Size
  • Source code size: 118.23 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.05 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 50s Average build duration of successful builds.
  • all releases: 49s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • barineco/neco-crates
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • barineco

neco-kdl

日本語

Zero-dependency KDL v2 parser. Suitable for configuration files and DSL parsing.

Features

  • Full KDL v2 specification parsing
    • Multiline strings, raw strings, escline
    • Type annotations: (type)node
    • Slashdash comments (/-), block comments (/* ... */), nested comments
    • #true / #false / #null / #inf / #-inf / #nan keywords
    • Hex, octal, binary literals with underscore separators
    • Version marker (/- kdl-version 2)
  • Normalized output (matches official test suite expected_kdl)
  • Zero external dependencies
  • Passes the full official test suite

Usage

[dependencies]
neco-kdl = "0.1"
use neco_kdl::{parse, normalize};

fn main() {
    let src = r#"
        node "hello" key=#true {
            child 42
        }
    "#;

    let doc = parse(src).unwrap();

    // Iterate over nodes
    for node in doc.nodes() {
        println!("{}: {} entries", node.name(), node.entries().len());
    }

    // Convert to normalized form
    let normalized = normalize(&doc);
    print!("{}", normalized);
}

API

parse

pub fn parse(input: &str) -> Result<KdlDocument, KdlError>

Parses a KDL v2 document and returns a KdlDocument.

normalize

pub fn normalize(doc: &KdlDocument) -> String

Converts a KdlDocument to its normalized string form. Normalization rules:

  • Strips comments
  • Sorts properties by key in alphabetical order
  • Deduplicates properties (last occurrence wins)
  • Converts all strings to quoted strings
  • Unquotes strings that are valid identifiers
  • Indents with 4 spaces
  • Converts numbers to decimal, strips underscores
  • Adds trailing newline

Types

Item Description
KdlDocument Parse result root. Access nodes via nodes()
KdlNode Node with ty(), name(), entries(), children() accessors
KdlEntry Argument (positional) or Property (named)
KdlValue String(String), Number(KdlNumber), Bool(bool), Null
KdlNumber Provides raw(), as_i64(), as_f64(). No upper bound on numeric size
KdlError Error with line(), col() (1-based), kind()
KdlErrorKind Error variant: UnexpectedChar, InvalidEscape, UnclosedString, etc.

License

MIT