use std::fmt;
use std::path::Path;
use crate::source::SourceSpan;
pub(crate) mod catalog;
pub use catalog::BuiltinDiagnostic;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Diagnostic {
pub code: &'static str,
pub message: String,
pub span: SourceSpan,
pub notes: Vec<String>,
}
impl Diagnostic {
#[must_use]
pub fn new(code: &'static str, message: impl Into<String>, span: SourceSpan) -> Self {
Self {
code,
message: message.into(),
span,
notes: Vec::new(),
}
}
#[must_use]
pub fn builtin(
diagnostic: BuiltinDiagnostic,
message: impl Into<String>,
span: SourceSpan,
) -> Self {
Self {
code: diagnostic.code(),
message: message.into(),
span,
notes: Vec::new(),
}
}
#[must_use]
pub fn note(mut self, note: impl Into<String>) -> Self {
self.notes.push(note.into());
self
}
#[must_use]
pub fn io(code: &'static str, path: &Path, error: &std::io::Error) -> Self {
Self {
code,
message: format!("could not access `{}`: {error}", path.display()),
span: SourceSpan::file_start(path),
notes: Vec::new(),
}
}
#[must_use]
pub fn builtin_io(diagnostic: BuiltinDiagnostic, path: &Path, error: &std::io::Error) -> Self {
Self::builtin(
diagnostic,
format!("could not access `{}`: {error}", path.display()),
SourceSpan::file_start(path),
)
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}:{}:{} [{}]\n\n{}",
self.span.file().display(),
self.span.line,
self.span.column,
self.code,
self.message
)?;
if let Some(line) = self
.span
.source()
.text()
.lines()
.nth(self.span.line.saturating_sub(1))
{
write!(
f,
"\n\n{line}\n{}^",
" ".repeat(self.span.column.saturating_sub(1))
)?;
}
for note in &self.notes {
write!(f, "\n\nnote: {note}")?;
}
Ok(())
}
}
impl std::error::Error for Diagnostic {}
pub type Result<T> = std::result::Result<T, Diagnostic>;
#[cfg(test)]
mod tests {
use std::io;
use std::path::Path;
use super::{BuiltinDiagnostic, Diagnostic};
use crate::source::SourceSpan;
#[test]
#[allow(
clippy::disallowed_methods,
reason = "the test verifies the custom diagnostic constructor retained for embedding applications"
)]
fn custom_and_builtin_construction_preserve_the_public_shape() {
let span = SourceSpan::file_start("program.clipasm");
let custom = Diagnostic::new("E_EMBEDDER_TEST", "custom failure", span.clone());
assert_eq!(custom.code, "E_EMBEDDER_TEST");
assert_eq!(custom.message, "custom failure");
assert_eq!(custom.span, span);
assert!(custom.notes.is_empty());
let builtin = Diagnostic::builtin(
BuiltinDiagnostic::UnknownProgram,
"unknown program `missing`",
span,
)
.note("check the import");
assert_eq!(builtin.code, "E_UNKNOWN_PROGRAM");
assert_eq!(builtin.message, "unknown program `missing`");
assert_eq!(builtin.notes, ["check the import"]);
}
#[test]
fn builtin_io_uses_the_typed_code_and_path_location() {
let path = Path::new("missing.clipasm");
let error = io::Error::new(io::ErrorKind::NotFound, "not found");
let diagnostic = Diagnostic::builtin_io(BuiltinDiagnostic::SourceIo, path, &error);
assert_eq!(diagnostic.code, "E_SOURCE_IO");
assert_eq!(diagnostic.span.file(), path);
assert!(diagnostic.message.contains("missing.clipasm"));
assert!(diagnostic.message.contains("not found"));
}
}