Skip to main content

lexical_ui/
error.rs

1use crate::json::Value;
2use std::fmt;
3use std::rc::Rc;
4
5/// A JS `invariant` failure (or another error JS throws), carrying the same
6/// message. Invariants that only a bug in a node implementation can break
7/// panic instead, as `RefCell` does; the ones a document can break return
8/// this.
9#[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    /// `warning` as the error `onWarn` receives, with its message.
28    pub fn from_warning(warning: LexicalWarning) -> Self {
29        Self {
30            message: warning.message.clone(),
31            warning: Some(Rc::new(warning)),
32        }
33    }
34
35    /// The [`LexicalWarning`] this error is, as JS's
36    /// `error instanceof LexicalWarning`.
37    pub fn warning(&self) -> Option<&LexicalWarning> {
38        self.warning.as_deref()
39    }
40}
41
42/// `LexicalWarning`: a warning with a stable code, passed to the editor's
43/// `onWarn` (as the [`LexicalError`] [`LexicalError::from_warning`] makes,
44/// whose [`warning`](LexicalError::warning) it is) by packages that report
45/// recoverable, warn-level conditions. The code is namespaced by the package that reports it
46/// (`"@example/pkg/some-condition"`); a `match` over codes needs a default
47/// arm, since packages may add codes in any release.
48#[derive(Clone, Debug, PartialEq)]
49pub struct LexicalWarning {
50    /// What happened, namespaced by the package that reports it.
51    pub code: String,
52    /// For people; it may change.
53    pub message: String,
54    /// Details for programs, documented with each code.
55    pub data: Option<Value>,
56}
57
58impl Eq for LexicalWarning {}
59
60impl LexicalWarning {
61    /// `name`.
62    pub const NAME: &'static str = "LexicalWarning";
63
64    /// `new LexicalWarning(code, message, data)`.
65    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    /// `String(warning)`: `LexicalWarning: <message>`.
76    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
91/// `invariant(cond, message, ...args)`: returns a [`LexicalError`] from the
92/// enclosing function unless `cond` holds.
93macro_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;