quickjs_rusty/serde/
error.rs

1use std::fmt::Display;
2
3use serde::{de, ser};
4use thiserror::Error as ThisError;
5
6use crate::{ExecutionError, ValueError};
7
8/// Error type for serde operations.
9#[allow(missing_docs)]
10#[derive(Debug, ThisError)]
11pub enum Error {
12    /// Error message.
13    #[error("{0}")]
14    Message(String),
15
16    #[error("circular reference detected")]
17    CircularReference,
18
19    #[error("end of file")]
20    Eof,
21    #[error("invalid syntax")]
22    Syntax,
23    #[error("expect boolean")]
24    ExpectedBoolean,
25    #[error("expect integer")]
26    ExpectedInteger,
27    #[error("expect float")]
28    ExpectedFloat,
29    #[error("expect string")]
30    ExpectedString,
31    #[error("expect null")]
32    ExpectedNull,
33    #[error("expect object")]
34    ExpectedObject,
35    #[error("expect array or object")]
36    ExpectedArrayOrObject,
37    #[error("expect array")]
38    ExpectedArray,
39    #[error("expect array comma")]
40    ExpectedArrayComma,
41    #[error("expect array end")]
42    ExpectedArrayEnd,
43    #[error("expect map")]
44    ExpectedMap,
45    #[error("expect map colon")]
46    ExpectedMapColon,
47    #[error("expect map comma")]
48    ExpectedMapComma,
49    #[error("expect map end")]
50    ExpectedMapEnd,
51    #[error("expect enum")]
52    ExpectedEnum,
53    #[error("trailing characters")]
54    TrailingCharacters,
55
56    #[error("big int overflow")]
57    BigIntOverflow,
58
59    /// transparent enum value for [ValueError].
60    #[error(transparent)]
61    ValueError(#[from] ValueError),
62    /// transparent enum value for [ExecutionError].
63    #[error(transparent)]
64    ExecutionError(#[from] ExecutionError),
65    /// transparent enum value for [std::io::Error].
66    #[error(transparent)]
67    IOError(#[from] std::io::Error),
68    /// transparent enum value for [anyhow::Error].
69    #[error(transparent)]
70    Others(#[from] anyhow::Error),
71}
72
73/// Result type for serde operations.
74pub type Result<T> = anyhow::Result<T, Error>;
75
76impl ser::Error for Error {
77    fn custom<T: Display>(msg: T) -> Self {
78        Error::Message(msg.to_string())
79    }
80}
81
82impl de::Error for Error {
83    fn custom<T: Display>(msg: T) -> Self {
84        Error::Message(msg.to_string())
85    }
86}