feat(error): Rename KdlParseFailure back to KdlError

This commit is contained in:
Kat Marchán 2024-12-20 01:27:56 -08:00
parent c486cda7a5
commit 12b2fd2f4f
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
7 changed files with 53 additions and 53 deletions

View File

@ -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).
@ -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)
@ -355,7 +355,7 @@ 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())
}
@ -363,7 +363,7 @@ impl KdlDocument {
/// 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())
@ -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)

View File

@ -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
@ -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)
@ -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())
}
@ -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)

View File

@ -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>,
@ -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,

View File

@ -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).
@ -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)
@ -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())
}
@ -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)

View File

@ -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
@ -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)
@ -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())
}
@ -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)

View File

@ -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>;
@ -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)
@ -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()
@ -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!(
@ -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!(
@ -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!(
@ -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(
@ -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(
@ -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(
@ -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(
@ -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(
@ -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(
@ -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(
@ -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));
// }
@ -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(
@ -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!(
@ -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:#?}");
@ -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!(
@ -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!(
@ -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:#?}");
@ -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));
}

View File

@ -4,7 +4,7 @@ use std::{
path::{Path, PathBuf},
};
use kdl::{KdlDocument, KdlIdentifier, KdlParseFailure, KdlValue};
use kdl::{KdlDocument, KdlError, KdlIdentifier, KdlValue};
use miette::{Diagnostic, IntoDiagnostic};
use thiserror::Error;
@ -20,11 +20,11 @@ struct ComplianceSuiteFailure {
enum ComplianceDiagnostic {
#[error("{}", PathBuf::from(.0.file_name().unwrap()).display())]
#[diagnostic(code(kdl::compliance::parse_failure))]
KdlParseFailure(
KdlError(
PathBuf,
#[source]
#[diagnostic_source]
KdlParseFailure,
KdlError,
),
#[error("{}:\nExpected:\n{expected}\nActual:\n{actual}", PathBuf::from(file.file_name().unwrap()).display())]
@ -74,7 +74,7 @@ fn spec_compliance() -> miette::Result<()> {
}
fn validate_res(
res: Result<KdlDocument, KdlParseFailure>,
res: Result<KdlDocument, KdlError>,
path: &Path,
src: &str,
) -> Result<(), ComplianceDiagnostic> {
@ -88,7 +88,7 @@ fn validate_res(
let expected_path = expected_dir.join(file_name);
let underscored = expected_dir.join(format!("_{}", PathBuf::from(file_name).display()));
if expected_path.exists() {
let doc = res.map_err(|e| ComplianceDiagnostic::KdlParseFailure(path.into(), e))?;
let doc = res.map_err(|e| ComplianceDiagnostic::KdlError(path.into(), e))?;
let expected = normalize_line_endings(fs::read_to_string(&expected_path)?);
let actual = stringify_to_expected(doc);
if actual != expected {
@ -105,7 +105,7 @@ fn validate_res(
PathBuf::from(file_name).display()
);
// } else {
// res.map_err(|e| ComplianceDiagnostic::KdlParseFailure(path.into(), e))?;
// res.map_err(|e| ComplianceDiagnostic::KdlError(path.into(), e))?;
}
Ok(())
}