1use crate::json::Value;
2use std::fmt;
3use std::rc::Rc;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct LexicalError {
11 message: String,
12 warning: Option<Rc<LexicalWarning>>,
13}
14
15impl LexicalError {
16 pub fn new(message: impl Into<String>) -> Self {
17 Self {
18 message: message.into(),
19 warning: None,
20 }
21 }
22
23 pub fn message(&self) -> &str {
24 &self.message
25 }
26
27 pub fn from_warning(warning: LexicalWarning) -> Self {
29 Self {
30 message: warning.message.clone(),
31 warning: Some(Rc::new(warning)),
32 }
33 }
34
35 pub fn warning(&self) -> Option<&LexicalWarning> {
38 self.warning.as_deref()
39 }
40}
41
42#[derive(Clone, Debug, PartialEq)]
49pub struct LexicalWarning {
50 pub code: String,
52 pub message: String,
54 pub data: Option<Value>,
56}
57
58impl Eq for LexicalWarning {}
59
60impl LexicalWarning {
61 pub const NAME: &'static str = "LexicalWarning";
63
64 pub fn new(code: impl Into<String>, message: impl Into<String>, data: Option<Value>) -> Self {
66 Self {
67 code: code.into(),
68 message: message.into(),
69 data,
70 }
71 }
72}
73
74impl fmt::Display for LexicalWarning {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(f, "{}: {}", Self::NAME, self.message)
78 }
79}
80
81impl std::error::Error for LexicalWarning {}
82
83impl fmt::Display for LexicalError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.write_str(&self.message)
86 }
87}
88
89impl std::error::Error for LexicalError {}
90
91macro_rules! invariant {
94 ($cond:expr, $($message:tt)+) => {
95 if !$cond {
96 return Err($crate::LexicalError::new(format!($($message)+)));
97 }
98 };
99}
100pub(crate) use invariant;