Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fixes to v2 so far #75

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ span = []

[dependencies]
miette = "7.2.0"
nom = "7.1.1"
# nom = "7.1.1"
thiserror = "1.0.40"
winnow = { version = "0.6.5", features = ["alloc", "unstable-recover"] }

Expand Down
28 changes: 28 additions & 0 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,27 @@ mod test {
use crate::{KdlEntry, KdlValue};

use super::*;
use std::sync::Once;

fn miette_setup() {
static MIETTE_SETUP: Once = Once::new();
MIETTE_SETUP.call_once(|| {
miette::set_hook(Box::new(|_| {
Box::new(
miette::MietteHandlerOpts::new()
.context_lines(1)
.break_words(true)
.build(),
)
}))
.unwrap();
});
}

#[test]
fn canonical_clear_fmt() -> miette::Result<()> {
miette_setup();

let left_src = r#"
// There is a node here
first_node /*with cool comments, too */ param=1.03e2 /-"commented" "argument" {
Expand Down Expand Up @@ -414,6 +432,8 @@ second_node /* This time, the comment is here */ param=153 {

#[test]
fn parsing() -> miette::Result<()> {
miette_setup();

let src = "
// This is the first node
foo 1 2 \"three\" null true bar=\"baz\" {
Expand Down Expand Up @@ -530,6 +550,8 @@ baz

#[test]
fn autoformat() -> miette::Result<()> {
miette_setup();

let mut doc: KdlDocument = r#"

/* x */ foo 1 "bar"=0xDEADbeef {
Expand Down Expand Up @@ -591,6 +613,8 @@ foo 1 bar=0xdeadbeef {

#[test]
fn simple_autoformat() -> miette::Result<()> {
miette_setup();

let mut doc: KdlDocument = "a { b { c { }; }; }".parse().unwrap();
KdlDocument::autoformat(&mut doc);
print!("{}", doc);
Expand Down Expand Up @@ -663,6 +687,8 @@ foo 1 bar=0xdeadbeef {
#[cfg(feature = "span")]
#[test]
fn span_test() -> miette::Result<()> {
miette_setup();

let input = r####"
this {
is (a)"cool" document="to" read=(int)5 10.1 (u32)0x45
Expand Down Expand Up @@ -791,6 +817,8 @@ inline { time; to; live "our" "dreams"; "y;all"; }

#[test]
fn parse_examples() -> miette::Result<()> {
miette_setup();

include_str!("../examples/kdl-schema.kdl").parse::<KdlDocument>()?;
include_str!("../examples/Cargo.kdl").parse::<KdlDocument>()?;
include_str!("../examples/ci.kdl").parse::<KdlDocument>()?;
Expand Down
68 changes: 35 additions & 33 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{
num::{ParseFloatError, ParseIntError},
sync::Arc,
use {
miette::{Diagnostic, SourceSpan},
std::{
num::{ParseFloatError, ParseIntError},
sync::Arc,
},
thiserror::Error,
winnow::error::{ErrorKind, FromExternalError},
};

use miette::{Diagnostic, SourceSpan};
use nom::error::{ContextError, ErrorKind, FromExternalError, ParseError};
use thiserror::Error;
//ContextError,, ParseError

#[cfg(doc)]
use {
Expand Down Expand Up @@ -116,33 +118,33 @@ pub(crate) struct KdlParseError<I> {
pub(crate) touched: bool,
}

impl<I> ParseError<I> for KdlParseError<I> {
fn from_error_kind(input: I, _kind: nom::error::ErrorKind) -> Self {
Self {
input,
len: 0,
label: None,
help: None,
context: None,
kind: None,
touched: false,
}
}

fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self {
other
}
}

impl<I> ContextError<I> for KdlParseError<I> {
fn add_context(_input: I, ctx: &'static str, mut other: Self) -> Self {
other.context = other.context.or(Some(ctx));
other
}
}
// impl<I> ParseError<I> for KdlParseError<I> {
// fn from_error_kind(input: I, _kind: winnow::error::ErrorKind) -> Self {
// Self {
// input,
// len: 0,
// label: None,
// help: None,
// context: None,
// kind: None,
// touched: false,
// }
// }

// fn append(_input: I, _kind: winnow::error::ErrorKind, other: Self) -> Self {
// other
// }
// }

// impl<I> ContextError<I> for KdlParseError<I> {
// fn add_context(_input: I, ctx: &'static str, mut other: Self) -> Self {
// other.context = other.context.or(Some(ctx));
// other
// }
// }

impl<'a> FromExternalError<&'a str, ParseIntError> for KdlParseError<&'a str> {
fn from_external_error(input: &'a str, _kind: ErrorKind, e: ParseIntError) -> Self {
fn from_external_error(input: &&'a str, _kind: ErrorKind, e: ParseIntError) -> Self {
KdlParseError {
input,
len: 0,
Expand All @@ -156,7 +158,7 @@ impl<'a> FromExternalError<&'a str, ParseIntError> for KdlParseError<&'a str> {
}

impl<'a> FromExternalError<&'a str, ParseFloatError> for KdlParseError<&'a str> {
fn from_external_error(input: &'a str, _kind: ErrorKind, e: ParseFloatError) -> Self {
fn from_external_error(input: &&'a str, _kind: ErrorKind, e: ParseFloatError) -> Self {
KdlParseError {
input,
len: 0,
Expand Down
14 changes: 11 additions & 3 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl KdlNode {
/// their formatting will be preserved.
///
/// If you want to clear formatting on all children and entries as well,
/// use [`Self::clear_fmt_recursive`].
/// use [`Self::clear_format_recursive`].
pub fn clear_format(&mut self) {
self.format = None;
}
Expand Down Expand Up @@ -496,7 +496,7 @@ impl FromStr for KdlNode {
type Err = KdlParseFailure;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let (maybe_val, errs) = dbg!(v2_parser::try_parse(v2_parser::padded_node, input));
let (maybe_val, errs) = v2_parser::try_parse(v2_parser::padded_node, input);
if let (Some(v), true) = (maybe_val, errs.is_empty()) {
Ok(v)
} else {
Expand Down Expand Up @@ -620,13 +620,21 @@ pub struct KdlNodeFormat {
mod test {
use super::*;

#[test]
fn try_it_out() -> miette::Result<()> {
// "foo {}".parse::<KdlNode>()?;
r#"prop ref description="A globally unique reference to another property node." {}"#
.parse::<KdlNode>()?;
Ok(())
}

#[test]
fn canonical_clear_fmt() -> miette::Result<()> {
let mut left_node: KdlNode = r#"node /-"commented" param_name=103.000 {
// This is a nested node
nested 1 2 3
}"#
.parse()?;
.parse::<KdlNode>()?;
let mut right_node: KdlNode = "node param_name=103.0 { nested 1 2 3; }".parse()?;
dbg!(&left_node);
dbg!(&right_node);
Expand Down
Loading