Integrate CompileError to compiler-core::BaseError

This commit is contained in:
Jeong YunWon 2022-08-22 20:21:40 +09:00
parent 7fcc18daea
commit e8230efe1a
3 changed files with 28 additions and 10 deletions

View file

@ -3,13 +3,13 @@ use std::fmt::Display;
use crate::Location; use crate::Location;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct Error<T> { pub struct BaseError<T> {
pub error: T, pub error: T,
pub location: Location, pub location: Location,
pub source_path: String, pub source_path: String,
} }
impl<T> std::ops::Deref for Error<T> { impl<T> std::ops::Deref for BaseError<T> {
type Target = T; type Target = T;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
@ -17,7 +17,7 @@ impl<T> std::ops::Deref for Error<T> {
} }
} }
impl<T> std::error::Error for Error<T> impl<T> std::error::Error for BaseError<T>
where where
T: std::fmt::Display + std::fmt::Debug, T: std::fmt::Display + std::fmt::Debug,
{ {
@ -26,7 +26,7 @@ where
} }
} }
impl<T> Display for Error<T> impl<T> Display for BaseError<T>
where where
T: std::fmt::Display, T: std::fmt::Display,
{ {
@ -35,8 +35,26 @@ where
} }
} }
impl<T> Error<T> { impl<T> BaseError<T> {
pub fn error(self) -> T { pub fn error(self) -> T {
self.error self.error
} }
pub fn from<U>(obj: BaseError<U>) -> Self
where
U: Into<T>,
{
Self {
error: obj.error.into(),
location: obj.location,
source_path: obj.source_path,
}
}
pub fn into<U>(self) -> BaseError<U>
where
T: Into<U>,
{
BaseError::from(self)
}
} }

View file

@ -7,6 +7,6 @@ mod location;
mod mode; mod mode;
pub use bytecode::*; pub use bytecode::*;
pub use error::Error; pub use error::BaseError;
pub use location::Location; pub use location::Location;
pub use mode::Mode; pub use mode::Mode;

View file

@ -118,7 +118,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
/// Represents an error during parsing /// Represents an error during parsing
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct ParseError(rustpython_compiler_core::Error<ParseErrorType>); pub struct ParseError(rustpython_compiler_core::BaseError<ParseErrorType>);
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ParseErrorType { pub enum ParseErrorType {
@ -134,7 +134,7 @@ pub enum ParseErrorType {
Lexical(LexicalErrorType), Lexical(LexicalErrorType),
} }
impl From<ParseError> for rustpython_compiler_core::Error<ParseErrorType> { impl From<ParseError> for rustpython_compiler_core::BaseError<ParseErrorType> {
fn from(err: ParseError) -> Self { fn from(err: ParseError) -> Self {
err.0 err.0
} }
@ -149,7 +149,7 @@ impl From<ParseError> for ParseErrorType {
/// Convert `lalrpop_util::ParseError` to our internal type /// Convert `lalrpop_util::ParseError` to our internal type
impl ParseError { impl ParseError {
fn new(error: ParseErrorType, location: Location, source_path: String) -> Self { fn new(error: ParseErrorType, location: Location, source_path: String) -> Self {
Self(rustpython_compiler_core::Error { Self(rustpython_compiler_core::BaseError {
error, error,
location, location,
source_path, source_path,
@ -239,7 +239,7 @@ impl ParseErrorType {
} }
impl std::ops::Deref for ParseError { impl std::ops::Deref for ParseError {
type Target = rustpython_compiler_core::Error<ParseErrorType>; type Target = rustpython_compiler_core::BaseError<ParseErrorType>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }