rustpython_parser_core/
error.rs

1use crate::text_size::TextSize;
2use std::fmt::Display;
3
4#[derive(Debug, PartialEq, Eq)]
5pub struct BaseError<T> {
6    pub error: T,
7    pub offset: TextSize,
8    pub source_path: String,
9}
10
11impl<T> std::ops::Deref for BaseError<T> {
12    type Target = T;
13
14    fn deref(&self) -> &Self::Target {
15        &self.error
16    }
17}
18
19impl<T> std::error::Error for BaseError<T>
20where
21    T: std::error::Error + 'static,
22{
23    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
24        Some(&self.error)
25    }
26}
27
28impl<T> Display for BaseError<T>
29where
30    T: std::fmt::Display,
31{
32    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
33        write!(
34            f,
35            "{} at byte offset {}",
36            &self.error,
37            u32::from(self.offset)
38        )
39    }
40}
41
42impl<T> BaseError<T> {
43    pub fn error(self) -> T {
44        self.error
45    }
46
47    pub fn from<U>(obj: BaseError<U>) -> Self
48    where
49        U: Into<T>,
50    {
51        Self {
52            error: obj.error.into(),
53            offset: obj.offset,
54            source_path: obj.source_path,
55        }
56    }
57
58    pub fn into<U>(self) -> BaseError<U>
59    where
60        T: Into<U>,
61    {
62        BaseError::from(self)
63    }
64}