$ show kdl-org/kdl-rs
This is a Rust library (`kdl`) for parsing, manipulating, and serializing the KDL Document Language. It provides a fully round-trip-preserving parser (formatting, whitespace, comments) supporting both KDL v2 and, optionally, v1. The library exposes a rich AST (`KdlDocument`, `KdlNode`, `KdlEntry`, `KdlIdentifier`, `KdlValue`) with builder patterns and formatting configuration. It integrates with `serde` for deserializing/serializing Rust types from/to KDL, using a well-documented mapping convention (e.g., `#args`, `#rest`, property renames via `#@`). Error handling is implemented via `miette` for rich diagnostics. The repository also includes a language server (`kdl-lsp`) based on `tower-lsp` that provides diagnostics for KDL files, though incomplete. Comprehensive compliance tests (hundreds of input/expected KDL files) ensure spec adherence. The architecture is a single library crate with optional serde support, a separate v1 fallback crate, and an LSP tool. The core parser uses `winnow` with error recovery. Dead code from a disabled KQL query engine exists, and serde support is marked experimental, indicating some technical drift. Overall, it's a high-quality, professionally engineered library with excellent test coverage and well-thought-out abstractions.
The LLM identified 4 files worth highlighting for reuse:
`FormatConfigBuilder` enables declarative construction of auto-formatting settings (indentation, whitespace, comment removal). It uses a fluent builder pattern and is isolated from other internal modules, making it reusable for any project needing configurable formatting of a structured text format.
`KdlDiagnostic` implements `miette::Diagnostic` for rich, user-friendly error reporting with source spans, labels, and help messages. The abstraction is clean and could be extracted as a pattern for any Rust project that wants structured error reporting with source context, independent of KDL.
The serde deserializer maps KDL documents to Rust types via serde's data model, using a clever convention for arguments, properties, and arrays (`-` children). The implementation is self-contained and demonstrates how to implement `serde::Deserialize` for a non-JSON format. This pattern can be adapted for other document formats that need serde support.
The v2 parser uses `winnow` combinators with a `resume_after_cut` pattern for error recovery during parsing. It also generates detailed diagnostics via `KdlDiagnostic`. The error recovery strategy and combinator organization are reusable for any parser that must continue parsing after errors while collecting meaningful diagnostics.