use super::ShellError;
use crate::Span;
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LabeledError {
pub msg: String,
#[serde(default)]
pub labels: Box<Vec<ErrorLabel>>,
#[serde(default)]
pub code: Option<String>,
#[serde(default)]
pub url: Option<String>,
#[serde(default)]
pub help: Option<String>,
#[serde(default)]
pub inner: Box<Vec<LabeledError>>,
}
impl LabeledError {
pub fn new(msg: impl Into<String>) -> LabeledError {
LabeledError {
msg: msg.into(),
labels: Box::new(vec![]),
code: None,
url: None,
help: None,
inner: Box::new(vec![]),
}
}
pub fn with_label(mut self, text: impl Into<String>, span: Span) -> Self {
self.labels.push(ErrorLabel {
text: text.into(),
span,
});
self
}
pub fn with_code(mut self, code: impl Into<String>) -> Self {
self.code = Some(code.into());
self
}
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
pub fn with_help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
pub fn with_inner(mut self, inner: impl Into<LabeledError>) -> Self {
self.inner.push(inner.into());
self
}
pub fn from_diagnostic(diag: &(impl miette::Diagnostic + ?Sized)) -> LabeledError {
LabeledError {
msg: diag.to_string(),
labels: diag
.labels()
.into_iter()
.flatten()
.map(|label| ErrorLabel {
text: label.label().unwrap_or("").into(),
span: Span::new(label.offset(), label.offset() + label.len()),
})
.collect::<Vec<_>>()
.into(),
code: diag.code().map(|s| s.to_string()),
url: diag.url().map(|s| s.to_string()),
help: diag.help().map(|s| s.to_string()),
inner: diag
.related()
.into_iter()
.flatten()
.map(Self::from_diagnostic)
.collect::<Vec<_>>()
.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErrorLabel {
pub text: String,
pub span: Span,
}
impl fmt::Display for LabeledError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.msg)
}
}
impl std::error::Error for LabeledError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.inner.first().map(|r| r as _)
}
}
impl Diagnostic for LabeledError {
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.code.as_ref().map(Box::new).map(|b| b as _)
}
fn severity(&self) -> Option<miette::Severity> {
None
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.help.as_ref().map(Box::new).map(|b| b as _)
}
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.url.as_ref().map(Box::new).map(|b| b as _)
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
None
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
Some(Box::new(self.labels.iter().map(|label| {
miette::LabeledSpan::new_with_span(
Some(label.text.clone()).filter(|s| !s.is_empty()),
label.span,
)
})))
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
Some(Box::new(self.inner.iter().map(|r| r as _)))
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
None
}
}
impl From<ShellError> for LabeledError {
fn from(err: ShellError) -> Self {
LabeledError::from_diagnostic(&err)
}
}