#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod additional_attributes;
pub mod code;
pub mod prelude;
pub mod sink;
pub mod traits;
pub use ariadne::{Config, Label};
pub use classified::ClassifiedDiagnostic;
pub use color_palette::ColorPalette;
pub use context::Context;
pub use severity::DiagnosticSeverity;
mod classified;
mod color_palette;
mod context;
mod severity;
use core::borrow::Borrow;
use code::DiagnosticCode;
use traits::DiagnosticGroup;
pub struct Diagnostic<S, D = code::DefaultDiscriminant> {
pub code: DiagnosticCode<D>,
pub span: S,
message: Box<dyn FnOnce() -> String>,
pub context_info: Vec<Context<S>>,
}
impl<S, D> Diagnostic<S, D> {
pub fn new<T>(group_member: impl Borrow<T>, span: S) -> Self
where
T: DiagnosticGroup<D> + ?Sized,
{
let group_member = group_member.borrow();
Self {
code: group_member.diagnostic_code(),
span,
message: group_member.message(),
context_info: Vec::new(),
}
}
pub fn label(&mut self, label: Label<S>) -> &mut Self {
self.context_info.push(Context::Label(label));
self
}
pub fn labels(&mut self, labels: impl IntoIterator<Item = Label<S>>) -> &mut Self {
self.context_info
.extend(labels.into_iter().map(Context::Label));
self
}
pub fn note(&mut self, note: impl ToString) -> &mut Self {
self.context_info.push(Context::new_note(note));
self
}
pub fn help(&mut self, help: impl ToString) -> &mut Self {
self.context_info.push(Context::new_help(help));
self
}
pub const fn classify(self, severity: DiagnosticSeverity) -> ClassifiedDiagnostic<S, D> {
ClassifiedDiagnostic {
inner: self,
severity,
}
}
pub fn report_with<C>(
self,
severity: DiagnosticSeverity,
config: Config,
cache: C,
) -> std::io::Result<()>
where
S: ariadne::Span,
D: code::Discriminant,
C: ariadne::Cache<S::SourceId>,
{
let mut builder = ariadne::Report::build(severity.into(), self.span)
.with_config(config)
.with_code(self.code)
.with_message((self.message)());
for context in self.context_info {
context.add_to_report_builder(&mut builder);
}
builder.finish().eprint(cache)
}
}
#[cfg(any(docsrs, feature = "derive"))]
mod macros {
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
pub use maybe_fatal_derive::*;
}
macro_rules! document_macro_reexports {
[$($derive_macro:ident),* $(,)?] => {
$(
#[cfg(any(docsrs, feature = "derive"))]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
pub use maybe_fatal_derive::$derive_macro;
)*
};
}
document_macro_reexports![
Diagnose,
DiagnosticGroup,
DiagnosticInfoWrapper,
PartialDiagnose,
];