use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
pub const INTERNAL_SENTINEL: char = '\u{1f}';
pub fn sanitize_internal_encoding(text: &str) -> String {
text.replace(INTERNAL_SENTINEL, " ")
}
pub fn assert_no_internal_encodings(text: &str) {
if text.contains(INTERNAL_SENTINEL) {
panic!("internal encoding leaked into user-facing diagnostic: {text:?}");
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Span {
pub line: usize,
pub col_start: Option<usize>,
pub col_end: Option<usize>,
pub source_line: Option<String>,
}
impl Span {
pub fn line_only(line: usize) -> Self {
Self {
line,
col_start: None,
col_end: None,
source_line: None,
}
}
pub fn full(line: usize, col_start: usize, col_end: usize, source_line: String) -> Self {
Self {
line,
col_start: Some(col_start),
col_end: Some(col_end),
source_line: Some(source_line),
}
}
pub fn line_col(line: usize, col: usize) -> Self {
Self {
line,
col_start: Some(col),
col_end: None,
source_line: None,
}
}
}
pub type DiagnosticResult<T> = Result<T, Diagnostic>;
#[derive(Debug, Clone)]
pub struct Diagnostic(Box<DiagnosticInner>);
#[derive(Debug, Clone)]
struct DiagnosticInner {
key: &'static str,
message: String,
fields: BTreeMap<String, String>,
span: Option<Span>,
hint: Option<String>,
source: Option<Arc<dyn std::error::Error + Send + Sync>>,
}
impl Diagnostic {
pub fn new(key: &'static str, message: String, fields: BTreeMap<String, String>) -> Self {
let message = sanitize_internal_encoding(&message);
let fields = fields
.into_iter()
.map(|(k, v)| {
(
sanitize_internal_encoding(&k),
sanitize_internal_encoding(&v),
)
})
.collect();
Self(Box::new(DiagnosticInner {
key,
message,
fields,
span: None,
hint: None,
source: None,
}))
}
pub fn make_fields(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
let mut map = BTreeMap::new();
for (k, v) in pairs {
map.insert(sanitize_internal_encoding(k), sanitize_internal_encoding(v));
}
map
}
pub fn key(&self) -> &'static str {
self.0.key
}
pub fn message(&self) -> &str {
&self.0.message
}
pub fn fields(&self) -> &BTreeMap<String, String> {
&self.0.fields
}
pub fn span(&self) -> Option<&Span> {
self.0.span.as_ref()
}
pub fn hint(&self) -> Option<&str> {
self.0.hint.as_deref()
}
pub fn with_span(mut self, span: Span) -> Self {
self.0.span = Some(span);
self
}
pub fn with_hint(mut self, hint: String) -> Self {
self.0.hint = Some(sanitize_internal_encoding(&hint));
self
}
pub fn with_source(mut self, source: Arc<dyn std::error::Error + Send + Sync>) -> Self {
self.0.source = Some(source);
self
}
}
impl PartialEq for Diagnostic {
fn eq(&self, other: &Self) -> bool {
self.0.key == other.0.key
&& self.0.message == other.0.message
&& self.0.fields == other.0.fields
&& self.0.span == other.0.span
&& self.0.hint == other.0.hint
}
}
impl Eq for Diagnostic {}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.message)?;
if let Some(hint) = &self.0.hint {
write!(f, "\nhint: {hint}")?;
}
if let Some(span) = &self.0.span
&& span.line > 0
{
match (span.col_start, span.col_end) {
(Some(s), Some(e)) => {
write!(f, "\n --> line {}, col {s}-{e}", span.line)?;
if let Some(line_text) = &span.source_line {
write!(f, "\n {} | {line_text}", span.line)?;
let width = e.saturating_sub(s).saturating_add(1).max(1);
let pad = " ".repeat(s.saturating_sub(1));
let carets = "^".repeat(width.min(80));
let gutter = " ".repeat(span.line.to_string().len());
write!(f, "\n {gutter} | {pad}{carets}")?;
}
}
_ => {
write!(f, "\n --> line {}", span.line)?;
if let Some(line_text) = &span.source_line {
write!(f, "\n {} | {line_text}", span.line)?;
}
}
}
}
Ok(())
}
}
impl std::error::Error for Diagnostic {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0
.source
.as_ref()
.map(|s| s.as_ref() as &(dyn std::error::Error + 'static))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn transport_sanitizes_sentinel_instead_of_panicking() {
let evil = format!("bad{}text", INTERNAL_SENTINEL);
let d = Diagnostic::new(
"test.sanitize",
evil.clone(),
Diagnostic::make_fields(&[("k", &evil)]),
);
assert!(!d.message().contains(INTERNAL_SENTINEL));
assert!(d.message().contains(' '));
assert_no_internal_encodings(d.message());
}
#[test]
fn source_chain_is_preserved() {
let io_err = Arc::new(io::Error::new(io::ErrorKind::NotFound, "missing file"));
let d = Diagnostic::new(
"test.io",
"failed to read script".to_string(),
Diagnostic::make_fields(&[("path", "x.ox")]),
)
.with_source(io_err);
let src = std::error::Error::source(&d).expect("source preserved");
assert!(src.to_string().contains("missing file"));
}
#[test]
fn caret_gutter_aligns_for_any_line_width() {
for line in [1usize, 9, 10, 99, 100, 1234] {
let d = Diagnostic::new(
"test.span",
"boom".to_string(),
Diagnostic::make_fields(&[]),
)
.with_span(Span::full(line, 2, 3, "hi".to_string()));
let rendered = d.to_string();
let width = line.to_string().len();
let gutter = format!(" {} |", " ".repeat(width));
assert!(rendered.contains(&gutter), "line {line}: {rendered}");
}
}
#[test]
fn diagnostic_stays_pointer_sized() {
assert_eq!(
std::mem::size_of::<Diagnostic>(),
std::mem::size_of::<usize>(),
"Diagnostic must stay one pointer wide so DiagnosticResult keeps the anyhow footprint"
);
}
#[test]
fn equality_ignores_source_chain() {
let a = Diagnostic::new("k", "m".to_string(), Diagnostic::make_fields(&[]));
let b = a.clone().with_source(Arc::new(io::Error::other("chain")));
assert_eq!(a, b);
}
#[test]
fn result_alias_carries_ok_and_err() {
let ok: DiagnosticResult<u32> = Ok(7);
let got = match ok {
Ok(got) => got,
Err(_) => panic!("expected ok"),
};
assert_eq!(got, 7);
let diag = Diagnostic::new(
"test.alias",
"boom".to_string(),
Diagnostic::make_fields(&[]),
);
let err: DiagnosticResult<u32> = Err(diag.clone());
let got = match err {
Ok(_) => panic!("expected err"),
Err(got) => got,
};
assert_eq!(got, diag);
}
#[test]
fn anyhow_blanket_from_preserves_message_and_chain() {
let io_err = Arc::new(io::Error::other("missing file"));
let diag = Diagnostic::new(
"test.interop",
"failed to read script".to_string(),
Diagnostic::make_fields(&[("path", "x.ox")]),
)
.with_source(io_err);
let anyhow_err: anyhow::Error = diag.into();
assert!(anyhow_err.to_string().contains("failed to read script"));
assert!(format!("{anyhow_err:#}").contains("missing file"));
}
#[test]
fn question_mark_converts_into_anyhow_result() {
fn fallible() -> DiagnosticResult<u32> {
Err(Diagnostic::new(
"test.qmark",
"boom".to_string(),
Diagnostic::make_fields(&[]),
))
}
fn caller() -> anyhow::Result<u32> {
Ok(fallible()? + 1)
}
let err = caller().expect_err("must propagate");
assert!(err.to_string().contains("boom"));
}
}