From 12b2fd2f4f58ba9b112842098bd041f527d04dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 20 Dec 2024 01:27:56 -0800 Subject: [PATCH] feat(error): Rename KdlParseFailure back to KdlError --- src/document.rs | 10 ++++----- src/entry.rs | 8 +++---- src/error.rs | 6 +++--- src/identifier.rs | 8 +++---- src/node.rs | 10 ++++----- src/v2_parser.rs | 52 ++++++++++++++++++++++----------------------- tests/compliance.rs | 12 +++++------ 7 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/document.rs b/src/document.rs index a2c55b8..f52930e 100644 --- a/src/document.rs +++ b/src/document.rs @@ -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 { + pub fn parse(s: &str) -> Result { #[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 { + pub fn parse_v1(s: &str) -> Result { let ret: Result = 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 { + pub fn v1_to_v2(s: &str) -> Result { let mut doc = KdlDocument::parse_v1(s)?; doc.autoformat(); Ok(doc.to_string()) @@ -386,7 +386,7 @@ impl From for KdlDocument { } impl std::str::FromStr for KdlDocument { - type Err = KdlParseFailure; + type Err = KdlError; fn from_str(s: &str) -> Result { KdlDocument::parse(s) diff --git a/src/entry.rs b/src/entry.rs index 0a71a30..f6fc3e8 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -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 { + pub fn parse(s: &str) -> Result { #[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 { + pub fn parse_v1(s: &str) -> Result { let ret: Result = 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 { KdlEntry::parse(s) diff --git a/src/error.rs b/src/error.rs index 16aaac8..1c210ab 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, @@ -76,10 +76,10 @@ pub struct KdlDiagnostic { } #[cfg(feature = "v1")] -impl From for KdlParseFailure { +impl From for KdlError { fn from(value: kdlv1::KdlError) -> Self { let input = Arc::new(value.input); - KdlParseFailure { + KdlError { input: input.clone(), diagnostics: vec![KdlDiagnostic { input, diff --git a/src/identifier.rs b/src/identifier.rs index 57d4134..06a7fa0 100644 --- a/src/identifier.rs +++ b/src/identifier.rs @@ -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 { + pub fn parse(s: &str) -> Result { #[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 { + pub fn parse_v1(s: &str) -> Result { let ret: Result = s.parse(); ret.map(|x| x.into()).map_err(|e| e.into()) } @@ -165,7 +165,7 @@ impl From for String { } impl FromStr for KdlIdentifier { - type Err = KdlParseFailure; + type Err = KdlError; fn from_str(s: &str) -> Result { KdlIdentifier::parse(s) diff --git a/src/node.rs b/src/node.rs index ed8f331..ab930a0 100644 --- a/src/node.rs +++ b/src/node.rs @@ -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 { + pub fn parse(s: &str) -> Result { #[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 { + pub fn parse_v1(s: &str) -> Result { let ret: Result = 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 { v2_parser::try_parse(v2_parser::padded_node, input) diff --git a/src/v2_parser.rs b/src/v2_parser.rs index 3434e7d..73986c3 100644 --- a/src/v2_parser.rs +++ b/src/v2_parser.rs @@ -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, KdlParseError>; @@ -30,7 +30,7 @@ type PResult = winnow::PResult; pub(crate) fn try_parse<'a, P: Parser, T, KdlParseError>, T>( mut parser: P, input: &'a str, -) -> Result { +) -> Result { 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, T, KdlParseError>, T>( } } -pub(crate) fn failure_from_errs(errs: Vec, input: &str) -> KdlParseFailure { +pub(crate) fn failure_from_errs(errs: Vec, 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = 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 = input.parse(); + let res: Result = input.parse(); // _print_diagnostic(res); // return Ok(()); println!("{res:#?}"); @@ -2504,13 +2504,13 @@ mod failure_tests { Ok(()) } - fn mkfail(input: Arc, diagnostics: Vec) -> KdlParseFailure { - KdlParseFailure { input, diagnostics } + fn mkfail(input: Arc, diagnostics: Vec) -> KdlError { + KdlError { input, diagnostics } } } #[cfg(test)] -fn _print_diagnostic(res: Result) { +fn _print_diagnostic(res: Result) { if let Err(e) = res { println!("{:?}", miette::Report::from(e)); } diff --git a/tests/compliance.rs b/tests/compliance.rs index d27456f..6e6fded 100644 --- a/tests/compliance.rs +++ b/tests/compliance.rs @@ -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, + res: Result, 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(()) }