Add Diagnostic implementation for crate error

pull/3/head
trivernis 1 year ago
parent f3bb9b58e3
commit 916dfa0f8d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -10,6 +10,7 @@ authors = ["trivernis <trivernis@proton.me>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
miette = "5.5.0"
nu-command = "0.75.0"
nu-engine = "0.75.0"
nu-parser = "0.75.0"

@ -1,3 +1,4 @@
use miette::Diagnostic;
use nu_parser::ParseError;
use nu_protocol::ShellError;
use thiserror::Error;
@ -15,3 +16,69 @@ pub enum CrateError {
#[error("Could not find the function {0}")]
FunctionNotFound(String),
}
impl Diagnostic for CrateError {
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
match self {
CrateError::NuShellError(n) => n.code(),
CrateError::NuParseError(n) => n.code(),
_ => None,
}
}
fn severity(&self) -> Option<miette::Severity> {
match self {
CrateError::NuShellError(n) => n.severity(),
CrateError::NuParseError(n) => n.severity(),
_ => None,
}
}
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
match self {
CrateError::NuShellError(n) => n.help(),
CrateError::NuParseError(n) => n.help(),
_ => None,
}
}
fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
match self {
CrateError::NuShellError(n) => n.url(),
CrateError::NuParseError(n) => n.url(),
_ => None,
}
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
match self {
CrateError::NuShellError(n) => n.source_code(),
CrateError::NuParseError(n) => n.source_code(),
_ => None,
}
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
match self {
CrateError::NuShellError(n) => n.labels(),
CrateError::NuParseError(n) => n.labels(),
_ => None,
}
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
match self {
CrateError::NuShellError(n) => n.related(),
CrateError::NuParseError(n) => n.related(),
_ => None,
}
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
match self {
CrateError::NuShellError(n) => n.diagnostic_source(),
CrateError::NuParseError(n) => n.diagnostic_source(),
_ => None,
}
}
}

Loading…
Cancel
Save