Skip to content

Commit

Permalink
feat(error): Rename KdlParseFailure back to KdlError
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 20, 2024
1 parent c486cda commit 12b2fd2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 53 deletions.
10 changes: 5 additions & 5 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::fmt::Display;

use crate::{FormatConfig, KdlNode, KdlParseFailure, KdlValue};
use crate::{FormatConfig, KdlError, KdlNode, KdlValue};

/// Represents a KDL
/// [`Document`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#document).
Expand Down Expand Up @@ -341,7 +341,7 @@ impl KdlDocument {
/// parse the string as a KDL v2 document, and, if that fails, it will try
/// to parse again as a KDL v1 document. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse(s: &str) -> Result<Self, KdlError> {
#[cfg(not(feature = "v1-fallback"))]
{
crate::v2_parser::try_parse(crate::v2_parser::document, s)
Expand All @@ -355,15 +355,15 @@ impl KdlDocument {

/// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlDocument, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
}

/// Takes a KDL v1 document string and returns the same document, but
/// autoformatted into valid KDL v2 syntax.
#[cfg(feature = "v1")]
pub fn v1_to_v2(s: &str) -> Result<String, KdlParseFailure> {
pub fn v1_to_v2(s: &str) -> Result<String, KdlError> {
let mut doc = KdlDocument::parse_v1(s)?;
doc.autoformat();
Ok(doc.to_string())
Expand All @@ -386,7 +386,7 @@ impl From<kdlv1::KdlDocument> for KdlDocument {
}

impl std::str::FromStr for KdlDocument {
type Err = KdlParseFailure;
type Err = KdlError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
KdlDocument::parse(s)
Expand Down
8 changes: 4 additions & 4 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};

use crate::{v2_parser, KdlIdentifier, KdlParseFailure, KdlValue};
use crate::{v2_parser, KdlError, KdlIdentifier, KdlValue};

/// KDL Entries are the "arguments" to KDL nodes: either a (positional)
/// [`Argument`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) or
Expand Down Expand Up @@ -248,7 +248,7 @@ impl KdlEntry {
/// parse the string as a KDL v2 entry, and, if that fails, it will try
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse(s: &str) -> Result<Self, KdlError> {
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::try_parse(v2_parser::padded_node_entry, s)
Expand All @@ -262,7 +262,7 @@ impl KdlEntry {

/// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlEntry, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
}
Expand Down Expand Up @@ -353,7 +353,7 @@ where
}

impl FromStr for KdlEntry {
type Err = KdlParseFailure;
type Err = KdlError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
KdlEntry::parse(s)
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use {
/// ```
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("Failed to parse KDL document")]
pub struct KdlParseFailure {
pub struct KdlError {
/// Original input that this failure came from.
#[source_code]
pub input: Arc<String>,
Expand Down Expand Up @@ -76,10 +76,10 @@ pub struct KdlDiagnostic {
}

#[cfg(feature = "v1")]
impl From<kdlv1::KdlError> for KdlParseFailure {
impl From<kdlv1::KdlError> for KdlError {
fn from(value: kdlv1::KdlError) -> Self {
let input = Arc::new(value.input);
KdlParseFailure {
KdlError {
input: input.clone(),
diagnostics: vec![KdlDiagnostic {
input,
Expand Down
8 changes: 4 additions & 4 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};

use crate::{v2_parser, KdlParseFailure, KdlValue};
use crate::{v2_parser, KdlError, KdlValue};

/// Represents a KDL
/// [Identifier](https://github.com/kdl-org/kdl/blob/main/SPEC.md#identifier).
Expand Down Expand Up @@ -94,7 +94,7 @@ impl KdlIdentifier {
/// parse the string as a KDL v2 entry, and, if that fails, it will try
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse(s: &str) -> Result<Self, KdlError> {
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::try_parse(v2_parser::identifier, s)
Expand All @@ -108,7 +108,7 @@ impl KdlIdentifier {

/// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlIdentifier, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl From<KdlIdentifier> for String {
}

impl FromStr for KdlIdentifier {
type Err = KdlParseFailure;
type Err = KdlError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
KdlIdentifier::parse(s)
Expand Down
10 changes: 5 additions & 5 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{
use miette::SourceSpan;

use crate::{
v2_parser, FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlIdentifier,
KdlParseFailure, KdlValue,
v2_parser, FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlError, KdlIdentifier,
KdlValue,
};

/// Represents an individual KDL
Expand Down Expand Up @@ -332,7 +332,7 @@ impl KdlNode {
/// parse the string as a KDL v2 node, and, if that fails, it will try
/// to parse again as a KDL v1 node. If both fail, only the v2 parse
/// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse(s: &str) -> Result<Self, KdlError> {
#[cfg(not(feature = "v1-fallback"))]
{
v2_parser::try_parse(v2_parser::padded_node, s)
Expand All @@ -346,7 +346,7 @@ impl KdlNode {

/// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlParseFailure> {
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlNode, kdlv1::KdlError> = s.parse();
ret.map(|x| x.into()).map_err(|e| e.into())
}
Expand Down Expand Up @@ -753,7 +753,7 @@ impl IndexMut<&str> for KdlNode {
}

impl FromStr for KdlNode {
type Err = KdlParseFailure;
type Err = KdlError;

fn from_str(input: &str) -> Result<Self, Self::Err> {
v2_parser::try_parse(v2_parser::padded_node, input)
Expand Down
52 changes: 26 additions & 26 deletions src/v2_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use winnow::{
};

use crate::{
KdlDiagnostic, KdlDocument, KdlDocumentFormat, KdlEntry, KdlEntryFormat, KdlIdentifier,
KdlNode, KdlNodeFormat, KdlParseFailure, KdlValue,
KdlDiagnostic, KdlDocument, KdlDocumentFormat, KdlEntry, KdlEntryFormat, KdlError,
KdlIdentifier, KdlNode, KdlNodeFormat, KdlValue,
};

type Input<'a> = Recoverable<Located<&'a str>, KdlParseError>;
Expand All @@ -30,7 +30,7 @@ type PResult<T> = winnow::PResult<T, KdlParseError>;
pub(crate) fn try_parse<'a, P: Parser<Input<'a>, T, KdlParseError>, T>(
mut parser: P,
input: &'a str,
) -> Result<T, KdlParseFailure> {
) -> Result<T, KdlError> {
let (_, maybe_val, errs) = parser.recoverable_parse(Located::new(input));
if let (Some(v), true) = (maybe_val, errs.is_empty()) {
Ok(v)
Expand All @@ -39,9 +39,9 @@ pub(crate) fn try_parse<'a, P: Parser<Input<'a>, T, KdlParseError>, T>(
}
}

pub(crate) fn failure_from_errs(errs: Vec<KdlParseError>, input: &str) -> KdlParseFailure {
pub(crate) fn failure_from_errs(errs: Vec<KdlParseError>, input: &str) -> KdlError {
let src = Arc::new(String::from(input));
KdlParseFailure {
KdlError {
input: src.clone(),
diagnostics: errs
.into_iter()
Expand Down Expand Up @@ -2050,13 +2050,13 @@ impl_negatable_unsigned!(u8, u16, u32, u64, u128, usize);
mod failure_tests {
use miette::Severity;

use crate::{KdlDiagnostic, KdlDocument, KdlParseFailure};
use crate::{KdlDiagnostic, KdlDocument, KdlError};
use std::sync::Arc;

#[test]
fn bad_node_name_test() -> miette::Result<()> {
let input = Arc::new("foo { bar; { baz; }; }".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// super::_print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand All @@ -2076,7 +2076,7 @@ mod failure_tests {
))
);
let input = Arc::new("no/de 1 {\n 1 2 foo\n bad#\n}".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// super::_print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand Down Expand Up @@ -2133,7 +2133,7 @@ mod failure_tests {
#[test]
fn bad_entry_number_test() -> miette::Result<()> {
let input = Arc::new("node 1asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// super::_print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand All @@ -2152,7 +2152,7 @@ mod failure_tests {
);

let input = Arc::new("node 0x1asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2169,7 +2169,7 @@ mod failure_tests {
);

let input = Arc::new("node 0o1asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2186,7 +2186,7 @@ mod failure_tests {
);

let input = Arc::new("node 0b1asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2203,7 +2203,7 @@ mod failure_tests {
);

let input = Arc::new("node 1.0asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2220,7 +2220,7 @@ mod failure_tests {
);

let input = Arc::new("node 1.asdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2237,7 +2237,7 @@ mod failure_tests {
);

let input = Arc::new("node 1.0easdf 2".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2263,7 +2263,7 @@ mod failure_tests {
#[test]
fn bad_string_test() -> miette::Result<()> {
let input = Arc::new("node \" 1".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand All @@ -2280,7 +2280,7 @@ mod failure_tests {
);

let input = Arc::new("node \"foo\"1".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// if let Err(e) = res {
// println!("{:?}", miette::Report::from(e));
// }
Expand All @@ -2300,7 +2300,7 @@ mod failure_tests {
);

let input = Arc::new("node \"\nlet's do multiline!\"".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
assert_eq!(
res,
Err(mkfail(
Expand Down Expand Up @@ -2331,7 +2331,7 @@ mod failure_tests {
#[test]
fn bad_child_test() -> miette::Result<()> {
let input = Arc::new("node {".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// _print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand All @@ -2350,7 +2350,7 @@ mod failure_tests {
);

let input = Arc::new("node {}}".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// _print_diagnostic(res);
// return Ok(());
// println!("{res:#?}");
Expand All @@ -2370,7 +2370,7 @@ mod failure_tests {
);

let input = Arc::new("node }{".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// _print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand Down Expand Up @@ -2421,7 +2421,7 @@ mod failure_tests {
);

let input = Arc::new("node {\n".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// _print_diagnostic(res);
// return Ok(());
assert_eq!(
Expand Down Expand Up @@ -2450,7 +2450,7 @@ mod failure_tests {
);

let input = Arc::new("node {\nnode2{{}}".to_string());
let res: Result<KdlDocument, KdlParseFailure> = input.parse();
let res: Result<KdlDocument, KdlError> = input.parse();
// _print_diagnostic(res);
// return Ok(());
println!("{res:#?}");
Expand Down Expand Up @@ -2504,13 +2504,13 @@ mod failure_tests {
Ok(())
}

fn mkfail(input: Arc<String>, diagnostics: Vec<KdlDiagnostic>) -> KdlParseFailure {
KdlParseFailure { input, diagnostics }
fn mkfail(input: Arc<String>, diagnostics: Vec<KdlDiagnostic>) -> KdlError {
KdlError { input, diagnostics }
}
}

#[cfg(test)]
fn _print_diagnostic<T>(res: Result<T, KdlParseFailure>) {
fn _print_diagnostic<T>(res: Result<T, KdlError>) {
if let Err(e) = res {
println!("{:?}", miette::Report::from(e));
}
Expand Down
Loading

0 comments on commit 12b2fd2

Please sign in to comment.