use std::{
fmt::{self, Display},
fs,
panic::Location,
};
use crate::MietteError;
pub trait Diagnostic: std::error::Error {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}
fn severity(&self) -> Option<Severity> {
None
}
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}
fn snippets<'a>(&'a self) -> Option<Box<dyn Iterator<Item = DiagnosticSnippet<'a>> + 'a>> {
None
}
}
impl std::error::Error for Box<dyn Diagnostic> {}
impl<T: Diagnostic + Send + Sync + 'static> From<T>
for Box<dyn Diagnostic + Send + Sync + 'static>
{
fn from(diag: T) -> Self {
Box::new(diag)
}
}
impl<T: Diagnostic + Send + Sync + 'static> From<T> for Box<dyn Diagnostic + Send + 'static> {
fn from(diag: T) -> Self {
Box::<dyn Diagnostic + Send + Sync>::from(diag)
}
}
impl<T: Diagnostic + Send + Sync + 'static> From<T> for Box<dyn Diagnostic + 'static> {
fn from(diag: T) -> Self {
Box::<dyn Diagnostic + Send + Sync>::from(diag)
}
}
impl From<&str> for Box<dyn Diagnostic> {
fn from(s: &str) -> Self {
From::from(String::from(s))
}
}
impl<'a> From<&str> for Box<dyn Diagnostic + Send + Sync + 'a> {
fn from(s: &str) -> Self {
From::from(String::from(s))
}
}
impl From<String> for Box<dyn Diagnostic> {
fn from(s: String) -> Self {
let err1: Box<dyn Diagnostic + Send + Sync> = From::from(s);
let err2: Box<dyn Diagnostic> = err1;
err2
}
}
impl From<String> for Box<dyn Diagnostic + Send + Sync> {
fn from(s: String) -> Self {
struct StringError(String);
impl std::error::Error for StringError {}
impl Diagnostic for StringError {}
impl Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl fmt::Debug for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
Box::new(StringError(s))
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for Box<dyn Diagnostic + Send + Sync> {
fn from(s: Box<dyn std::error::Error + Send + Sync>) -> Self {
#[derive(thiserror::Error)]
#[error(transparent)]
struct BoxedDiagnostic(Box<dyn std::error::Error + Send + Sync>);
impl fmt::Debug for BoxedDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl Diagnostic for BoxedDiagnostic {}
Box::new(BoxedDiagnostic(s))
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Severity {
Error,
Warning,
Advice,
}
pub trait Source: std::fmt::Debug + Send + Sync {
fn read_span<'a>(
&'a self,
span: &SourceSpan,
) -> Result<Box<dyn SpanContents + 'a>, MietteError>;
fn name(&self) -> Option<String> {
None
}
}
pub trait SpanContents {
fn data(&self) -> &[u8];
fn line(&self) -> usize;
fn column(&self) -> usize;
}
#[derive(Clone, Debug)]
pub struct MietteSpanContents<'a> {
data: &'a [u8],
line: usize,
column: usize,
}
impl<'a> MietteSpanContents<'a> {
pub fn new(data: &'a [u8], line: usize, column: usize) -> MietteSpanContents<'a> {
MietteSpanContents { data, line, column }
}
}
impl<'a> SpanContents for MietteSpanContents<'a> {
fn data(&self) -> &[u8] {
self.data
}
fn line(&self) -> usize {
self.line
}
fn column(&self) -> usize {
self.column
}
}
#[derive(Clone, Debug)]
pub struct DiagnosticSnippet<'a> {
pub message: Option<String>,
pub source: &'a (dyn Source),
pub context: SourceSpan,
pub highlights: Option<Vec<(Option<String>, SourceSpan)>>,
}
#[derive(Clone, Debug)]
pub struct SourceSpan {
offset: SourceOffset,
length: SourceOffset,
}
impl SourceSpan {
pub fn new(start: SourceOffset, length: SourceOffset) -> Self {
Self {
offset: start,
length,
}
}
pub fn offset(&self) -> usize {
self.offset.offset()
}
pub fn len(&self) -> usize {
self.length.offset()
}
pub fn is_empty(&self) -> bool {
self.length.offset() == 0
}
}
impl From<(ByteOffset, ByteOffset)> for SourceSpan {
fn from((start, len): (ByteOffset, ByteOffset)) -> Self {
Self {
offset: start.into(),
length: len.into(),
}
}
}
impl From<(SourceOffset, SourceOffset)> for SourceSpan {
fn from((start, len): (SourceOffset, SourceOffset)) -> Self {
Self {
offset: start,
length: len,
}
}
}
pub type ByteOffset = usize;
#[derive(Clone, Copy, Debug)]
pub struct SourceOffset(ByteOffset);
impl SourceOffset {
pub fn offset(&self) -> ByteOffset {
self.0
}
pub fn from_location(source: impl AsRef<str>, loc_line: usize, loc_col: usize) -> Self {
let mut line = 0usize;
let mut col = 0usize;
let mut offset = 0usize;
for char in source.as_ref().chars() {
if char == '\n' {
col = 0;
line += 1;
} else {
col += 1;
}
if line + 1 >= loc_line && col + 1 >= loc_col {
break;
}
offset += char.len_utf8();
}
SourceOffset(offset)
}
#[track_caller]
pub fn from_current_location() -> Result<(String, Self), MietteError> {
let loc = Location::caller();
Ok((
loc.file().into(),
fs::read_to_string(loc.file())
.map(|txt| Self::from_location(&txt, loc.line() as usize, loc.column() as usize))?,
))
}
}
impl From<ByteOffset> for SourceOffset {
fn from(bytes: ByteOffset) -> Self {
SourceOffset(bytes)
}
}