diff --git a/codegen/src/error.rs b/codegen/src/error.rs index 8c3ea3f..a4ac60b 100644 --- a/codegen/src/error.rs +++ b/codegen/src/error.rs @@ -1,12 +1,6 @@ -use rustpython_compiler_core::Location; use std::{error::Error, fmt}; -#[derive(Debug)] -pub struct CodegenError { - pub error: CodegenErrorType, - pub location: Location, - pub source_path: String, -} +pub type CodegenError = rustpython_compiler_core::Error; #[derive(Debug)] #[non_exhaustive] @@ -87,15 +81,3 @@ impl fmt::Display for CodegenErrorType { } impl Error for CodegenErrorType {} - -impl fmt::Display for CodegenError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.location.fmt_with(f, &self.error) - } -} - -impl Error for CodegenError { - fn source(&self) -> Option<&(dyn Error + 'static)> { - None - } -} diff --git a/core/src/error.rs b/core/src/error.rs index 4553b60..d249755 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use crate::Location; #[derive(Debug, PartialEq, Eq)] @@ -15,6 +17,24 @@ impl std::ops::Deref for Error { } } +impl std::error::Error for Error +where + T: std::fmt::Display + std::fmt::Debug, +{ + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + None + } +} + +impl Display for Error +where + T: std::fmt::Display, +{ + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + self.location.fmt_with(f, &self.error) + } +} + impl Error { pub fn error(self) -> T { self.error diff --git a/parser/src/error.rs b/parser/src/error.rs index b64abcb..257d4c6 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -193,7 +193,7 @@ impl ParseError { impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.location.fmt_with(f, &self.error) + self.0.fmt(f) } } diff --git a/src/lib.rs b/src/lib.rs index 4e057a6..933f0d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub use rustpython_codegen::compile::CompileOpts; pub use rustpython_compiler_core::Mode; #[derive(Debug, thiserror::Error)] -pub enum CodegenErrorType { +pub enum CompileErrorType { #[error(transparent)] Compile(#[from] rustpython_codegen::error::CodegenErrorType), #[error(transparent)] @@ -20,7 +20,7 @@ pub enum CodegenErrorType { #[derive(Debug, thiserror::Error)] pub struct CompileError { - pub error: CodegenErrorType, + pub error: CompileErrorType, pub source_path: String, pub location: Location, pub statement: Option,