Skip to content

Commit f31750a

Browse files
authored
feat(errors): error collection/recovery and reporting improvements (#94)
Fixes: #93
1 parent cbadb35 commit f31750a

File tree

4 files changed

+1013
-283
lines changed

4 files changed

+1013
-283
lines changed

src/document.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ final;";
531531
// if you're making KdlEntries this way, you need to inject
532532
// your own whitespace (or format the node)
533533
node.push(" \"blah\"=0xDEADbeef".parse::<KdlEntry>()?);
534-
dbg!(&node);
535534
doc.nodes_mut().push(node);
536535

537536
assert_eq!(
@@ -887,7 +886,7 @@ inline { time; to; live "our" "dreams"; "y;all" }
887886
check_span("time", inline_nodes[0].span(), &input);
888887
check_span("to", inline_nodes[1].span(), &input);
889888
check_span(r#"live "our" "dreams""#, inline_nodes[2].span(), &input);
890-
check_span(r#""y;all""#, inline_nodes[3].span(), &input);
889+
check_span(r#""y;all" "#, inline_nodes[3].span(), &input);
891890

892891
Ok(())
893892
}

src/error.rs

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
num::{ParseFloatError, ParseIntError},
3-
sync::Arc,
4-
};
1+
use std::sync::Arc;
52

63
use miette::{Diagnostic, SourceSpan};
74
use thiserror::Error;
@@ -38,7 +35,7 @@ use {
3835
/// help: Floating point numbers must be base 10, and have numbers after the decimal point.
3936
/// ```
4037
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
41-
#[error("Failed to parse KDL.")]
38+
#[error("Failed to parse KDL document")]
4239
pub struct KdlParseFailure {
4340
/// Original input that this failure came from.
4441
#[source_code]
@@ -53,58 +50,27 @@ pub struct KdlParseFailure {
5350
///
5451
/// While generally signifying errors, they can also be treated as warnings.
5552
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
56-
#[error("{kind}")]
53+
#[error("{}", message.clone().unwrap_or_else(|| "Unexpected error".into()))]
5754
pub struct KdlDiagnostic {
5855
/// Shared source for the diagnostic.
5956
#[source_code]
6057
pub input: Arc<String>,
6158

6259
/// Offset in chars of the error.
63-
#[label("{}", label.unwrap_or("here"))]
60+
#[label("{}", label.clone().unwrap_or_else(|| "here".into()))]
6461
pub span: SourceSpan,
6562

63+
/// Message for the error itself.
64+
pub message: Option<String>,
65+
6666
/// Label text for this span. Defaults to `"here"`.
67-
pub label: Option<&'static str>,
67+
pub label: Option<String>,
6868

6969
/// Suggestion for fixing the parser error.
7070
#[help]
71-
pub help: Option<&'static str>,
71+
pub help: Option<String>,
7272

7373
/// Severity level for the Diagnostic.
7474
#[diagnostic(severity)]
7575
pub severity: miette::Severity,
76-
77-
/// Specific error kind for this parser error.
78-
pub kind: KdlErrorKind,
79-
}
80-
81-
/// A type representing additional information specific to the type of error being returned.
82-
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
83-
pub enum KdlErrorKind {
84-
/// An error occurred while parsing an integer.
85-
#[error(transparent)]
86-
#[diagnostic(code(kdl::parse_int))]
87-
ParseIntError(ParseIntError),
88-
89-
/// An error occurred while parsing a floating point number.
90-
#[error(transparent)]
91-
#[diagnostic(code(kdl::parse_float))]
92-
ParseFloatError(ParseFloatError),
93-
94-
/// Tried to parse a negative number as an unsigned integer.
95-
#[error("Tried to parse a negative number as an unsigned integer.")]
96-
#[diagnostic(code(kdl::negative_unsigned))]
97-
NegativeUnsignedError,
98-
99-
/// Generic parsing error. The given context string denotes the component
100-
/// that failed to parse.
101-
#[error("Expected {0}.")]
102-
#[diagnostic(code(kdl::parse_component))]
103-
Context(&'static str),
104-
105-
/// Generic unspecified error. If this is returned, the call site should
106-
/// be annotated with context, if possible.
107-
#[error("An unspecified parse error occurred.")]
108-
#[diagnostic(code(kdl::other))]
109-
Other,
11076
}

0 commit comments

Comments
 (0)