1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::num::{ParseFloatError, ParseIntError};
use nom::error::{ContextError, ErrorKind, FromExternalError, ParseError};
use thiserror::Error;
#[cfg(doc)]
use {
crate::KdlNode,
std::convert::{TryFrom, TryInto},
};
#[derive(Debug, Clone, Eq, PartialEq, Error)]
#[error("Error parsing document at line {line} column {column}. {kind}")]
pub struct KdlError {
pub input: String,
pub offset: usize,
pub line: usize,
pub column: usize,
pub kind: KdlErrorKind,
}
#[derive(Debug, Clone, Eq, PartialEq, Error)]
pub enum KdlErrorKind {
#[error(transparent)]
ParseIntError(ParseIntError),
#[error(transparent)]
ParseFloatError(ParseFloatError),
#[error("Failed to parse {0} component of semver string.")]
Context(&'static str),
#[error("An unspecified error occurred.")]
Other,
}
#[derive(Debug, Clone, Eq, PartialEq, Error)]
#[error("Failed to convert from KdlNodeValue::{variant} to {expected}.")]
pub struct TryFromKdlNodeValueError {
pub(crate) expected: &'static str,
pub(crate) variant: &'static str,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct KdlParseError<I> {
pub(crate) input: I,
pub(crate) context: Option<&'static str>,
pub(crate) kind: Option<KdlErrorKind>,
}
impl<I> ParseError<I> for KdlParseError<I> {
fn from_error_kind(input: I, _kind: nom::error::ErrorKind) -> Self {
Self {
input,
context: None,
kind: None,
}
}
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 = 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 {
KdlParseError {
input,
context: None,
kind: Some(KdlErrorKind::ParseIntError(e)),
}
}
}
impl<'a> FromExternalError<&'a str, ParseFloatError> for KdlParseError<&'a str> {
fn from_external_error(input: &'a str, _kind: ErrorKind, e: ParseFloatError) -> Self {
KdlParseError {
input,
context: None,
kind: Some(KdlErrorKind::ParseFloatError(e)),
}
}
}