diff --git a/codegen/src/error.rs b/codegen/src/error.rs index a4ac60b..cfc4741 100644 --- a/codegen/src/error.rs +++ b/codegen/src/error.rs @@ -1,6 +1,6 @@ use std::{error::Error, fmt}; -pub type CodegenError = rustpython_compiler_core::Error; +pub type CodegenError = rustpython_compiler_core::BaseError; #[derive(Debug)] #[non_exhaustive] diff --git a/core/src/error.rs b/core/src/error.rs index d249755..40d9734 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -3,13 +3,13 @@ use std::fmt::Display; use crate::Location; #[derive(Debug, PartialEq, Eq)] -pub struct Error { +pub struct BaseError { pub error: T, pub location: Location, pub source_path: String, } -impl std::ops::Deref for Error { +impl std::ops::Deref for BaseError { type Target = T; fn deref(&self) -> &Self::Target { @@ -17,7 +17,7 @@ impl std::ops::Deref for Error { } } -impl std::error::Error for Error +impl std::error::Error for BaseError where T: std::fmt::Display + std::fmt::Debug, { @@ -26,7 +26,7 @@ where } } -impl Display for Error +impl Display for BaseError where T: std::fmt::Display, { @@ -35,8 +35,26 @@ where } } -impl Error { +impl BaseError { pub fn error(self) -> T { self.error } + + pub fn from(obj: BaseError) -> Self + where + U: Into, + { + Self { + error: obj.error.into(), + location: obj.location, + source_path: obj.source_path, + } + } + + pub fn into(self) -> BaseError + where + T: Into, + { + BaseError::from(self) + } } diff --git a/core/src/lib.rs b/core/src/lib.rs index f38c6d0..7360cf7 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -7,6 +7,6 @@ mod location; mod mode; pub use bytecode::*; -pub use error::Error; +pub use error::BaseError; pub use location::Location; pub use mode::Mode; diff --git a/parser/src/error.rs b/parser/src/error.rs index 257d4c6..77f2f8f 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -118,7 +118,7 @@ impl From for LalrpopError { /// Represents an error during parsing #[derive(Debug, PartialEq)] -pub struct ParseError(rustpython_compiler_core::Error); +pub struct ParseError(rustpython_compiler_core::BaseError); #[derive(Debug, PartialEq)] pub enum ParseErrorType { @@ -134,7 +134,7 @@ pub enum ParseErrorType { Lexical(LexicalErrorType), } -impl From for rustpython_compiler_core::Error { +impl From for rustpython_compiler_core::BaseError { fn from(err: ParseError) -> Self { err.0 } @@ -149,7 +149,7 @@ impl From for ParseErrorType { /// Convert `lalrpop_util::ParseError` to our internal type impl ParseError { fn new(error: ParseErrorType, location: Location, source_path: String) -> Self { - Self(rustpython_compiler_core::Error { + Self(rustpython_compiler_core::BaseError { error, location, source_path, @@ -239,7 +239,7 @@ impl ParseErrorType { } impl std::ops::Deref for ParseError { - type Target = rustpython_compiler_core::Error; + type Target = rustpython_compiler_core::BaseError; fn deref(&self) -> &Self::Target { &self.0 } diff --git a/src/lib.rs b/src/lib.rs index 933f0d0..afcb1ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ use rustpython_codegen::{compile, symboltable}; -use rustpython_compiler_core::CodeObject; +use rustpython_compiler_core::{BaseError, CodeObject}; use rustpython_parser::{ ast::{fold::Fold, ConstantOptimizer, Location}, error::ParseErrorType, @@ -18,14 +18,21 @@ pub enum CompileErrorType { Parse(#[from] rustpython_parser::error::ParseErrorType), } +pub type CompileErrorBody = BaseError; + #[derive(Debug, thiserror::Error)] pub struct CompileError { - pub error: CompileErrorType, - pub source_path: String, - pub location: Location, + pub body: CompileErrorBody, pub statement: Option, } +impl std::ops::Deref for CompileError { + type Target = CompileErrorBody; + fn deref(&self) -> &Self::Target { + &self.body + } +} + impl fmt::Display for CompileError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let loc = self.location; @@ -41,20 +48,18 @@ impl fmt::Display for CompileError { impl CompileError { fn from_codegen(error: rustpython_codegen::error::CodegenError, source: &str) -> Self { + let statement = get_statement(source, error.location); Self { - error: error.error.into(), - location: error.location, - source_path: error.source_path, - statement: get_statement(source, error.location), + body: error.into(), + statement, } } fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self { - let error: rustpython_compiler_core::Error = error.into(); + let error: rustpython_compiler_core::BaseError = error.into(); + let statement = get_statement(source, error.location); Self { - error: error.error.into(), - location: error.location, - source_path: error.source_path, - statement: get_statement(source, error.location), + body: error.into(), + statement, } } fn from_symtable(