Skip to content

Commit

Permalink
Polished litho-diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
cutsoy committed Oct 19, 2024
1 parent d2879c5 commit 23427d6
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 2 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/cargo_publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: cargo publish

on:
workflow_dispatch:
inputs:
crate:
description: "Crate"
required: true
type: choice
options:
- litho-diagnostics

env:
CARGO_TERM_COLOR: always
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Install Rust Stable
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt
- name: Publish
run: cargo publish
working-directory: ${{ inputs.crate }}/
7 changes: 6 additions & 1 deletion litho-diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[package]
name = "litho-diagnostics"
version = "0.0.0"
version = "1.3.4"
edition = "2021"
license = "MIT"
description = "Definitions for all diagnostics that Litho (a GraphQL framework) can emit."
documentation = "https://docs.rs/litho-diagnostics/"
homepage = "https://github.com/glacyr/litho/tree/main/litho-diagnostics"
repository = "https://github.com/glacyr/litho"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
49 changes: 49 additions & 0 deletions litho-diagnostics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Litho Diagnostics

This crate implements all diagnostics that [Litho](https://litho.dev) (a GraphQL
framework) can emit.

Within Litho, these diagnostics are emitted by the lexer, parser, type checker
and compiler. Typically, each of those can emit multiple diagnostics without
bailing out after the first diagnostic, but the compiler will only advance to
the next phase if there are no errors in the previous phase.

## Usage

All diagnostics are generated by a DSL Macro (see source code), implement
`DiagnosticInfo<S>` and are generic with respect to the span that triggered
the diagnostic. Diagnostics consist of an error code, name, message and zero or
more labels. Labels are additional hints that refer to a separate span in the
source code.

### Example

Below is an example of a diagnostic that Litho generates:
`MissingArgumentsClosingParentheses`. It starts with the `code` in square
brackets, followed by a `message`. It shows a snippet of the lines that caused
the diagnostic along with the primary span that starts at `graphql:6:20`. And
finally, it also shows two more spans (`first` and `second`) with additional
hints.

```graphql
[E0006] Error: Arguments are missing closing parenthesis.

╭─[graphql:6:20]
6 │ field(arg: true
· ┬ ┬
· ╰──────────── This `(` here ...
· │
· ╰── ... should have a matching `)` here.
───╯
```

## License

Copyright 2022-2024 Glacyr B.V.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 changes: 26 additions & 1 deletion litho-diagnostics/src/dsl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/// Trait implemented by all different diagnostic types that Litho uses.
pub trait DiagnosticInfo<S> {
/// Returns the code of this diagnostic. This is usually a letter (e.g. `E`)
/// followed by 4 digits that represent the error number.
fn code(&self) -> &'static str;

/// Returns the message of this diagnostic.
fn message(&self) -> &'static str;

/// Returns the primary span that triggered this diagnostic.
fn span(&self) -> S;

/// Returns additional labels for this diagnostic: pairs of spans and
/// explanations.
fn labels(&self) -> Vec<(S, String)>;

/// Returns a boolean that indicates if this diagnostic is deprecated.
/// Deprecated diagnostics are no longer returned but still part of the docs
/// for historic purposes.
fn is_deprecated(&self) -> bool;
}

Expand All @@ -24,6 +38,7 @@ macro_rules! diagnostics {
),*
} $(@$directive:ident)?
),*) => {
/// Enum that contains all possible diagnostics that Litho can return.
#[derive(Clone, Debug)]
pub enum Diagnostic<S> where S: Copy {
$(
Expand Down Expand Up @@ -52,24 +67,30 @@ macro_rules! diagnostics {
)*
}

/// Returns the code of this diagnostic. This is usually a letter (e.g. `E`)
/// followed by 4 digits that represent the error number.
pub fn code(&self) -> &'static str {
match self {
$(Diagnostic::$name(diagnostic) => diagnostic.code(),)*
}
}

/// Returns the message of this diagnostic.
pub fn message(&self) -> &'static str {
match self {
$(Diagnostic::$name(diagnostic) => diagnostic.message(),)*
}
}

/// Returns the primary span that triggered this diagnostic.
pub fn span(&self) -> S {
match self {
$(Diagnostic::$name(diagnostic) => diagnostic.span(),)*
}
}

/// Returns additional labels for this diagnostic: pairs of spans and
/// explanations.
pub fn labels(&self) -> Vec<(S, String)> {
match self {
$(Diagnostic::$name(diagnostic) => diagnostic.labels(),)*
Expand All @@ -78,10 +99,14 @@ macro_rules! diagnostics {
}

$(
#[allow(rustdoc::bare_urls)]
#[doc = concat!("(", stringify!($code), ") ", $message)]
#[derive(Clone, Debug)]
pub struct $name<S> where S: Copy {
$($(pub $var: String,)*)?
$($(
#[doc = concat!("Value of `{", stringify!($var), "}` that is referenced in the message and/or one of the labels.")]
pub $var: String,
)*)?
$(
#[doc = $label]
pub $label_span: S,
Expand Down
40 changes: 40 additions & 0 deletions litho-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
#![deny(missing_docs)]
//! This crate implements all diagnostics that [Litho](https://litho.dev) (a
//! GraphQL framework) can emit.
//!
//! Within Litho, these diagnostics are emitted by the lexer, parser, type
//! checker and compiler. Typically, each of those can emit multiple diagnostics
//! without bailing out after the first diagnostic, but the compiler will only
//! advance to the next phase if there are no errors in the previous phase.
//!
//! All diagnostics are generated by a DSL Macro (see source code), implement
//! [`DiagnosticInfo<S>`] and are generic with respect to the span that
//! triggered the diagnostic. Diagnostics consist of an error code, name,
//! message and zero or more labels. Labels are additional hints that refer to a
//! separate span in the source code.
//!
//! ### Example
//!
//! Below is an example of a diagnostic that Litho generates:
//! [`MissingArgumentsClosingParentheses`]. It starts with the
//! [`code`](DiagnosticInfo::code) in square brackets, followed by a
//! [`message`](DiagnosticInfo::message). It shows a snippet of the lines that
//! caused the diagnostic along with the primary span that starts at
//! `graphql:6:20`. And finally, it also shows two more spans
//! ([`first`](MissingArgumentsClosingParentheses::first) and
//! [`second`](MissingArgumentsClosingParentheses::second)) with additional
//! hints.
//!
//! ```graphql
//! [E0006] Error: Arguments are missing closing parenthesis.
//!
//! ╭─[graphql:6:20]
//! │
//! 6 │ field(arg: true
//! · ┬ ┬
//! · ╰──────────── This `(` here ...
//! · │
//! · ╰── ... should have a matching `)` here.
//! ───╯
//! ```
#[macro_use]
mod dsl;

Expand Down

0 comments on commit 23427d6

Please sign in to comment.