lattice_core/error.rs
1//! The crate's error type, [`CoreError`], and its [`CoreResult`] alias.
2
3use thiserror::Error;
4
5use lattice_protocol::ProtocolError;
6
7/// Every error `lattice-core` returns.
8///
9/// Callers mostly propagate it with `?` via [`CoreResult`]. The variants
10/// worth matching on are [`CoreError::NothingToUndo`] /
11/// [`CoreError::NothingToRedo`] (a user-facing no-op, not a failure) and
12/// [`CoreError::Cancelled`] (a cooperative abort, not a failure either).
13#[derive(Debug, Error)]
14pub enum CoreError {
15 /// A position or range was invalid for the buffer it was applied to
16 /// (out of bounds, or `end < start`). Raised by
17 /// [`Buffer::slice`](crate::Buffer::slice),
18 /// [`Buffer::apply_edit`](crate::Buffer::apply_edit) and everything built
19 /// on them.
20 #[error(transparent)]
21 Protocol(#[from] ProtocolError),
22
23 /// A filesystem read or write failed — [`Document::open`](crate::Document::open)
24 /// (including a file that is not valid UTF-8) or a save.
25 #[error("io: {0}")]
26 Io(#[from] std::io::Error),
27
28 /// [`Document::undo`](crate::Document::undo) with an empty undo stack.
29 #[error("nothing to undo")]
30 NothingToUndo,
31
32 /// [`Document::redo`](crate::Document::redo) with an empty redo stack.
33 #[error("nothing to redo")]
34 NothingToRedo,
35
36 /// [`Document::save`](crate::Document::save) on a document with no path.
37 #[error("document has no path; use save_as")]
38 NoPath,
39
40 /// A long-running operation was interrupted by a flipped
41 /// [`lattice_protocol::CancellationToken`]. Bubbles up from
42 /// the search hot loops so callers (grammar dispatcher,
43 /// substitute) can map it to their domain-specific error.
44 #[error("operation cancelled")]
45 Cancelled,
46}
47
48/// `Result` specialised to [`CoreError`].
49pub type CoreResult<T> = Result<T, CoreError>;