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