#诊断 #错误报告 #编程语言 #标签 #输出 #基于文本 #终端

rspack-codespan-reporting

针对基于文本的编程语言的美丽诊断报告

1 个不稳定版本

0.11.2 2023年8月29日

#2#基于文本

Download history 13/week @ 2024-04-19 11/week @ 2024-04-26 9/week @ 2024-05-03 10/week @ 2024-05-10 13/week @ 2024-05-17 14/week @ 2024-05-24 3/week @ 2024-05-31 823/week @ 2024-06-21 2298/week @ 2024-06-28 2264/week @ 2024-07-05 2055/week @ 2024-07-12 1839/week @ 2024-07-19 1758/week @ 2024-07-26 1890/week @ 2024-08-02

8,099 每月下载量
2 个crate中使用 (通过 databend-common-ast)

Apache-2.0

105KB
1.5K SLoC

codespan-reporting

Continuous integration Crates.io Docs.rs Matrix

针对基于文本的编程语言的美丽诊断报告。

Example preview

像Rust和Elm这样的语言已经支持美丽的错误报告输出,但为新的编程语言实现这一功能可能需要大量的工作!codespan-reporting crate旨在使美丽的错误诊断对每个人来说都变得简单且相对容易!

我们仍在努力改进crate以支持更广泛的使用场景,并提高诊断渲染的质量,请继续关注更新,如果有任何反馈,请告诉我们。我们也非常欢迎贡献!

示例

use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFiles;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

// `files::SimpleFile` and `files::SimpleFiles` help you get up and running with
// `codespan-reporting` quickly! More complicated use cases can be supported
// by creating custom implementations of the `files::Files` trait.

let mut files = SimpleFiles::new();

let file_id = files.add(
    "FizzBuzz.fun",
    unindent::unindent(
        r#"
            module FizzBuzz where

            fizz₁ : Nat → String
            fizz₁ num = case (mod num 5) (mod num 3) of
                0 0 => "FizzBuzz"
                0 _ => "Fizz"
                _ 0 => "Buzz"
                _ _ => num

            fizz₂ : Nat → String
            fizz₂ num =
                case (mod num 5) (mod num 3) of
                    0 0 => "FizzBuzz"
                    0 _ => "Fizz"
                    _ 0 => "Buzz"
                    _ _ => num
        "#,
    ),
);

// We normally recommend creating a custom diagnostic data type for your
// application, and then converting that to `codespan-reporting`'s diagnostic
// type, but for the sake of this example we construct it directly.

let diagnostic = Diagnostic::error()
    .with_message("`case` clauses have incompatible types")
    .with_code("E0308")
    .with_labels(vec![
        Label::primary(file_id, 328..331).with_message("expected `String`, found `Nat`"),
        Label::secondary(file_id, 211..331).with_message("`case` clauses have incompatible types"),
        Label::secondary(file_id, 258..268).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 284..290).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 306..312).with_message("this is found to be of type `String`"),
        Label::secondary(file_id, 186..192).with_message("expected type `String` found here"),
    ])
    .with_notes(vec![unindent::unindent(
        "
            expected type `String`
                found type `Nat`
        ",
    )]);

// We now set up the writer and configuration, and then finally render the
// diagnostic to standard error.

let writer = StandardStream::stderr(ColorChoice::Always);
let config = codespan_reporting::term::Config::default();

term::emit(&mut writer.lock(), &config, &files, &diagnostic)?;

运行CLI示例

要了解彩色CLI输出的样子,克隆仓库并运行以下shell命令

cargo run --example term

示例目录中可以找到使用codespan-reporting的更多示例。

使用codespan-reporting的项目

codespan-reporting目前被以下项目使用

codespan-reporting的替代方案

包括codespan-reporting在内的替代方案有很多,例如

这些都是受rustc的优秀错误报告基础设施启发的。

贡献

有关向codespan-reporting贡献的指南可以在这里找到

行为准则

请注意,该项目遵循行为准则。参与此项目即表示您同意遵守其条款。

依赖项

~0.4-7.5MB
~44K SLoC