use std::fmt;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub struct Location {
pub file: Arc<str>,
pub line: u32,
pub column: u32,
pub len: u32,
}
impl Location {
pub fn new(file: impl Into<Arc<str>>, line: u32, column: u32, len: u32) -> Self {
Self {
file: file.into(),
line,
column,
len,
}
}
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}:{}", self.file, self.line, self.column)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Diagnostic {
pub location: Option<Location>,
pub message: String,
pub hint: Option<String>,
pub related: Option<Location>,
}
impl Diagnostic {
pub fn new(location: Option<Location>, message: impl Into<String>) -> Self {
Self {
location,
message: message.into(),
hint: None,
related: None,
}
}
#[must_use]
pub fn with_hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
#[must_use]
pub fn with_related(mut self, related: Option<Location>) -> Self {
self.related = related;
self
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.location {
Some(at) => write!(f, "{at}: {}", self.message)?,
None => f.write_str(&self.message)?,
}
if let Some(other) = &self.related {
write!(f, " (also at {other})")?;
}
if let Some(hint) = &self.hint {
write!(f, "\n hint: {hint}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostics(Vec<Diagnostic>);
impl Diagnostics {
pub fn new(items: Vec<Diagnostic>) -> Self {
Self(items)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> std::slice::Iter<'_, Diagnostic> {
self.0.iter()
}
pub fn first(&self) -> Option<&Diagnostic> {
self.0.first()
}
pub fn into_vec(self) -> Vec<Diagnostic> {
self.0
}
}
impl IntoIterator for Diagnostics {
type Item = Diagnostic;
type IntoIter = std::vec::IntoIter<Diagnostic>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a Diagnostics {
type Item = &'a Diagnostic;
type IntoIter = std::slice::Iter<'a, Diagnostic>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl fmt::Display for Diagnostics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, item) in self.0.iter().enumerate() {
if i > 0 {
writeln!(f)?;
}
write!(f, "{item}")?;
}
Ok(())
}
}
impl std::error::Error for Diagnostics {}
pub(crate) const MAX_DIAGNOSTICS: usize = 100;
pub(crate) fn capped(mut items: Vec<Diagnostic>, hidden: usize) -> Vec<Diagnostic> {
let mut more = hidden;
if items.len() > MAX_DIAGNOSTICS {
more += items.len() - MAX_DIAGNOSTICS;
items.truncate(MAX_DIAGNOSTICS);
}
if more > 0 {
items.push(Diagnostic::new(None, format!("and {more} more errors not shown")));
}
items
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PrintError {
#[error("cannot print {what}: {reason}")]
Unrepresentable { what: String, reason: String },
#[error("the {0} syntax is not available yet")]
Unsupported(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Span {
pub line: u32,
pub col: u32,
pub len: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Raw {
pub span: Span,
pub message: String,
pub hint: Option<String>,
}
impl Raw {
pub fn new(span: Span, message: impl Into<String>) -> Self {
Self {
span,
message: message.into(),
hint: None,
}
}
pub fn hint(mut self, hint: impl Into<String>) -> Self {
self.hint = Some(hint.into());
self
}
pub fn locate(self, file: &Arc<str>) -> Diagnostic {
Diagnostic {
location: Some(Location {
file: Arc::clone(file),
line: self.span.line,
column: self.span.col,
len: self.span.len.max(1),
}),
message: self.message,
hint: self.hint,
related: None,
}
}
}
pub(crate) type PResult<T> = Result<T, Raw>;