2023-08-18 19:37:52 +10:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use thiserror::Error;
|
2023-09-05 16:02:27 -07:00
|
|
|
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity};
|
|
|
|
|
2024-11-07 11:23:41 -05:00
|
|
|
use crate::{ast::types::ModuleId, executor::SourceRange, lsp::IntoDiagnostic};
|
2023-08-18 19:37:52 +10:00
|
|
|
|
Remove just one enum (#1096)
# Problem
This is my proposal for fixing #1107 . I've only done it for one stdlib function, `tangentialArcTo` -- if y'all like it, I'll apply this idea to the rest of the stdlib.
Previously, if users want to put a tag on the arc, the function's parameters change type.
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is object
tangentialArcTo({to: [x, y], tag: "myTag"}, %)
```
# Solution
My proposal in #1006 is that KCL should have optional values. This means we can change the stdlib `tangentialArcTo` function to use them. In this PR, the calls are now like
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is array still, but we now pass a tag at the end.
tangentialArcTo([x, y], %, "myTag")
```
This adds an "option" type to KCL typesystem, but it's not really revealed to users (no KCL types are revealed to users right now, they write untyped code and only interact with types when they get type errors upon executing programs). Also adds a None type, which is the default case of the Optional enum.
2023-12-18 23:49:32 -06:00
|
|
|
#[derive(Error, Debug, Serialize, Deserialize, ts_rs::TS, Clone, PartialEq, Eq)]
|
2023-08-19 23:18:54 -07:00
|
|
|
#[ts(export)]
|
2023-08-18 19:37:52 +10:00
|
|
|
#[serde(tag = "kind", rename_all = "snake_case")]
|
|
|
|
pub enum KclError {
|
2023-11-01 17:20:49 -05:00
|
|
|
#[error("lexical: {0:?}")]
|
|
|
|
Lexical(KclErrorDetails),
|
2023-08-18 19:37:52 +10:00
|
|
|
#[error("syntax: {0:?}")]
|
|
|
|
Syntax(KclErrorDetails),
|
|
|
|
#[error("semantic: {0:?}")]
|
|
|
|
Semantic(KclErrorDetails),
|
2024-10-17 00:48:33 -04:00
|
|
|
#[error("import cycle: {0:?}")]
|
|
|
|
ImportCycle(KclErrorDetails),
|
2023-08-18 19:37:52 +10:00
|
|
|
#[error("type: {0:?}")]
|
|
|
|
Type(KclErrorDetails),
|
|
|
|
#[error("unimplemented: {0:?}")]
|
|
|
|
Unimplemented(KclErrorDetails),
|
2023-08-22 13:28:02 +10:00
|
|
|
#[error("unexpected: {0:?}")]
|
|
|
|
Unexpected(KclErrorDetails),
|
2023-08-18 19:37:52 +10:00
|
|
|
#[error("value already defined: {0:?}")]
|
|
|
|
ValueAlreadyDefined(KclErrorDetails),
|
|
|
|
#[error("undefined value: {0:?}")]
|
|
|
|
UndefinedValue(KclErrorDetails),
|
|
|
|
#[error("invalid expression: {0:?}")]
|
2023-08-29 14:12:48 -07:00
|
|
|
InvalidExpression(KclErrorDetails),
|
2023-08-24 15:34:51 -07:00
|
|
|
#[error("engine: {0:?}")]
|
|
|
|
Engine(KclErrorDetails),
|
2023-11-03 13:30:19 -05:00
|
|
|
#[error("internal error, please report to KittyCAD team: {0:?}")]
|
|
|
|
Internal(KclErrorDetails),
|
2023-08-18 19:37:52 +10:00
|
|
|
}
|
|
|
|
|
Remove just one enum (#1096)
# Problem
This is my proposal for fixing #1107 . I've only done it for one stdlib function, `tangentialArcTo` -- if y'all like it, I'll apply this idea to the rest of the stdlib.
Previously, if users want to put a tag on the arc, the function's parameters change type.
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is object
tangentialArcTo({to: [x, y], tag: "myTag"}, %)
```
# Solution
My proposal in #1006 is that KCL should have optional values. This means we can change the stdlib `tangentialArcTo` function to use them. In this PR, the calls are now like
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is array still, but we now pass a tag at the end.
tangentialArcTo([x, y], %, "myTag")
```
This adds an "option" type to KCL typesystem, but it's not really revealed to users (no KCL types are revealed to users right now, they write untyped code and only interact with types when they get type errors upon executing programs). Also adds a None type, which is the default case of the Optional enum.
2023-12-18 23:49:32 -06:00
|
|
|
#[derive(Debug, Serialize, Deserialize, ts_rs::TS, Clone, PartialEq, Eq)]
|
2023-08-19 23:18:54 -07:00
|
|
|
#[ts(export)]
|
2023-08-18 19:37:52 +10:00
|
|
|
pub struct KclErrorDetails {
|
|
|
|
#[serde(rename = "sourceRanges")]
|
2023-09-05 16:02:27 -07:00
|
|
|
pub source_ranges: Vec<SourceRange>,
|
2023-08-18 19:37:52 +10:00
|
|
|
#[serde(rename = "msg")]
|
|
|
|
pub message: String,
|
|
|
|
}
|
2023-08-19 14:22:11 -07:00
|
|
|
|
2023-08-29 14:12:48 -07:00
|
|
|
impl KclError {
|
2024-11-22 13:25:14 +13:00
|
|
|
pub fn internal(message: String) -> KclError {
|
|
|
|
KclError::Internal(KclErrorDetails {
|
|
|
|
source_ranges: Default::default(),
|
|
|
|
message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-11 19:23:35 -04:00
|
|
|
/// Get the error message.
|
|
|
|
pub fn get_message(&self) -> String {
|
|
|
|
format!("{}: {}", self.error_type(), self.message())
|
2023-11-03 13:30:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error_type(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
KclError::Lexical(_) => "lexical",
|
|
|
|
KclError::Syntax(_) => "syntax",
|
|
|
|
KclError::Semantic(_) => "semantic",
|
2024-10-17 00:48:33 -04:00
|
|
|
KclError::ImportCycle(_) => "import cycle",
|
2023-11-03 13:30:19 -05:00
|
|
|
KclError::Type(_) => "type",
|
|
|
|
KclError::Unimplemented(_) => "unimplemented",
|
|
|
|
KclError::Unexpected(_) => "unexpected",
|
|
|
|
KclError::ValueAlreadyDefined(_) => "value already defined",
|
|
|
|
KclError::UndefinedValue(_) => "undefined value",
|
|
|
|
KclError::InvalidExpression(_) => "invalid expression",
|
|
|
|
KclError::Engine(_) => "engine",
|
|
|
|
KclError::Internal(_) => "internal",
|
|
|
|
}
|
2023-08-29 14:12:48 -07:00
|
|
|
}
|
2023-09-05 16:02:27 -07:00
|
|
|
|
|
|
|
pub fn source_ranges(&self) -> Vec<SourceRange> {
|
|
|
|
match &self {
|
2023-11-01 17:20:49 -05:00
|
|
|
KclError::Lexical(e) => e.source_ranges.clone(),
|
2023-09-05 16:02:27 -07:00
|
|
|
KclError::Syntax(e) => e.source_ranges.clone(),
|
|
|
|
KclError::Semantic(e) => e.source_ranges.clone(),
|
2024-10-17 00:48:33 -04:00
|
|
|
KclError::ImportCycle(e) => e.source_ranges.clone(),
|
2023-09-05 16:02:27 -07:00
|
|
|
KclError::Type(e) => e.source_ranges.clone(),
|
|
|
|
KclError::Unimplemented(e) => e.source_ranges.clone(),
|
|
|
|
KclError::Unexpected(e) => e.source_ranges.clone(),
|
|
|
|
KclError::ValueAlreadyDefined(e) => e.source_ranges.clone(),
|
|
|
|
KclError::UndefinedValue(e) => e.source_ranges.clone(),
|
|
|
|
KclError::InvalidExpression(e) => e.source_ranges.clone(),
|
|
|
|
KclError::Engine(e) => e.source_ranges.clone(),
|
2023-11-03 13:30:19 -05:00
|
|
|
KclError::Internal(e) => e.source_ranges.clone(),
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
}
|
New parser built in Winnow (#731)
* New parser built with Winnow
This new parser uses [winnow](docs.rs/winnow) to replace the handwritten recursive parser.
## Differences
I think the Winnow parser is more readable than handwritten one, due to reusing standard combinators. If you have a parsre like `p` or `q` you can combine them with standard functions like `repeat(0..4, p)`, `opt(p)`, `alt((p, q))` and `separated1(p, ", ")`. This IMO makes it more readable once you know what those standard functions do.
It's also more accurate now -- e.g. the parser no longer swallows whitespace between comments, or inserts it where there was none before. It no longer changes // comments to /* comments depending on the surrounding whitespace.
Primary form of testing was running the same KCL program through both the old and new parsers and asserting that both parsers produce the same AST. See the test `parser::parser_impl::tests::check_parsers_work_the_same`. But occasionally the new and old parsers disagree. This is either:
- Innocuous (e.g. disagreeing on whether a comment starts at the preceding whitespace or at the //)
- Helpful (e.g. new parser recognizes comments more accurately, preserving the difference between // and /* comments)
- Acceptably bad (e.g. new parser sometimes outputs worse error messages, TODO in #784)
so those KCL programs have their own unit tests in `parser_impl.rs` demonstrating the behaviour.
If you'd like to review this PR, it's arguably more important to review changes to the existing unit tests rather than the new parser itself. Because changes to the unit tests show where my parser changes behaviour -- usually for the better, occasionally for the worse (e.g. a worse error message than before). I think overall the improvements are worth it that I'd like to merge it without spending another week fixing it up -- we can fix the error messages in a follow-up PR.
## Performance
| Benchmark | Old parser (this branch) | New parser (this branch) | Speedup |
| ------------- | ------------- | ------------- | ------------- |
| Pipes on pipes | 922 ms | 42 ms | 21x |
| Kitt SVG | 148 ms | 7 ms | 21x |
There's definitely still room to improve performance:
- https://github.com/KittyCAD/modeling-app/issues/839
- https://github.com/KittyCAD/modeling-app/issues/840
## Winnow
Y'all know I love [Nom](docs.rs/nom) and I've blogged about it a lot. But I'm very happy using Winnow, a fork. It's got some really nice usability improvements. While writing this PR I found some bugs or unclear docs in Winnow:
- https://github.com/winnow-rs/winnow/issues/339
- https://github.com/winnow-rs/winnow/issues/341
- https://github.com/winnow-rs/winnow/issues/342
- https://github.com/winnow-rs/winnow/issues/344
The maintainer was quick to close them and release new versions within a few hours, so I feel very confident building the parser on this library. It's a clear improvement over Nom and it's used in toml-edit (and therefore within Cargo) and Gitoxide, so it's becoming a staple of the Rust ecosystem, which adds confidence.
Closes #716
Closes #815
Closes #599
2023-10-12 09:42:37 -05:00
|
|
|
|
|
|
|
/// Get the inner error message.
|
|
|
|
pub fn message(&self) -> &str {
|
|
|
|
match &self {
|
2023-11-01 17:20:49 -05:00
|
|
|
KclError::Lexical(e) => &e.message,
|
New parser built in Winnow (#731)
* New parser built with Winnow
This new parser uses [winnow](docs.rs/winnow) to replace the handwritten recursive parser.
## Differences
I think the Winnow parser is more readable than handwritten one, due to reusing standard combinators. If you have a parsre like `p` or `q` you can combine them with standard functions like `repeat(0..4, p)`, `opt(p)`, `alt((p, q))` and `separated1(p, ", ")`. This IMO makes it more readable once you know what those standard functions do.
It's also more accurate now -- e.g. the parser no longer swallows whitespace between comments, or inserts it where there was none before. It no longer changes // comments to /* comments depending on the surrounding whitespace.
Primary form of testing was running the same KCL program through both the old and new parsers and asserting that both parsers produce the same AST. See the test `parser::parser_impl::tests::check_parsers_work_the_same`. But occasionally the new and old parsers disagree. This is either:
- Innocuous (e.g. disagreeing on whether a comment starts at the preceding whitespace or at the //)
- Helpful (e.g. new parser recognizes comments more accurately, preserving the difference between // and /* comments)
- Acceptably bad (e.g. new parser sometimes outputs worse error messages, TODO in #784)
so those KCL programs have their own unit tests in `parser_impl.rs` demonstrating the behaviour.
If you'd like to review this PR, it's arguably more important to review changes to the existing unit tests rather than the new parser itself. Because changes to the unit tests show where my parser changes behaviour -- usually for the better, occasionally for the worse (e.g. a worse error message than before). I think overall the improvements are worth it that I'd like to merge it without spending another week fixing it up -- we can fix the error messages in a follow-up PR.
## Performance
| Benchmark | Old parser (this branch) | New parser (this branch) | Speedup |
| ------------- | ------------- | ------------- | ------------- |
| Pipes on pipes | 922 ms | 42 ms | 21x |
| Kitt SVG | 148 ms | 7 ms | 21x |
There's definitely still room to improve performance:
- https://github.com/KittyCAD/modeling-app/issues/839
- https://github.com/KittyCAD/modeling-app/issues/840
## Winnow
Y'all know I love [Nom](docs.rs/nom) and I've blogged about it a lot. But I'm very happy using Winnow, a fork. It's got some really nice usability improvements. While writing this PR I found some bugs or unclear docs in Winnow:
- https://github.com/winnow-rs/winnow/issues/339
- https://github.com/winnow-rs/winnow/issues/341
- https://github.com/winnow-rs/winnow/issues/342
- https://github.com/winnow-rs/winnow/issues/344
The maintainer was quick to close them and release new versions within a few hours, so I feel very confident building the parser on this library. It's a clear improvement over Nom and it's used in toml-edit (and therefore within Cargo) and Gitoxide, so it's becoming a staple of the Rust ecosystem, which adds confidence.
Closes #716
Closes #815
Closes #599
2023-10-12 09:42:37 -05:00
|
|
|
KclError::Syntax(e) => &e.message,
|
|
|
|
KclError::Semantic(e) => &e.message,
|
2024-10-17 00:48:33 -04:00
|
|
|
KclError::ImportCycle(e) => &e.message,
|
New parser built in Winnow (#731)
* New parser built with Winnow
This new parser uses [winnow](docs.rs/winnow) to replace the handwritten recursive parser.
## Differences
I think the Winnow parser is more readable than handwritten one, due to reusing standard combinators. If you have a parsre like `p` or `q` you can combine them with standard functions like `repeat(0..4, p)`, `opt(p)`, `alt((p, q))` and `separated1(p, ", ")`. This IMO makes it more readable once you know what those standard functions do.
It's also more accurate now -- e.g. the parser no longer swallows whitespace between comments, or inserts it where there was none before. It no longer changes // comments to /* comments depending on the surrounding whitespace.
Primary form of testing was running the same KCL program through both the old and new parsers and asserting that both parsers produce the same AST. See the test `parser::parser_impl::tests::check_parsers_work_the_same`. But occasionally the new and old parsers disagree. This is either:
- Innocuous (e.g. disagreeing on whether a comment starts at the preceding whitespace or at the //)
- Helpful (e.g. new parser recognizes comments more accurately, preserving the difference between // and /* comments)
- Acceptably bad (e.g. new parser sometimes outputs worse error messages, TODO in #784)
so those KCL programs have their own unit tests in `parser_impl.rs` demonstrating the behaviour.
If you'd like to review this PR, it's arguably more important to review changes to the existing unit tests rather than the new parser itself. Because changes to the unit tests show where my parser changes behaviour -- usually for the better, occasionally for the worse (e.g. a worse error message than before). I think overall the improvements are worth it that I'd like to merge it without spending another week fixing it up -- we can fix the error messages in a follow-up PR.
## Performance
| Benchmark | Old parser (this branch) | New parser (this branch) | Speedup |
| ------------- | ------------- | ------------- | ------------- |
| Pipes on pipes | 922 ms | 42 ms | 21x |
| Kitt SVG | 148 ms | 7 ms | 21x |
There's definitely still room to improve performance:
- https://github.com/KittyCAD/modeling-app/issues/839
- https://github.com/KittyCAD/modeling-app/issues/840
## Winnow
Y'all know I love [Nom](docs.rs/nom) and I've blogged about it a lot. But I'm very happy using Winnow, a fork. It's got some really nice usability improvements. While writing this PR I found some bugs or unclear docs in Winnow:
- https://github.com/winnow-rs/winnow/issues/339
- https://github.com/winnow-rs/winnow/issues/341
- https://github.com/winnow-rs/winnow/issues/342
- https://github.com/winnow-rs/winnow/issues/344
The maintainer was quick to close them and release new versions within a few hours, so I feel very confident building the parser on this library. It's a clear improvement over Nom and it's used in toml-edit (and therefore within Cargo) and Gitoxide, so it's becoming a staple of the Rust ecosystem, which adds confidence.
Closes #716
Closes #815
Closes #599
2023-10-12 09:42:37 -05:00
|
|
|
KclError::Type(e) => &e.message,
|
|
|
|
KclError::Unimplemented(e) => &e.message,
|
|
|
|
KclError::Unexpected(e) => &e.message,
|
|
|
|
KclError::ValueAlreadyDefined(e) => &e.message,
|
|
|
|
KclError::UndefinedValue(e) => &e.message,
|
|
|
|
KclError::InvalidExpression(e) => &e.message,
|
|
|
|
KclError::Engine(e) => &e.message,
|
2023-11-03 13:30:19 -05:00
|
|
|
KclError::Internal(e) => &e.message,
|
New parser built in Winnow (#731)
* New parser built with Winnow
This new parser uses [winnow](docs.rs/winnow) to replace the handwritten recursive parser.
## Differences
I think the Winnow parser is more readable than handwritten one, due to reusing standard combinators. If you have a parsre like `p` or `q` you can combine them with standard functions like `repeat(0..4, p)`, `opt(p)`, `alt((p, q))` and `separated1(p, ", ")`. This IMO makes it more readable once you know what those standard functions do.
It's also more accurate now -- e.g. the parser no longer swallows whitespace between comments, or inserts it where there was none before. It no longer changes // comments to /* comments depending on the surrounding whitespace.
Primary form of testing was running the same KCL program through both the old and new parsers and asserting that both parsers produce the same AST. See the test `parser::parser_impl::tests::check_parsers_work_the_same`. But occasionally the new and old parsers disagree. This is either:
- Innocuous (e.g. disagreeing on whether a comment starts at the preceding whitespace or at the //)
- Helpful (e.g. new parser recognizes comments more accurately, preserving the difference between // and /* comments)
- Acceptably bad (e.g. new parser sometimes outputs worse error messages, TODO in #784)
so those KCL programs have their own unit tests in `parser_impl.rs` demonstrating the behaviour.
If you'd like to review this PR, it's arguably more important to review changes to the existing unit tests rather than the new parser itself. Because changes to the unit tests show where my parser changes behaviour -- usually for the better, occasionally for the worse (e.g. a worse error message than before). I think overall the improvements are worth it that I'd like to merge it without spending another week fixing it up -- we can fix the error messages in a follow-up PR.
## Performance
| Benchmark | Old parser (this branch) | New parser (this branch) | Speedup |
| ------------- | ------------- | ------------- | ------------- |
| Pipes on pipes | 922 ms | 42 ms | 21x |
| Kitt SVG | 148 ms | 7 ms | 21x |
There's definitely still room to improve performance:
- https://github.com/KittyCAD/modeling-app/issues/839
- https://github.com/KittyCAD/modeling-app/issues/840
## Winnow
Y'all know I love [Nom](docs.rs/nom) and I've blogged about it a lot. But I'm very happy using Winnow, a fork. It's got some really nice usability improvements. While writing this PR I found some bugs or unclear docs in Winnow:
- https://github.com/winnow-rs/winnow/issues/339
- https://github.com/winnow-rs/winnow/issues/341
- https://github.com/winnow-rs/winnow/issues/342
- https://github.com/winnow-rs/winnow/issues/344
The maintainer was quick to close them and release new versions within a few hours, so I feel very confident building the parser on this library. It's a clear improvement over Nom and it's used in toml-edit (and therefore within Cargo) and Gitoxide, so it's becoming a staple of the Rust ecosystem, which adds confidence.
Closes #716
Closes #815
Closes #599
2023-10-12 09:42:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 13:37:22 -07:00
|
|
|
pub fn override_source_ranges(&self, source_ranges: Vec<SourceRange>) -> Self {
|
|
|
|
let mut new = self.clone();
|
|
|
|
match &mut new {
|
|
|
|
KclError::Lexical(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Syntax(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Semantic(e) => e.source_ranges = source_ranges,
|
2024-10-17 00:48:33 -04:00
|
|
|
KclError::ImportCycle(e) => e.source_ranges = source_ranges,
|
2024-03-11 13:37:22 -07:00
|
|
|
KclError::Type(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Unimplemented(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Unexpected(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::ValueAlreadyDefined(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::UndefinedValue(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::InvalidExpression(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Engine(e) => e.source_ranges = source_ranges,
|
|
|
|
KclError::Internal(e) => e.source_ranges = source_ranges,
|
|
|
|
}
|
|
|
|
|
|
|
|
new
|
|
|
|
}
|
2024-05-21 00:49:57 -07:00
|
|
|
|
|
|
|
pub fn add_source_ranges(&self, source_ranges: Vec<SourceRange>) -> Self {
|
|
|
|
let mut new = self.clone();
|
|
|
|
match &mut new {
|
|
|
|
KclError::Lexical(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Syntax(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Semantic(e) => e.source_ranges.extend(source_ranges),
|
2024-10-17 00:48:33 -04:00
|
|
|
KclError::ImportCycle(e) => e.source_ranges.extend(source_ranges),
|
2024-05-21 00:49:57 -07:00
|
|
|
KclError::Type(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Unimplemented(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Unexpected(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::ValueAlreadyDefined(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::UndefinedValue(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::InvalidExpression(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Engine(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
KclError::Internal(e) => e.source_ranges.extend(source_ranges),
|
|
|
|
}
|
|
|
|
|
|
|
|
new
|
|
|
|
}
|
2023-08-29 14:12:48 -07:00
|
|
|
}
|
|
|
|
|
2024-06-11 19:23:35 -04:00
|
|
|
impl IntoDiagnostic for KclError {
|
|
|
|
fn to_lsp_diagnostic(&self, code: &str) -> Diagnostic {
|
|
|
|
let message = self.get_message();
|
|
|
|
let source_ranges = self.source_ranges();
|
|
|
|
|
2024-11-07 11:23:41 -05:00
|
|
|
// Limit to only errors in the top-level file.
|
|
|
|
let module_id = ModuleId::default();
|
|
|
|
let source_ranges = source_ranges
|
|
|
|
.iter()
|
|
|
|
.filter(|r| r.module_id() == module_id)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2024-06-11 19:23:35 -04:00
|
|
|
Diagnostic {
|
|
|
|
range: source_ranges.first().map(|r| r.to_lsp_range(code)).unwrap_or_default(),
|
2024-06-27 15:43:49 -07:00
|
|
|
severity: Some(self.severity()),
|
2024-06-11 19:23:35 -04:00
|
|
|
code: None,
|
|
|
|
// TODO: this is neat we can pass a URL to a help page here for this specific error.
|
|
|
|
code_description: None,
|
|
|
|
source: Some("kcl".to_string()),
|
|
|
|
message,
|
|
|
|
related_information: None,
|
|
|
|
tags: None,
|
|
|
|
data: None,
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 15:43:49 -07:00
|
|
|
|
|
|
|
fn severity(&self) -> DiagnosticSeverity {
|
|
|
|
DiagnosticSeverity::ERROR
|
|
|
|
}
|
2024-06-11 19:23:35 -04:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:22:11 -07:00
|
|
|
/// This is different than to_string() in that it will serialize the Error
|
|
|
|
/// the struct as JSON so we can deserialize it on the js side.
|
|
|
|
impl From<KclError> for String {
|
|
|
|
fn from(error: KclError) -> Self {
|
|
|
|
serde_json::to_string(&error).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
|
|
|
|
impl From<String> for KclError {
|
|
|
|
fn from(error: String) -> Self {
|
|
|
|
serde_json::from_str(&error).unwrap()
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 17:32:08 -07:00
|
|
|
|
|
|
|
#[cfg(feature = "pyo3")]
|
|
|
|
impl From<pyo3::PyErr> for KclError {
|
|
|
|
fn from(error: pyo3::PyErr) -> Self {
|
|
|
|
KclError::Internal(KclErrorDetails {
|
|
|
|
source_ranges: vec![],
|
|
|
|
message: error.to_string(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "pyo3")]
|
|
|
|
impl From<KclError> for pyo3::PyErr {
|
|
|
|
fn from(error: KclError) -> Self {
|
|
|
|
pyo3::exceptions::PyException::new_err(error.to_string())
|
|
|
|
}
|
|
|
|
}
|