diff --git a/Cargo.toml b/Cargo.toml index 446344d..c7867d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,17 +22,16 @@ members = ["tools/*"] [dependencies] miette.workspace = true -thiserror.workspace = true num = "0.4.2" winnow = { version = "=0.6.24", features = ["alloc", "unstable-recover"] } kdlv1 = { package = "kdl", version = "4.7.0", optional = true } [workspace.dependencies] -miette = "7.2.0" -thiserror = "1.0.40" +miette = { git = "https://github.com/TheLostLambda/miette.git" } [dev-dependencies] miette = { workspace = true, features = ["fancy"] } +thiserror = "2.0.12" pretty_assertions = "1.3.0" # The profile that 'dist' will build with diff --git a/src/error.rs b/src/error.rs index 1c210ab..d4b3a9a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,6 @@ -use std::sync::Arc; +use std::{error::Error, fmt::Display, sync::Arc}; use miette::{Diagnostic, SourceSpan}; -use thiserror::Error; #[cfg(doc)] use { @@ -34,8 +33,7 @@ use { /// ╰──── /// help: Floating point numbers must be base 10, and have numbers after the decimal point. /// ``` -#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)] -#[error("Failed to parse KDL document")] +#[derive(Debug, Diagnostic, Clone, Eq, PartialEq)] pub struct KdlError { /// Original input that this failure came from. #[source_code] @@ -46,11 +44,17 @@ pub struct KdlError { pub diagnostics: Vec, } +impl Display for KdlError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Failed to parse KDL document") + } +} +impl Error for KdlError {} + /// An individual diagnostic message for a KDL parsing issue. /// /// While generally signifying errors, they can also be treated as warnings. -#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)] -#[error("{}", message.clone().unwrap_or_else(|| "Unexpected error".into()))] +#[derive(Debug, Diagnostic, Clone, Eq, PartialEq)] pub struct KdlDiagnostic { /// Shared source for the diagnostic. #[source_code] @@ -75,6 +79,17 @@ pub struct KdlDiagnostic { pub severity: miette::Severity, } +impl Display for KdlDiagnostic { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let message = self + .message + .clone() + .unwrap_or_else(|| "Unexpected error".into()); + write!(f, "{message}") + } +} +impl Error for KdlDiagnostic {} + #[cfg(feature = "v1")] impl From for KdlError { fn from(value: kdlv1::KdlError) -> Self { @@ -92,3 +107,41 @@ impl From for KdlError { } } } + +#[cfg(test)] +mod tests { + use std::error::Error; + + use super::*; + + #[test] + fn kdl_error() { + let kdl_error = KdlError { + input: Default::default(), + diagnostics: Default::default(), + }; + + assert_eq!(kdl_error.to_string(), "Failed to parse KDL document"); + assert!(kdl_error.source().is_none()); + } + + #[test] + fn kdl_diagnostic() { + let mut kdl_diagnostic = KdlDiagnostic { + input: Default::default(), + span: SourceSpan::new(0.into(), 1), + message: Default::default(), + label: Default::default(), + help: Default::default(), + severity: Default::default(), + }; + + assert_eq!(kdl_diagnostic.to_string(), "Unexpected error"); + assert!(kdl_diagnostic.source().is_none()); + + kdl_diagnostic.message = Some("mega bad news, kiddo".to_owned()); + + assert_eq!(kdl_diagnostic.to_string(), "mega bad news, kiddo"); + assert!(kdl_diagnostic.source().is_none()); + } +}