#![warn(unused_crate_dependencies)]
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
pub use alloc::{boxed::Box, format, string::String};
use core::fmt;
pub use indexer::{Indexer, LineIndexer};
pub use span::{SimpleSpan, Span};
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
mod indexer;
mod span;
#[cfg(feature = "annotate-snippets")]
mod annotate_snippets_impl;
#[cfg(feature = "ariadne")]
mod ariadne_impl;
#[cfg(feature = "codespan-reporting")]
mod codespan_reporting_impl;
#[cfg(feature = "miette")]
mod miette_impl;
#[derive(Clone, Copy, Default)]
pub enum Kind {
#[default]
Error,
Warn,
}
impl Kind {
pub fn short_str(&self) -> &'static str {
match self {
Kind::Error => "E",
Kind::Warn => "W",
}
}
}
pub trait ErrorType: core::error::Error {
type Span: Span + Default;
type Message: fmt::Display;
fn kind(&self) -> Kind;
fn number(&self) -> &str;
fn code(&self) -> &str;
fn primary_span(&self) -> Option<Self::Span>;
fn primary_message(&self) -> Self::Message;
fn primary_label(&self) -> Self::Message;
fn primary(&self) -> (Option<Self::Span>, Self::Message, Self::Message) {
(
self.primary_span(),
self.primary_message(),
self.primary_label(),
)
}
fn additional(
&self,
) -> Box<dyn Iterator<Item = (Option<Self::Span>, Self::Message, Self::Message)>>;
}
impl<T: ErrorType + ?Sized> ErrorType for &T {
type Span = T::Span;
type Message = T::Message;
#[inline]
fn kind(&self) -> Kind {
(*self).kind()
}
#[inline]
fn number(&self) -> &str {
(*self).number()
}
#[inline]
fn code(&self) -> &str {
(*self).code()
}
#[inline]
fn primary_span(&self) -> Option<Self::Span> {
(*self).primary_span()
}
#[inline]
fn primary_message(&self) -> Self::Message {
(*self).primary_message()
}
#[inline]
fn primary_label(&self) -> Self::Message {
(*self).primary_label()
}
#[inline]
fn primary(&self) -> (Option<Self::Span>, Self::Message, Self::Message) {
(*self).primary()
}
#[inline]
fn additional(
&self,
) -> Box<dyn Iterator<Item = (Option<Self::Span>, Self::Message, Self::Message)>> {
(*self).additional()
}
}
pub trait ErrorTypeExt: ErrorType {
#[cfg(feature = "annotate-snippets")]
#[cfg_attr(docsrs, doc(cfg(feature = "annotate-snippets")))]
fn fmt_as_annotate_snippets(&self) -> String {
annotate_snippets_impl::fmt_as_annotate_snippets(
self,
annotate_snippets::display_list::FormatOptions::default(),
)
}
#[cfg(feature = "annotate-snippets")]
#[cfg_attr(docsrs, doc(cfg(feature = "annotate-snippets")))]
fn fmt_as_annotate_snippets_with_opts(
&self,
opts: annotate_snippets::display_list::FormatOptions,
) -> String {
annotate_snippets_impl::fmt_as_annotate_snippets(self, opts)
}
#[cfg(feature = "ariadne")]
#[cfg_attr(docsrs, doc(cfg(feature = "ariadne")))]
fn fmt_as_ariadne_report(&self) -> Result<String, std::io::Error> {
ariadne_impl::fmt_as_ariadne_report(
self,
ariadne::Config::new().with_index_type(ariadne::IndexType::Byte),
)
}
#[cfg(feature = "ariadne")]
#[cfg_attr(docsrs, doc(cfg(feature = "ariadne")))]
fn fmt_as_ariadne_report_with(
&self,
config: ariadne::Config,
) -> Result<String, std::io::Error> {
ariadne_impl::fmt_as_ariadne_report(self, config)
}
#[cfg(feature = "codespan-reporting")]
#[cfg_attr(docsrs, doc(cfg(feature = "codespan-reporting")))]
fn as_codespan_diagnostic(
&self,
) -> (
codespan_reporting::diagnostic::Diagnostic<usize>,
codespan_reporting_impl::Files<Self>,
) {
codespan_reporting_impl::to_codespan_diagnostic(self)
}
#[cfg(feature = "codespan-reporting")]
#[cfg_attr(docsrs, doc(cfg(feature = "codespan-reporting")))]
fn fmt_as_codespan_diagnostic_with(
&self,
config: codespan_reporting::term::Config,
styles: Option<&codespan_reporting::term::Styles>,
) -> Result<String, codespan_reporting::files::Error> {
codespan_reporting_impl::fmt_as_codespan_diagnostic(self, config, styles)
}
#[cfg(feature = "miette")]
#[cfg_attr(docsrs, doc(cfg(feature = "miette")))]
fn as_miette_diagnostic(&self) -> impl miette::Diagnostic + '_
where
Self::Span: Send + Sync,
{
miette_impl::Wrapper::new(self)
}
#[cfg(feature = "miette")]
#[cfg_attr(docsrs, doc(cfg(feature = "miette")))]
fn fmt_as_miette_diagnostic_with(&self, handler: &impl miette::ReportHandler) -> String
where
Self: 'static + Sized,
Self::Span: Send + Sync,
{
miette_impl::Wrapper::new(self).fmt_with(handler)
}
}
impl<T: ErrorType + ?Sized> ErrorTypeExt for T {}