js2rust_predicate/
error.rs1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum ErrorKind {
8 Syntax,
10 Unsupported,
13 Type,
15 Runtime,
17 Limit,
19}
20
21#[derive(Clone, Debug)]
23pub struct Error {
24 kind: ErrorKind,
25 offset: Option<usize>,
26 message: String,
27}
28
29impl Error {
30 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
32 Self {
33 kind,
34 offset: None,
35 message: message.into(),
36 }
37 }
38
39 #[must_use]
41 pub fn with_offset(mut self, offset: usize) -> Self {
42 self.offset = Some(offset);
43 self
44 }
45
46 pub(crate) fn shift(self, base: usize) -> Self {
48 match self.offset {
49 Some(o) => Self {
50 offset: Some(o.saturating_add(base)),
51 ..self
52 },
53 None => self,
54 }
55 }
56
57 pub fn kind(&self) -> ErrorKind {
59 self.kind
60 }
61
62 pub fn offset(&self) -> Option<usize> {
64 self.offset
65 }
66}
67
68impl fmt::Display for Error {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self.offset {
71 Some(off) => write!(f, "{:?} @{}: {}", self.kind, off, self.message),
72 None => write!(f, "{:?}: {}", self.kind, self.message),
73 }
74 }
75}
76
77impl std::error::Error for Error {}