mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Implement Display
for Tracepoint
This commit is contained in:
parent
a0065eef86
commit
989d344d3d
14
src/diag.rs
14
src/diag.rs
@ -1,5 +1,7 @@
|
|||||||
//! Diagnostics.
|
//! Diagnostics.
|
||||||
|
|
||||||
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::syntax::{Span, Spanned};
|
use crate::syntax::{Span, Spanned};
|
||||||
@ -59,6 +61,18 @@ pub enum Tracepoint {
|
|||||||
Import,
|
Import,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Tracepoint {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Tracepoint::Call(Some(name)) => {
|
||||||
|
write!(f, "error occured in this call of function `{}`", name)
|
||||||
|
}
|
||||||
|
Tracepoint::Call(None) => f.pad("error occured in this function call"),
|
||||||
|
Tracepoint::Import => f.pad("error occured while importing this module"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convert a [`StrResult`] to a [`TypResult`] by adding span information.
|
/// Convert a [`StrResult`] to a [`TypResult`] by adding span information.
|
||||||
pub trait At<T> {
|
pub trait At<T> {
|
||||||
/// Add the span information.
|
/// Add the span information.
|
||||||
|
11
src/main.rs
11
src/main.rs
@ -9,7 +9,7 @@ use codespan_reporting::term::{self, termcolor, Config, Styles};
|
|||||||
use same_file::is_same_file;
|
use same_file::is_same_file;
|
||||||
use termcolor::{ColorChoice, StandardStream, WriteColor};
|
use termcolor::{ColorChoice, StandardStream, WriteColor};
|
||||||
|
|
||||||
use typst::diag::{Error, Tracepoint};
|
use typst::diag::Error;
|
||||||
use typst::source::SourceStore;
|
use typst::source::SourceStore;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -136,14 +136,7 @@ fn print_diagnostics(
|
|||||||
|
|
||||||
// Stacktrace-like helper diagnostics.
|
// Stacktrace-like helper diagnostics.
|
||||||
for point in error.trace {
|
for point in error.trace {
|
||||||
let message = match point.v {
|
let message = point.v.to_string();
|
||||||
Tracepoint::Call(Some(name)) => {
|
|
||||||
format!("error occured in this call of function `{}`", name)
|
|
||||||
}
|
|
||||||
Tracepoint::Call(None) => "error occured in this function call".into(),
|
|
||||||
Tracepoint::Import => "error occured while importing this module".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let help = Diagnostic::help().with_message(message).with_labels(vec![
|
let help = Diagnostic::help().with_message(message).with_labels(vec![
|
||||||
Label::primary(point.span.source, point.span.to_range()),
|
Label::primary(point.span.source, point.span.to_range()),
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user