text_document/error.rs
1//! The public error type for the `text-document` API.
2//!
3//! Every fallible public function returns [`Result<T>`], i.e.
4//! `Result<T, DocumentError>`. Unlike the previous opaque
5//! `anyhow::Result`, callers can now match on [`DocumentError`] to react
6//! to specific failure categories (a cursor used outside a table, a
7//! lookup that found nothing, an out-of-range index, …).
8//!
9//! Errors originating deep inside the backend crates arrive as
10//! [`DocumentError::Internal`] via the `From<anyhow::Error>` bridge, so
11//! propagation with `?` continues to work unchanged.
12
13use thiserror::Error;
14
15/// Errors returned by the public `text-document` API.
16///
17/// The `Display` text is the original human-readable message; the variant
18/// carries the machine-matchable category. Marked `#[non_exhaustive]` so
19/// new categories can be added without breaking callers — match with a
20/// `_` arm.
21#[derive(Debug, Error)]
22#[non_exhaustive]
23pub enum DocumentError {
24 /// A referenced entity (block, cell, document, …) does not exist.
25 #[error("{0}")]
26 NotFound(String),
27
28 /// An operation was attempted with the cursor in the wrong structural
29 /// context (e.g. a table operation while not inside a table).
30 #[error("{0}")]
31 InvalidCursorContext(String),
32
33 /// An index or position was outside the valid range.
34 #[error("{0}")]
35 OutOfRange(String),
36
37 /// The arguments were individually valid but invalid in combination
38 /// (e.g. a selection spanning multiple frames, mismatched tables).
39 #[error("{0}")]
40 InvalidArgument(String),
41
42 /// Any other error propagated from the backend layers.
43 #[error(transparent)]
44 Internal(#[from] anyhow::Error),
45}
46
47/// Result alias used throughout the public API.
48pub type Result<T> = std::result::Result<T, DocumentError>;