use std::sync::Arc;
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;
#[cfg(doc)]
use {
crate::KdlNode,
std::convert::{TryFrom, TryInto},
};
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("Failed to parse KDL document")]
pub struct KdlError {
#[source_code]
pub input: Arc<String>,
#[related]
pub diagnostics: Vec<KdlDiagnostic>,
}
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("{}", message.clone().unwrap_or_else(|| "Unexpected error".into()))]
pub struct KdlDiagnostic {
#[source_code]
pub input: Arc<String>,
#[label("{}", label.clone().unwrap_or_else(|| "here".into()))]
pub span: SourceSpan,
pub message: Option<String>,
pub label: Option<String>,
#[help]
pub help: Option<String>,
#[diagnostic(severity)]
pub severity: miette::Severity,
}
#[cfg(feature = "v1")]
impl From<kdlv1::KdlError> for KdlError {
fn from(value: kdlv1::KdlError) -> Self {
let input = Arc::new(value.input);
KdlError {
input: input.clone(),
diagnostics: vec![KdlDiagnostic {
input,
span: SourceSpan::new(value.span.offset().into(), value.span.len()),
message: Some(format!("{}", value.kind)),
label: value.label.map(|x| x.into()),
help: value.help.map(|x| x.into()),
severity: miette::Severity::Error,
}],
}
}
}