Compare commits

...

2 Commits

Author SHA1 Message Date
d4b168a78f SourceRange can be optional 2024-06-15 22:40:33 -05:00
ebd1d1a9a5 Support miette for beautiful KCL errors in the CLI 2024-06-15 22:29:28 -05:00
4 changed files with 144 additions and 0 deletions

107
src/wasm-lib/Cargo.lock generated
View File

@ -221,6 +221,15 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "backtrace-ext"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50"
dependencies = [
"backtrace",
]
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.13.1" version = "0.13.1"
@ -1325,6 +1334,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "is_ci"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"
@ -1396,6 +1411,7 @@ dependencies = [
"js-sys", "js-sys",
"kittycad", "kittycad",
"lazy_static", "lazy_static",
"miette",
"mime_guess", "mime_guess",
"parse-display", "parse-display",
"pretty_assertions", "pretty_assertions",
@ -1556,6 +1572,37 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "miette"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4edc8853320c2a0dab800fbda86253c8938f6ea88510dc92c5f1ed20e794afc1"
dependencies = [
"backtrace",
"backtrace-ext",
"cfg-if",
"miette-derive",
"owo-colors",
"supports-color",
"supports-hyperlinks",
"supports-unicode",
"terminal_size",
"textwrap",
"thiserror",
"unicode-width",
]
[[package]]
name = "miette-derive"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf09caffaac8068c346b6df2a7fc27a177fd20b39421a39ce0a211bde679a6c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.66",
]
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.17" version = "0.3.17"
@ -1715,6 +1762,12 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "owo-colors"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.11.2" version = "0.11.2"
@ -2625,6 +2678,12 @@ version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smawk"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.6" version = "0.5.6"
@ -2710,6 +2769,27 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "supports-color"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9829b314621dfc575df4e409e79f9d6a66a3bd707ab73f23cb4aa3a854ac854f"
dependencies = [
"is_ci",
]
[[package]]
name = "supports-hyperlinks"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c0a1e5168041f5f3ff68ff7d95dcb9c8749df29f6e7e89ada40dd4c9de404ee"
[[package]]
name = "supports-unicode"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -2806,6 +2886,27 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "terminal_size"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7"
dependencies = [
"rustix",
"windows-sys 0.48.0",
]
[[package]]
name = "textwrap"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.61" version = "1.0.61"
@ -3237,6 +3338,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-linebreak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
[[package]] [[package]]
name = "unicode-normalization" name = "unicode-normalization"
version = "0.1.23" version = "0.1.23"

View File

@ -26,6 +26,8 @@ git_rev = "0.1.0"
gltf-json = "1.4.1" gltf-json = "1.4.1"
kittycad = { workspace = true, features = ["clap"] } kittycad = { workspace = true, features = ["clap"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
# TODO: Make this optional
miette = { version = "7.2.0", optional = false, features = ["fancy"] }
mime_guess = "2.0.4" mime_guess = "2.0.4"
parse-display = "0.9.1" parse-display = "0.9.1"
reqwest = { version = "0.11.26", default-features = false, features = ["stream", "rustls-tls"] } reqwest = { version = "0.11.26", default-features = false, features = ["stream", "rustls-tls"] }
@ -62,6 +64,7 @@ tower-lsp = { version = "0.20.0", features = ["proposed"] }
[features] [features]
default = ["cli", "engine"] default = ["cli", "engine"]
cli = ["dep:clap"] cli = ["dep:clap"]
# miette = ["dep:miette"]
engine = [] engine = []
[profile.release] [profile.release]

View File

@ -32,6 +32,32 @@ pub enum KclError {
Internal(KclErrorDetails), Internal(KclErrorDetails),
} }
/// A KCL error that can be displayed fancy.
#[derive(Error, Debug, miette::Diagnostic)]
#[error("KCL error: {error}")]
pub struct KclErrorReport {
#[source]
error: KclError,
// The Source that we're gonna be printing snippets out of.
// This can be a String if you don't have or care about file names.
#[source_code]
src: miette::NamedSource<String>,
// Snippets and highlights can be included in the diagnostic!
#[label("Error here")]
source_range: Option<SourceRange>,
}
impl KclErrorReport {
/// Create a new error report.
pub fn new(error: KclError, kcl_filename: String, kcl_code: String, source_range: Option<SourceRange>) -> Self {
Self {
error,
src: miette::NamedSource::new(kcl_filename, kcl_code),
source_range,
}
}
}
#[derive(Debug, Serialize, Deserialize, ts_rs::TS, Clone, PartialEq, Eq)] #[derive(Debug, Serialize, Deserialize, ts_rs::TS, Clone, PartialEq, Eq)]
#[ts(export)] #[ts(export)]
pub struct KclErrorDetails { pub struct KclErrorDetails {

View File

@ -619,6 +619,14 @@ pub struct Rotation(#[ts(type = "[number, number, number, number]")] pub [f64; 4
#[ts(export)] #[ts(export)]
pub struct SourceRange(#[ts(type = "[number, number]")] pub [usize; 2]); pub struct SourceRange(#[ts(type = "[number, number]")] pub [usize; 2]);
impl From<SourceRange> for miette::SourceSpan {
fn from(value: SourceRange) -> Self {
let [start, end] = value.0;
let length = end - start;
miette::SourceSpan::new(start.into(), length)
}
}
impl SourceRange { impl SourceRange {
/// Create a new source range. /// Create a new source range.
pub fn new(start: usize, end: usize) -> Self { pub fn new(start: usize, end: usize) -> Self {