#[cfg(feature = "span")]
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};
use crate::{parser, KdlError, KdlNode, KdlValue};
#[derive(Debug, Clone)]
pub struct KdlDocument {
pub(crate) leading: Option<String>,
pub(crate) nodes: Vec<KdlNode>,
pub(crate) trailing: Option<String>,
#[cfg(feature = "span")]
pub(crate) span: SourceSpan,
}
impl PartialEq for KdlDocument {
fn eq(&self, other: &Self) -> bool {
self.leading == other.leading
&& self.nodes == other.nodes
&& self.trailing == other.trailing
}
}
impl Default for KdlDocument {
fn default() -> Self {
Self {
leading: Default::default(),
nodes: Default::default(),
trailing: Default::default(),
#[cfg(feature = "span")]
span: SourceSpan::from(0..0),
}
}
}
impl KdlDocument {
pub fn new() -> Self {
Default::default()
}
#[cfg(feature = "span")]
pub fn span(&self) -> &SourceSpan {
&self.span
}
#[cfg(feature = "span")]
pub fn span_mut(&mut self) -> &mut SourceSpan {
&mut self.span
}
#[cfg(feature = "span")]
pub fn set_span(&mut self, span: impl Into<SourceSpan>) {
self.span = span.into();
}
pub fn get(&self, name: &str) -> Option<&KdlNode> {
self.nodes.iter().find(move |n| n.name().value() == name)
}
pub fn get_mut(&mut self, name: &str) -> Option<&mut KdlNode> {
self.nodes
.iter_mut()
.find(move |n| n.name().value() == name)
}
pub fn get_arg(&self, name: &str) -> Option<&KdlValue> {
self.get(name)
.and_then(|node| node.get(0))
.map(|e| e.value())
}
pub fn get_args(&self, name: &str) -> Vec<&KdlValue> {
self.get(name)
.map(|n| n.entries())
.unwrap_or_default()
.iter()
.filter(|e| e.name().is_none())
.map(|e| e.value())
.collect()
}
pub fn get_arg_mut(&mut self, name: &str) -> Option<&mut KdlValue> {
self.get_mut(name)
.and_then(|node| node.get_mut(0))
.map(|e| e.value_mut())
}
pub fn get_dash_vals(&self, name: &str) -> Vec<&KdlValue> {
self.get(name)
.and_then(|n| n.children())
.map(|doc| doc.nodes())
.unwrap_or_default()
.iter()
.filter(|e| e.name().value() == "-")
.map(|e| e.get(0))
.filter(|v| v.is_some())
.map(|v| v.unwrap().value())
.collect()
}
pub fn nodes(&self) -> &[KdlNode] {
&self.nodes
}
pub fn nodes_mut(&mut self) -> &mut Vec<KdlNode> {
&mut self.nodes
}
pub fn leading(&self) -> Option<&str> {
self.leading.as_deref()
}
pub fn set_leading(&mut self, leading: impl Into<String>) {
self.leading = Some(leading.into());
}
pub fn trailing(&self) -> Option<&str> {
self.trailing.as_deref()
}
pub fn set_trailing(&mut self, trailing: impl Into<String>) {
self.trailing = Some(trailing.into());
}
pub fn len(&self) -> usize {
format!("{}", self).len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn clear_fmt(&mut self) {
self.leading = None;
self.trailing = None;
}
pub fn clear_fmt_recursive(&mut self) {
self.clear_fmt();
for node in self.nodes.iter_mut() {
node.clear_fmt_recursive();
}
}
pub fn fmt(&mut self) {
self.fmt_impl(0, false);
}
pub fn fmt_no_comments(&mut self) {
self.fmt_impl(0, true);
}
}
impl Display for KdlDocument {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.stringify(f, 0)
}
}
impl KdlDocument {
pub(crate) fn fmt_impl(&mut self, indent: usize, no_comments: bool) {
if let Some(s) = self.leading.as_mut() {
crate::fmt::fmt_leading(s, indent, no_comments);
}
let mut has_nodes = false;
for node in &mut self.nodes {
has_nodes = true;
node.fmt_impl(indent, no_comments);
}
if let Some(s) = self.trailing.as_mut() {
crate::fmt::fmt_trailing(s, no_comments);
if !has_nodes {
s.push('\n');
}
}
}
pub(crate) fn stringify(
&self,
f: &mut std::fmt::Formatter<'_>,
indent: usize,
) -> std::fmt::Result {
if let Some(leading) = &self.leading {
write!(f, "{}", leading)?;
}
for node in &self.nodes {
node.stringify(f, indent)?;
if node.trailing.is_none() {
writeln!(f)?;
}
}
if let Some(trailing) = &self.trailing {
write!(f, "{}", trailing)?;
}
Ok(())
}
}
impl IntoIterator for KdlDocument {
type Item = KdlNode;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.nodes.into_iter()
}
}
impl FromStr for KdlDocument {
type Err = KdlError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let kdl_parser = parser::KdlParser::new(input);
kdl_parser.parse(parser::document(&kdl_parser))
}
}
#[cfg(test)]
mod test {
#[cfg(feature = "span")]
use crate::KdlIdentifier;
use crate::{KdlEntry, KdlValue};
use super::*;
#[test]
fn canonical_clear_fmt() -> miette::Result<()> {
let left_src = r#"
// There is a node here
first_node /*with cool comments, too */ param=1.03e2 /-"commented" "argument" {
// With nested nodes too
nested 1 2 3
nested_2 "hi" "world" // this one is cool
}
second_node param=153 { nested one=1 two=2; }"#;
let right_src = r#"
first_node param=103.0 "argument" {
// Different indentation, because
// Why not
nested 1 2 3
nested_2 "hi" /* actually, "hello" */ "world"
}
// There is a node here
second_node /* This time, the comment is here */ param=153 {
nested one=1 two=2
}"#;
let mut left_doc: KdlDocument = left_src.parse()?;
let mut right_doc: KdlDocument = right_src.parse()?;
assert_ne!(left_doc, right_doc);
left_doc.clear_fmt_recursive();
right_doc.clear_fmt_recursive();
assert_eq!(left_doc, right_doc);
Ok(())
}
#[test]
fn parsing() -> miette::Result<()> {
let src = "
// This is the first node
foo 1 2 \"three\" null true bar=\"baz\" {
- 1
- 2
- \"three\"
(mytype)something (\"name\")\"else\"\r
}
null_id null_prop=null
true_id true_prop=null
+false true
bar \"indented\" // trailing whitespace after this\t
/*
Some random comment
*/
a; b; c;
/-commented \"node\"
another /*foo*/ \"node\" /-1 /*bar*/ null;
final;";
let mut doc: KdlDocument = src.parse()?;
assert_eq!(doc.leading, Some("".into()));
assert_eq!(doc.get_arg("foo"), Some(&1.into()));
assert_eq!(
doc.get_dash_vals("foo"),
vec![&1.into(), &2.into(), &"three".into()]
);
let foo = doc.get("foo").expect("expected a foo node");
assert_eq!(foo.leading, Some("\n// This is the first node\n".into()));
assert_eq!(&foo[2], &"three".into());
assert_eq!(&foo["bar"], &"baz".into());
assert_eq!(
foo.children().unwrap().get_arg("something"),
Some(&"else".into())
);
assert_eq!(doc.get_arg("another"), Some(&"node".into()));
let null = doc.get("null_id").expect("expected a null_id node");
assert_eq!(&null["null_prop"], &KdlValue::Null);
let tru = doc.get("true_id").expect("expected a true_id node");
assert_eq!(&tru["true_prop"], &KdlValue::Null);
let plusfalse = doc.get("+false").expect("expected a +false node");
assert_eq!(&plusfalse[0], &KdlValue::Bool(true));
let bar = doc.get("bar").expect("expected a bar node");
assert_eq!(
format!("{}", bar),
"\n bar \"indented\" // trailing whitespace after this\t\n"
);
let a = doc.get("a").expect("expected a node");
assert_eq!(
format!("{}", a),
"/*\nSome random comment\n */\n\na; ".to_string()
);
let b = doc.get("b").expect("expected a node");
assert_eq!(format!("{}", b), "b; ".to_string());
assert_eq!(format!("{}", doc), src);
let mut node: KdlNode = "new\n".parse()?;
node.push("\"blah\"=0xDEADbeef".parse::<KdlEntry>()?);
doc.nodes_mut().push(node);
assert_eq!(
format!("{}", doc),
format!("{}new \"blah\"=0xDEADbeef\n", src)
);
Ok(())
}
#[test]
fn construction() {
let mut doc = KdlDocument::new();
doc.nodes_mut().push(KdlNode::new("foo"));
let mut bar = KdlNode::new("bar");
bar.insert("prop", "value");
bar.push(1);
bar.push(2);
bar.push(false);
bar.push(KdlValue::Null);
let subdoc = bar.ensure_children();
subdoc.nodes_mut().push(KdlNode::new("barchild"));
doc.nodes_mut().push(bar);
doc.nodes_mut().push(KdlNode::new("baz"));
assert_eq!(
r#"foo
bar prop="value" 1 2 false null {
barchild
}
baz
"#,
format!("{}", doc)
);
}
#[test]
fn fmt() -> miette::Result<()> {
let mut doc: KdlDocument = r#"
/* x */ foo 1 "bar"=0xDEADbeef {
child1 1 ;
// child 2 comment
child2 2 // comment
child3 "
string\t" \
{
/*
multiline*/
inner1 \
r"value" \
;
inner2 \ //comment
{
inner3
}
}
}
// trailing comment here
"#
.parse()?;
KdlDocument::fmt(&mut doc);
print!("{}", doc);
assert_eq!(
doc.to_string(),
r#"/* x */
foo 1 bar=0xdeadbeef {
child1 1
// child 2 comment
child2 2 // comment
child3 "\n\n string\t" {
/*
multiline*/
inner1 r"value"
inner2 {
inner3
}
}
}
// trailing comment here"#
);
Ok(())
}
#[test]
fn simple_fmt() -> miette::Result<()> {
let mut doc: KdlDocument = "a { b { c { }; }; }".parse().unwrap();
KdlDocument::fmt(&mut doc);
print!("{}", doc);
assert_eq!(
doc.to_string(),
r#"a {
b {
c {
}
}
}
"#
);
Ok(())
}
#[cfg(feature = "span")]
fn check_spans_for_doc(doc: &KdlDocument, source: &impl miette::SourceCode) {
for node in doc.nodes() {
check_spans_for_node(node, source);
}
}
#[cfg(feature = "span")]
fn check_spans_for_node(node: &KdlNode, source: &impl miette::SourceCode) {
check_span_for_ident(node.name(), source);
if let Some(ty) = node.ty() {
check_span_for_ident(ty, source);
}
for entry in node.entries() {
if let Some(name) = entry.name() {
check_span_for_ident(name, source);
}
if let Some(ty) = entry.ty() {
check_span_for_ident(ty, source);
}
if let Some(repr) = entry.value_repr() {
if entry.name().is_none() && entry.ty().is_none() {
check_span(repr, entry.span(), source);
}
}
}
if let Some(children) = node.children() {
check_spans_for_doc(children, source);
}
}
#[cfg(feature = "span")]
#[track_caller]
fn check_span_for_ident(ident: &KdlIdentifier, source: &impl miette::SourceCode) {
if let Some(repr) = ident.repr() {
check_span(repr, ident.span(), source);
} else {
check_span(ident.value(), ident.span(), source);
}
}
#[cfg(feature = "span")]
#[track_caller]
fn check_span(expected: &str, span: &SourceSpan, source: &impl miette::SourceCode) {
let span = source.read_span(span, 0, 0).unwrap();
let span = std::str::from_utf8(span.data()).unwrap();
assert_eq!(span, expected);
}
#[cfg(feature = "span")]
#[test]
fn span_test() -> miette::Result<()> {
let input = r####"
this {
is (a)"cool" document="to" read=(int)5 10.1 (u32)0x45
and x="" {
"it" /*shh*/ "has"="💯" r##"the"##
Best🎊est
"syntax ever"
}
"yknow?" 0x10
}
// that's
nice
inline { time; to; live "our" "dreams"; "y;all"; }
"####;
let doc: KdlDocument = input.parse().unwrap();
check_spans_for_doc(&doc, &input);
check_span(input, doc.span(), &input);
let is_node = doc
.get("this")
.unwrap()
.children()
.unwrap()
.get("is")
.unwrap();
check_span(
r##"is (a)"cool" document="to" read=(int)5 10.1 (u32)0x45"##,
is_node.span(),
&input,
);
check_span(r#"(a)"cool""#, is_node.get(0).unwrap().span(), &input);
check_span(
r#"read=(int)5"#,
is_node.get("read").unwrap().span(),
&input,
);
check_span(r#"10.1"#, is_node.get(1).unwrap().span(), &input);
check_span(r#"(u32)0x45"#, is_node.get(2).unwrap().span(), &input);
let and_node = doc
.get("this")
.unwrap()
.children()
.unwrap()
.get("and")
.unwrap();
check_span(
r####"and x="" {
"it" /*shh*/ "has"="💯" r##"the"##
Best🎊est
"syntax ever"
}"####,
and_node.span(),
&input,
);
check_span(
r####"
"it" /*shh*/ "has"="💯" r##"the"##
Best🎊est
"syntax ever"
"####,
and_node.children().unwrap().span(),
&input,
);
check_span(r#"x="""#, and_node.get("x").unwrap().span(), &input);
let it_node = and_node.children().unwrap().get("it").unwrap();
check_span(
r####""it" /*shh*/ "has"="💯" r##"the"##"####,
it_node.span(),
&input,
);
check_span(r#""has"="💯""#, it_node.get("has").unwrap().span(), &input);
check_span(
r####"r##"the"##"####,
it_node.get(0).unwrap().span(),
&input,
);
let inline_node = doc.get("inline").unwrap();
check_span(
r#"inline { time; to; live "our" "dreams"; "y;all"; }"#,
inline_node.span(),
&input,
);
let inline_children = inline_node.children().unwrap();
check_span(
r#" time; to; live "our" "dreams"; "y;all"; "#,
inline_children.span(),
&input,
);
let inline_nodes = inline_children.nodes();
check_span("time", inline_nodes[0].span(), &input);
check_span("to", inline_nodes[1].span(), &input);
check_span(r#"live "our" "dreams""#, inline_nodes[2].span(), &input);
check_span(r#""y;all""#, inline_nodes[3].span(), &input);
Ok(())
}
#[test]
fn parse_examples() -> miette::Result<()> {
include_str!("../examples/kdl-schema.kdl").parse::<KdlDocument>()?;
include_str!("../examples/Cargo.kdl").parse::<KdlDocument>()?;
include_str!("../examples/ci.kdl").parse::<KdlDocument>()?;
include_str!("../examples/nuget.kdl").parse::<KdlDocument>()?;
Ok(())
}
}